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

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.


patricklatella
Mega Sage

Thanks everyone for the input!



I'm working forward with the onSubmitHello Pradeep,


thanks for the scripts, I had made some updates and they match pretty closely yours.



One question, as they are right now, the onSubmit client script is creating a new record on the sys_user table even if there is already a user record that has the "emp_workday_id" value.



So I need something that will check if there is an existing user record, and if so then it does not run the script include and does not create a new record.



So basically I need to pass the value of the "emp_workday_id" variable to the sys_user table and if it finds a match, it does not create a new record.


To do that, update your glideajax call to pass another parameter, then you can update the script include:



insertUserRecord: function(){


var eId = this.getParameter('sysparm_emp_workday_id');


var u = new GlideRecord('sys_user');


u.addQuery('column_you_have_id_stored_in',eId);


u.query();


if(u.next()){


              gs.info('Workday ID match found'); //you can add more information here so that if the submit doesn't do the insert you can research why


}


else{


        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();


}


patricklatella
Mega Sage

hi Kristen,


thanks for the reply.   So I've implemented your script and I'm not sure it's working...should the "gs.info" part be showing me a screen message if a match is found?



If so, then something might be wrong, can you check out my scripts?



(this is on an order guide by the way, the "Describe Needs" page) - the client script is meant to check the sys_user table to see if there is an existing record with the Workday ID value that is entered on the form in the "emp_workday_id" field.   The variable on the user record for this is "u_workday_employee_number".   If there is not a match I want it to create a user record with the values listed in the script, but then I also want it to return the new sys_id of the created record to populate the "user" field on the form.



onSubmit catalog client script:



function onSubmit() {

//Create a new sys_user record IF there is no matching Workday ID 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_workdayID', g_form.getValue('emp_workday_id'));
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.getXML(PopulateUserVariable); // Always try to use asynchronous (getXML) calls rather than synchronous (getXMLWait)
}


// 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',answer);


} else {
  //DOES SOMETHING NEED TO BE HERE?
}

}



and my script include:



insertUserRecord: function(){


var workdayID = this.getParameter('sysparm_workdayID');//do this for each variable


var u = new GlideRecord('sys_user');


u.addQuery('u_workday_employee_number',workdayID);


u.query();



if(u.next()){


  gs.info('Workday ID match found'); //you can add more information here so that if the submit doesn't do the insert you can research why


}


else{


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;


}





Hi Patrick,



The gs.info would enter a line in the logs, so it's more for the developers/system admins to be able to look into why it might have failed to create a user.



In your client script, I think you'll want to add gs.addInfoMessage('Message you want shown') to alert them to the issue. You could also use Error message: GlideSystem - ServiceNow Wiki.