"nesting" a function within a function in onSubmit catalog client script

patricklatella
Mega Sage

I've got an onSubmit catalog client script on an order guide setup to create a new user record in the sys_user table.   On the first page of the order guide, I've got a reference field for the sys_id of the new user record that is create.   What I need is for the onSubmit script to create the new user record, but then also return back the new sys_id so that it can populate that field on the form.   As I have it right now it is not returning the value before moving on to the "Choose Options" page of the order guide.

Anyone done this kind of thing before?   I sort of need a function within a function.

here is my catalog client script:

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','answer');//THIS IS PROBABLY NOT CORRECT, BUT SHOWS WHAT I'M TRYING TO DO

}

// 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);//THIS RETURNS, BUT NOT IN TIME TO POPULATE THE FIELD
  alert(answer);
} else {
 
}

}

and here is my script include:

var u_New_Hire_Scripts_Ajax = Class.create();

u_New_Hire_Scripts_Ajax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getEmployeeDetails: function(){//THIS IS A DIFFERENT FUNCTION
  var retVal; // Return value
  var user = this.getParameter('sysparm_user');
 
  var detailsRec   = new GlideRecord('sys_user');//table where desired variable lives
  detailsRec.addQuery('u_workday_employee_number',user);
  detailsRec.query();
 
  // Query user records
  if(detailsRec.next())
    {
   
    retVal = detailsRec.first_name+','+detailsRec.last_name+','+detailsRec.manager+','+detailsRec.location+','+detailsRec.sys_id;
  }
 
  return retVal;
 
},

insertUserRecord: function(){//THIS IS THE FUNCTION TO CREATE THE USER RECORD AND RETURN THE VALUE OF THE NEW SYS_ID
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');
}
else{
  gs.info('Workday ID match NOT found');
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;
}
}
});

1 ACCEPTED SOLUTION

patricklatella
Mega Sage

Thanks Siva and Trey,


it ended up being quite simple, just used the getAnswer function.   This successfully determined if a user record already existed, created one onSubmit if it didn't find a match on the sys_user table, and populated the field on the order guide all in one shot.



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;


}


View solution in original post

5 REPLIES 5

patricklatella
Mega Sage

Thanks Siva and Trey,


it ended up being quite simple, just used the getAnswer function.   This successfully determined if a user record already existed, created one onSubmit if it didn't find a match on the sys_user table, and populated the field on the order guide all in one shot.



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;


}