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

patricklatella
Mega Sage

now tinkering with the JSON script I've gotten to where I believe the script include is passing the info correctly back to the client, but my client script is not parsing out the info correctly.   I've put in a gs.info into the script include and am seeing in the log what appears to me to show that the info is being captured properly.  


But it's not populating the fields on the form properly, so I believe my client script is where the problem is.



find_real_file.png



here's the catalog client script:



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


if (isLoading){


return;


}


if (newValue == ''){  


g_form.setValue('first_name','');  


g_form.setValue('last_name','');


g_form.setValue('supervisor','');


g_form.setValue('emp_location','');


}  



if (newValue != oldValue) {


var ga = new GlideAjax('u_employee_details_lookup_Ajax');


ga.addParam('sysparm_name', 'getEmployeeDetails');  


ga.addParam('sysparm_user', newValue);                  


ga.getXML(EmployeeDetailsLookup);


}                                


}  



// 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


alert(answer);


alert(answer.firstName);


alert(answer.lastName);


alert(answer.supervisor);


alert(answer.location);



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



g_form.setValue('first_name', answer.firstName);


g_form.setValue('last_name', answer.lastName);


g_form.setValue('supervisor',answer.manager);


g_form.setValue('emp_location',answer.location);


}


}



and here's the script include:



var u_employee_details_lookup_Ajax = Class.create();


u_employee_details_lookup_Ajax.prototype = Object.extendsObject(AbstractAjaxProcessor, {



getEmployeeDetails: function(){



var object = {};



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


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


emp.addQuery('u_workday_employee_number',wdNumber);


emp.query();


emp.next();


gs.info('u_employee_details_lookup_Ajax: rowcount=' + emp.getRowCount());


if(emp.getRowCount()>0){


gs.info('u_employee_details_lookup_Ajax: First Name=' + emp.first_name);


gs.info('u_employee_details_lookup_Ajax: Last Name=' + emp.last_name);


gs.info('u_employee_details_lookup_Ajax: Manager =' + emp.manager);


gs.info('u_employee_details_lookup_Ajax: Location =' + emp.location);


object.firstName = emp.first_name;


object.lastName = emp.last_name;


object.supervisor = emp.manager;


object.location = emp.location;


var json = new JSON();


return json.encode(object);//JSON formatted string


}



},



type: 'u_employee_details_lookup_Ajax'



});


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


patricklatella
Mega Sage

Hi Arindam,


thanks for the reply.   Are you suggestions meant to go into my original scripts at the beginning of this thread?   Or into these attempts using JSON?



Patrick


Please try with below script if this helps, it worked for me.



Client Script:


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


if (isLoading){


return;


}


if (newValue == ''){


g_form.setValue('first_name','');


g_form.setValue('last_name','');


g_form.setValue('supervisor','');


g_form.setValue('emp_location','');


}


if (newValue != oldValue) {


var ga = new GlideAjax('u_employee_details_lookup_Ajax');


ga.addParam('sysparm_name', 'getEmployeeDetails');


ga.addParam('sysparm_user', newValue);                


ga.getXML(EmployeeDetailsLookup);


}                              


}


// 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


alert('FN:   ' + answer.firstName + ', LN:   ' + answer.lastName + ', Manager:   '+ answer.supervisor + ', Location:   '+ answer.location);


g_form.setValue('first_name', answer.firstName);


g_form.setValue('last_name', answer.lastName);


g_form.setValue('supervisor',answer.manager);


g_form.setValue('emp_location',answer.location);


}



Script Include:


var u_employee_details_lookup_Ajax = Class.create();


u_employee_details_lookup_Ajax.prototype = Object.extendsObject(AbstractAjaxProcessor, {


getEmployeeDetails: function(){


var object = {};


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


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


//emp.addQuery('u_workday_employee_number',wdNumber);


emp.addQuery('sys_id', wdNumber); //Changed the u_workday_employee_number to sys_id


emp.query();


emp.next();


gs.info('u_employee_details_lookup_Ajax: rowcount=' + emp.getRowCount());


if(emp.getRowCount()>0){


gs.info('u_employee_details_lookup_Ajax: First Name=' + emp.first_name);


gs.info('u_employee_details_lookup_Ajax: Last Name=' + emp.last_name);


gs.info('u_employee_details_lookup_Ajax: Manager =' + emp.getDisplayValue('manager'));


gs.info('u_employee_details_lookup_Ajax: Location =' + emp.getDisplayValue('location'));


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


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


object.supervisor = emp.getDisplayValue('manager');


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


var json = new JSON();


return json.encode(object);//JSON formatted string


}


},


type: 'u_employee_details_lookup_Ajax'


});



find_real_file.png


patricklatella
Mega Sage

Thanks everyone!



I actually got it to work using my original scripts from the beginning of this thread and the simple edits from Arindam (although I had to change a couple field names).  


Obviously it appears as though there might be several ways to achieve this, so hopefully with all the suggestions on this thread people can see various options.



So the final scripts I used were actual quite simple I believe and no need for JSON or Objects...just had to split up the retVal per Arindam's suggestion.



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



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



}



script include:



var u_employee_details_lookup_Ajax = Class.create();



u_employee_details_lookup_Ajax.prototype = Object.extendsObject(AbstractAjaxProcessor, {



getEmployeeDetails: function(){


var retVal; // Return value


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



var detailsRec   = new GlideRecord('sys_user');


detailsRec.addQuery('user_name',user);


detailsRec.query();



// Query user records


if(detailsRec.next())


{



retVal = detailsRec.first_name+','+detailsRec.last_name+','+detailsRec.manager+','+detailsRec.location;


}



return retVal;



},



});