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

kristenankeny
Tera Guru

To the end of your GlideAjax call, add ga.getXML(); Then, you'll want to set your first and last name to different sysparms (sysparm_first, sysparm_last). In the script include, you should reference the parameters as this.getParameter('sysparm_first').


Oh, and you'll also want to setValue, not getValue. I also read it's a good idea to use gr.newRecord() instead of .initialize() because it will also set any default fields you might have for the table.


Nate23
Mega Guru

I think it would be best to do this in a workflow script activity in the workflow on your catalog item This way a GlideAjax call can be avoided and it can be processed on the server side. No script include would be needed either.


your workflow run script would be:




var gr = new GlideRecord('sys_user');


gr.newRecord();


gr.first_name = current.variables.first_name;


gr.last_name = current.variables.last_name;


gr.location = current.variables.emp_location;


gr.manager = current.variables.supervisor;


gr.insert();