using GlideAjax to retrieve multiple values off a user record

patricklatella
Mega Sage

I'm looking to populate several fields on a catalog item by looking up values on a user record based on their workID#.   I've done this successfully for one variable using a catalog client script and a script include.   However I don't want to have to create multiple catalog client scripts and script includes and would rather combine it all into 1 of each.   I'm having trouble with how to script each for multiple variables.   Any help would be great, thanks!

here's the catalog client script, in here you see the 4 variables I want to populate off the user record.   IS THIS SCRIPT CORRECT?

function onChange(control, oldValue, newValue, isLoading) {

      if (newValue == ''){

g_form.setValue('first_name','');//name of field on form you want to auto populate

g_form.setValue('last_name','');//name of field on form you want to auto populate

g_form.setValue('supervisor','');//name of field on form you want to auto populate

g_form.setValue('emp_location','');//name of field on form you want to auto populate

}

var ga = new GlideAjax('u_employee_details_lookup_Ajax');//name of script include

      ga.addParam('sysparm_name', 'getEmployeeDetails');//name of function on script include

ga.addParam('sysparm_user', g_form.getValue('emp_workday_id'));//name of field on form triggering call

      ga.getXML(EmployeeDetailsLookup); // Always try to use asynchronous (getXML) calls rather than synchronous (getXMLWait)

}

// Callback function to process the response returned from the server

function EmployeeDetailsLookup(response) {

var answer = response.responseXML.documentElement.getAttribute("answer");

//alert('answer: ' + answer);

var answers = answer.split('|');

g_form.setValue('first_name',answers[0]);

g_form.setValue('last_name',answers[1]);

g_form.setValue('supervisor',answers[2]);

g_form.setValue('emp_location',answers[3]);

}

and here is the script include: NOT SURE WHAT TO DO WITH THIS TO ACCOMMODATE FOR THE 4 VARIABLES, I KNOW THIS IS INCOMPLETE.

var u_employee_details_lookup_Ajax = Class.create();

u_employee_details_lookup_Ajax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getEmployeeDetailsfunction(){

var retVal; // Return value

var user = this.getParameter('sysparm_user');

var firstNameRec   = new GlideRecord('sys_user');//table where desired variable lives

firstNameRec.addQuery('user_name',user);

firstNameRec.query();

// Query user records

if(firstNameRec.next())

{

retVal = firstNameRec.first_name;//name of field with info you want to grab

}

return retVal;

},

});

1 ACCEPTED SOLUTION

Arindam Ghosh
Mega Guru

Hi Pat,



You can return a string from script include with all value separated by comma.


for example:



retVal = firstNameRec.first_name+','+firstNameRec.last_name+','+firstNameRec.supervisor+','+firstNameRec.emp_location;



And split this string and set the each field values in Client script:(Like below)



var answers = answer.split(',');


g_form.setValue('first_name',answers[0]);


g_form.setValue('last_name',answers[1]);


g_form.setValue('supervisor',answers[2]);


g_form.setValue('emp_location',answers[3]);



Thanks,


Arindam


View solution in original post

22 REPLIES 22

Hi Patrcik,



Thanks for sharing your script. Was struggling to pass multiple values until I saw your post.



Cheers to you and Arindam!!



Regards,


DasB


Piotr Ba_amut
Giga Expert

Hi, in my case, I've got a problem with filling in the Location variable from the user who has been selected in list collector. using glide ajax, script include, am being given correct value in alert. 

The only problem is that the reference Location doesn't want to be populated by the line: 

g_form.setValue('location',answer);

Hi Piotr,

is your alert that shows the "answer" showing a sys_id?  Can you provide a screen shot?