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

harishdasari
Tera Guru

Hi Patrick,



I suggest you to go through this link it will be very for you to get your desired requirement.



https://fruitionpartners.eu/blog/2015/11/17/glideajax-return-multiple-values-using-json/



Thanks


manishm
Mega Guru

Hi Patrick,



You can return the data as a string (say comma separated) and parse (not recommended) or use the json format (recommended). The following links explain the approach in detail:



multiple values from Script Include


Return multiple values from Glide Ajax



Manish


patricklatella
Mega Sage

H Manish & Harish,


I've followed the explanation from your suggested link and it's not working just yet...I'm not sure I'm doing exactly what the link you sent is doing, but I've tried to get it close.   Can you see what might be wrong with scripts?



HERE IS MY CATALOG CLIENT SCRIPT:



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



answer = answer.evalJSON(); //Transform the JSON string to an object


for( var i=0 ; i < answer.length ; i++){//loop into the array


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


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


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


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



}


}



AND HERE IS MY SCRIPT INCLUDE:



var u_employee_details_lookup_Ajax = Class.create();



u_employee_details_lookup_Ajax.prototype = Object.extendsObject(AbstractAjaxProcessor, {



getEmployeeDetails: function(){


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


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


EmployeeDetailsRec.query();



var array = [];



while(user.net()) {


var object = {};


object.firstName = EmployeeDetailsRec.getDisplayValue('first_name');


object.lastName = EmployeeDetailsRec.getDisplayValue('last_name');


object.supervisory = EmployeeDetailsRec.getDisplayValue('manager');


object.location = EmployeeDetailsRec.getDisplayValue('location');


array.push(object);


}



    var json = new JSON();


var data = json.encode(array); //JSON formatted string



return data;


},



type: 'u_employee_details_lookup_Ajax'



});