using GlideAjax to insert a new user record onSubmit of a form

patricklatella
Mega Sage

I want to use an onSubmit catalog client script to call a script include to insert a new user record in the sys_user table.

I am having trouble with the sysparm part of the script in the client script and how to "pass" the values I want to sent to the server to populate the new user record.

Here's where I am so far...any help would be great...thanks!

function onSubmit() {

//Create a new sys_user record and populate the fields with the values below

var ga = new GlideAjax('u_New_Hire_Scripts_Ajax');//name of script include

ga.addParam('sysparm_name', 'insertUserRecord');//name of function on script include

ga.addParam('sysparm_user', g_form.getValue('first_name'));//name of field on the form I want to pass value for

ga.addParam('sysparm_user', g_form.getValue('last_name'));//name of field on the form I want to pass value for

}

and here's the function in my script include

insertUserRecord: function(){

var user = this.getParameter('sysparm_user');

var gr = new GlideRecord('sys_user');

gr.initialize();

gr.first_name = g_form.getValue('first_name');

gr.last_name = g_form.getValue('last_name');

gr.location = g_form.getValue('emp_location');

gr.manager = g_form.getValue('supervisor');

gr.insert();

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Patrick,



Here is the updated code.



function onSubmit() {



//Create a new sys_user record and populate the fields with the values below



var ga = new GlideAjax('u_New_Hire_Scripts_Ajax');//name of script include


ga.addParam('sysparm_name', 'insertUserRecord');//name of function on script include


ga.addParam('sysparm_user_first', g_form.getValue('first_name'));//name of field on the form I want to pass value for


ga.addParam('sysparm_user_last', g_form.getValue('last_name'));//name of field on the form I want to pass value for


ga.addParam('sysparm_user_location',   g_form.getValue('emp_location'));


ga.addParam('sysparm_user_supervisor',   g_form.getValue('supervisor'));


}




and here's the function in my script include




insertUserRecord: function(){


var first_name = this.getParameter('sysparm_user_first');


var last_name = this.getParameter('sysparm_user_last');


var emp_location = this.getParameter('sysparm_user_location');


var supervisor = this.getParameter('sysparm_user_supervisor');


var gr = new GlideRecord('sys_user');


gr.initialize();


gr.first_name = first_name;


gr.last_name = last_name;


gr.location = emp_location;


gr.manager = supervisor;


gr.insert();



Note: g_form should not be used at script include as it triggers only at client side and not at server.


View solution in original post

16 REPLIES 16

Oh, and you'll probably want to return false when you have the error so they cannot proceed.


patricklatella
Mega Sage

ahh, right thanks!   Ok so I believe the following things are working:



- it is creating a new user record if it cannot find a match


- it is not creating a new user record if it does not find a match



here's what is not working yet:


- onSubmit of the form, if a new user record is created (because a match of the workday ID was not found), then I need the new sys_id of the user record that was just created to return back to the form to populate the "user" field on the form.   Now of course the onSubmit in this case (since it's an Order Guide") is the "Choose Options" button...so you're actually leaving the form...however is it still possible to get that value back at that moment?   I need it to populate a field on each of the corresponding catalog items that are a part of the order guide.   Is this possible?


Just to double check: the user fields on your subsequent forms have the same backend name as the user field on the order guide form and you have "cascade variables" checked on your order guide?


patricklatella
Mega Sage

I'm actually using a rule base for this...and when I just enter values that are already there it cascades properly.   So where it's at is, I can confirm that the sys_id can be returned in the "answer" function below in my script, but cannot figure out how to have it use the getXMLWait on the onSubmit function to retrieve the new sys_id before moving on.



here's my current script, with the checks in place to show what is returning and when.



function onSubmit() {

//Create a new sys_user record and populate the fields with the values below
var ga = new GlideAjax('u_New_Hire_Scripts_Ajax');//name of script include
ga.addParam('sysparm_name', 'insertUserRecord');//name of function on script include
ga.addParam('sysparm_firstname', g_form.getValue('first_name'));//do this for all variables
ga.addParam('sysparm_lastname', g_form.getValue('last_name'));
ga.addParam('sysparm_supervisor', g_form.getValue('supervisor'));
ga.addParam('sysparm_location', g_form.getValue('emp_location'));
ga.addParam('sysparm_workdayID', g_form.getValue('emp_workday_id'));

ga.getXMLWait(PopulateUserVariable); // Always try to use asynchronous (getXML) calls rather than synchronous (getXMLWait)
g_form.setValue('user','7b4d65ca4f1ab640038fcb4e0210c7a5');


}


// Callback function to process the response returned from the server
function PopulateUserVariable(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer){
  //g_form.setValue('user','7b4d65ca4f1ab640038fcb4e0210c7a5');
  alert(answer);
} else {
  //something here??
}

}


patricklatella
Mega Sage

Hi Kristen,


thanks so much for your help, I marked Pradeep's as correct to give him credit since I based my script in the end of his.   I also closed this query up because I pretty much now changed the topic to how to "nest" a function within a function and started a different thread.



My issue now is how to get the sys_id returned from the newly created user record to populate the field on the "Describe Needs" page of the order guide before leaving that page onSubmit.



I've seen a couple blogs about "nesting" or "currying" a function within a function but am struggling to get it to work.   Are you familiar?