Questions and discussion about developing processes and programming in PHP, JavaScript, web services & REST API.
Forum rules: Please search to see if a question has already asked before creating a new topic. Please don't post the same question in multiple forums.
#828766
hi
I'm trying to update Users by API with jquery Ajax
but I dont know how to post data from jquery Ajax
I read wiki but no sample exist
please give me a sample
this is my data :
{
usr_birthday: "2019-09-14"
usr_create_date: "2019-09-14 00:47:00"
usr_due_date: "2021-09-14"
usr_email: "Administrator@oautomation.ir"
usr_firstname: "fghf"
usr_lastname: "fghfgh"
usr_photo_path: "/opt/processmaker/workflow/public_html/images/user.gif"
usr_role: "PROCESSMAKER_OPERATOR"
usr_status: "ACTIVE"
usr_uid: "1082202175d7bf93c319774069666327"
usr_update_date: "2019-09-14 00:47:00"
usr_username: "Administrator"
usr_ux: "NORMAL"
}

this is error :
{
"error": {
"code": 400,
"message": "Bad Request: This user: , can not update the data."
}
}
#828786
Hi Amir,

I found JS example to do the REST API PUT - for update, kindly follow the example see if get what you want. If not the doc has PHP script that you can use trigger to update User's info instead of using JS. Good luck!

https://wiki.processmaker.com/3.0/Calling_REST_Endpoints#PUT_requests_2

Pls go to the above link, some of its copy shown here below, FYI:
P.S. The login user to do this JS must be having Admin right..
Code: Select all
//The following example changes the status of a user to "VACATION" and sets the user who will replace this user when they goes on vacation.

$pmServer = "https://example.com";            //set to your ProcessMaker address
$userId = "75740355754dd28ba73d7d6082172937"; //set to the unique ID of a user

var oVars = {
  'usr_status':      "VACATION",
  'usr_replaced_by': "14680180454ca4477335a27034362107"
};

for (var v in oVars) {
   if (oVars.hasOwnProperty(v))
      oVars[v] = encodeURIComponent(oVars[v]);
}

var req = XMLHttpRequest();
req.open('PUT', pmServer + '/api/1.0/workflow/user/' + userId, true);
req.setRequestHeader("Authorization", "Bearer " + getCookie("access_token"));
var sVars = JSON.stringify(oVars);
req.setRequestHeader('Content-type','application/json; charset=utf-8');
req.setRequestHeader('Content-length', sVars.length);
#828800
hi kirkwg
I tried this code
But this code had some problems
First of all :
var req = XMLHttpRequest();
This part of the code gives a syntax error because did not use 'new'
this is syntax error : Please use the 'new' operator, this DOM object constructor cannot be called as a function.
why process maker wiki is wrong?
Secondly :
Returns the same error again:
{"error":{"code":400,"message":"Bad Request: This user: , can not update the data."}}
Thanks for your response, but I could not use it
User avatar
By kirkwg
#828801
Hi Amir,
Thanks for your trying out, and I agree that missed new, the doc may not updated or something else...

I suggest use an easier way $.ajax(), try follow example 2 in the doc below using PUT,
https://wiki.processmaker.com/3.1/JavaScript_Functions_and_Methods
In case still not working I will test it by myself on Monday...
Keep me updated, this issue is supposed not complicated, Good luck again....cheers :roll:
#828802
hi kirkwg
Thank you for taking the time to solve my problem
My main problem is whether the data is sent correctly in the put method ?
Because in this example, it seems that there is no place to send data
https://wiki.processmaker.com/3.0/Calling_REST_Endpoints#PUT_requests_2

In this example too, I do not see a place to send data
https://wiki.processmaker.com/3.1/JavaScript_Functions_and_Methods

My next problem is that when I send data in the same way and use encodeURIComponent, the email gets an error due to the use of the @ character.

this is MyCode By $.ajax
var userInfo = {
usr_birthday: "2021-01-02",
usr_due_date: "2021-12-12",
usr_firstname: "Amir",
usr_lastname: "Shapourian" ,
usr_role: "PROCESSMAKER_OPERATOR",
usr_status: "ACTIVE",
usr_username: "AmirShapourian",
usr_email : 'AmirShapourian@gmail.com'
usr_uid: "5048239955ff07f1f9d7263033053397",
usr_ux: "NORMAL"
}
var strModel = {}
$.each(this.userInfo , function(index , obj){
if (obj) {
strModel[index] = encodeURIComponent(obj)
}
})
var baseApiAddress = 'myServerAPIAddress'
_Token = getAccessToken() // method to get token
$.ajax({
url : baseApiAddress + "api/1.0/workflow/user/" + userInfo.usr_uid ,
data : JSON.stringify(strModel) ,
type : "PUT" ,
beforeSend : function(req){
req.setRequestHeader('Content-type','application/json; charset=utf-8');
req.setRequestHeader('Authorization',"Bearer " + _Token.access_token );
}
success: function (result){
console.log('success');
} ,
error: function (xhr , status, message){
console.log('error');
}
});
User avatar
By kirkwg
#828808
Hi Amir,

I test the following JS code is working on my side, that I can change/edit 2 fields of user info, even change the email address has no problem with '@' char which is not the error cause.
Code: Select all
var host = PMDynaform.getHostName();  
var ws = PMDynaform.getWorkspaceName(); 	//workflow

var token = PMDynaform.getAccessToken();
//alert("token: " + token);

//------------- REST GET in ajax()
function getUserInfo() {           //GET you can ignore the GET code as you just want PUT, I also attach FYI only
  	var user = "6413636415f4c68721324c1111111111";   //put your user UID here for changing its fields
  
    alert(host + "/api/1.0/" + ws + "/user/" + user);
  
	var      x  = 	[];
	$.ajax({
	    url:		host + "/api/1.0/" + ws + "/user/" + user, 
	    data:		{},		//JSON.stringify( {param1: "value1", param2: "value2"} )
	    type:		"GET",
	    beforeSend: 	function(xhr) {
			xhr.setRequestHeader('Authorization', 'Bearer ' + token);
	     },
	    success:	function(user) {
         						alert(JSON.stringify(user));
          						console.log(JSON.stringify(user));
		x.push(user);
	    	$('#example').DataTable ({                    //here use external Lib or plugin DataTable for display the data I got, ignore it..
		       "data":	x,
		       "columns": [
			   { "data": "usr_firstname", "title": "First Name"},    
			   { "data": "usr_lastname", "title": "Last Name"},
			   { "data": "usr_email", "title": "Email"}
			] 
		}); //dataTable
	   	}  //success end
	}) ;
} 
$("#show").click(getUserInfo);      //Button event handler

//------------- REST PUT in ajax()
function putUserInfo() {
  	var user = "<your user uid here>";
  
    //alert(host + "/api/1.0/" + ws + "/user/" + user);
  
	$.ajax({
	    url:		host + "/api/1.0/" + ws + "/user/" + user, 
	    data:		JSON.stringify({usr_address: "Main Street 1234567", usr_email: "test.01@abc11.com"}),	
      	                          //JSON.stringify( {param1: "value1", param2: "value2"} )
	    type:		"PUT",
      
           contentType: "application/json",

           dataType: 'text',
      
	    beforeSend: 	function(xhr) {
			xhr.setRequestHeader('Authorization', 'Bearer ' + token);
	     },
	    /*success:	function(user) {
         						alert(JSON.stringify(user));
          						console.log(JSON.stringify(user)); */
        success: function(xhr, status, error) {
          alert("Success" + (xhr.responseText ? ": "+xhr.responseText: ''));
        },
        error: function(xhr, status, error) {
          if (xhr.responseText) {
            var oResponse = JSON.parse(xhr.responseText);
            alert(oResponse.error.message)
          } else {
            alert("Error: " + error);
          }
        }
	}) ;
} 
putUserInfo();       //directly call the put function
The code tested workable, I try to change the field usr_country which was shown bad command, so there is possible some fields could not be changed or not....The code above at least shows that it is correct code and workable. I can get back the same user and found its content fields has been changed correctly. Hope this can solve your problem.
#828810
thank you
I tested these codes in all ways, but the error is still the same. I even tested this with postman, but it still did not work.
It is interesting that when I change the names of the variables, it still has no effect. For example, I put usr_address1 instead of usr_address , but it still gives the same error.
data : JSON.stringify({usr_address1: "Main Street 1234567", usr_email: "test.01@abc11.com"})
I decided to use PHP code to do this
#828813
1. First you need to check if you have the admin right or not to get the valid token? (login with admin-right) and run the preview

2. If the code is tested okay, meant not something with the code above should be something else that you missed other to notice?

3. usr_address1 is not a proper parameter name as well in PMT_USER_INFORMATION table of processmaker.

A 1xbet clone script is a pre-designed software so[…]

4rabet clone script is enabling entrepreneurs to e[…]

Parimatch clone script is enabling entrepreneurs t[…]

In the world of cryptocurrency, a wallet is an app[…]