- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2017 01:24 PM
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;
}
}
});
Solved! Go to Solution.
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2017 04:40 PM
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;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2017 01:28 PM
something like a "function within a function" that I've seen some blogs about: this is not working, but am I maybe close? thanks!
function onSubmit() {
function PopulateUserVariable(response) {
//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)
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer){
g_form.setValue('user',answer);
alert(answer);
} else {
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2017 03:22 PM
here's my latest script...and you'll see the 2 alerts towards the end. Now oddly both alerts show BEFORE the onSubmit finishes (like they should) and goes to the "Choose Options" page of the order guide. However the g_form.setValue('user',answer); does not activate between the two alerts like I would think it should. It has the correct value at that point as the alerts indicate, but it doesn't populate the field.
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.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){
alert(answer);
g_form.setValue('user',answer);
alert("hello");
return function (PopulateUserVariable){};
} else {
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2017 03:52 PM
Hi Patrick, you should try to setup your logic in the onChange logic of the variables, not onSubmit if you want the user to see the data populated before the Submission (and hence moving to the next page). The tricky part is to set this up on all the variables that are needed and writing logic such that all the mandatory data is available. It will execute on filling the last mandatory field. Challenge with what you are doing though is, if there is a typo and the user corrects it, you might end up with two user records.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2017 04:22 PM
Hi Patrick,
I have faced a similar issue myself when trying to call a GlideAjax script include asynchronously. The issue is that without getXMLWait() the submit proceeds without blocking to allow the value to be returned from the Ajax call.
But I see that one of your variants actually uses getXMLWait() (except you have included a comment about how the synchronous version is bad, so I'm confused). In this case you definitely need the synchronous (wait) variant.
The way you have it written it looks like it will have already submitted the form with the blank (or old) user value, before the callback returns and populates it into the form.
I can tell you that a function within a function is not the issue, as I have used the construct many times without issues. Javascript functions are really just objects under the hood and those other functions are just properties.
Regards,
Trey Carroll