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

I have had functions call other functions. Two possible options: add a setTimeout to force it to wait for the field to actually be populated before it proceeds. Or, you could change this from an onSubmit client script to a UI Macro button that runs the code instead and then require the user field.


patricklatella
Mega Sage

Hi Kristen,


I ended up using getAnswer with an getXMLWait, quite simple in the end.   Thanks for your help!   Here are my scripts:



this is the onSubmit:



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();
g_form.setValue('user',ga.getAnswer());


}



and this was the function in my script include:



insertUserRecord: function(){


var workdayID = this.getParameter('sysparm_workdayID');


var u = new GlideRecord('sys_user');


u.addQuery('u_workday_employee_number',workdayID);


u.query();



if(u.next()){


gs.info('Workday ID match found');//here for testing


}


else{


gs.info('Workday ID match NOT found');//here for testing


var firstName = this.getParameter('sysparm_firstname');


var lastName = this.getParameter('sysparm_lastname');


var manager = this.getParameter('sysparm_supervisor');


var location = this.getParameter('sysparm_location');


var gr = new GlideRecord('sys_user');


gr.initialize();


gr.first_name = firstName;//do this for each variable


gr.last_name = lastName;


gr.manager = manager;


gr.location = location;


gr.u_workday_employee_number = workdayID;


gr.insert();



var retVal = gr.sys_id;


return retVal;


}