Ajax and JSON to fill fields

KB15
Giga Guru

My requirement is to return multiple fields from a user's manager's fields (the manager's email and username) based on field. I'm a little lost with JSON with Ajax. I can mostly understand Ajax.

I'm getting confused on the server side section based. I mostly based this on this post: GlideAjax Example Cheat Sheet

I'm not sure how it's getting its query results and how to pull those to the client side to fill in the fields.

This is my client script

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

    if (isLoading || newValue == '') {

          return;

    }

var employee = g_form.getValue('u_employee');

var ga = new GlideAjax('getEmployeeFields');

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

ga.addParam('syparm_emp', employee);

ga.getXML(stateCallback);

}

function stateCallback(response) {

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

if (answer) {

var returneddata = answer.evalJSON(true);

//g_form.setValue('y_employee_username', sepEmp.user_name);

//g_form.setValue('y_employee_dn', sepEmp.u_display_name);

//g_form.setValue('y_employee_email', sepEmp.email);

g_form.setValue('y_manager_username', returneddata.manager_username);

//g_form.setValue('y_manager_email', returneddata.manager_email);

}

}

This is my script include

var asu_GetLocationData = Class.create();

asu_GetLocationData.prototype = Object.extendsObject(AbstractAjaxProcessor, {

      getEmployeeData: function () {

              var employee = this.getParameter('sysparm_emp');

              var userTable = new GlideRecord('sys_user');

              if (userTable.get(employee)) {

              var json = new JSON();

              var results = {

              "manage_username": userTable.getValue('manager.user_name'),

              "manager_email": userTable.getValue('manager.email')

            };

            return json.encode(results);

          }          

    }

});

1 ACCEPTED SOLUTION

KB15
Giga Guru

I was able to play around with some of the script and found a few typos. Other than the typos, I was running into an [object Object] error with my results. I used a combination of suggestions this what worked for me. I found that using getDisplayValue() fixed the [object Object] error but I'm not sure if that would be right fix. I'm curious why I couldn't go down another level if I used



userTable.getValue('manager.user_name');



If there are best practices I should be using, please feel free to correct. I'd rather learn the right way.



Server Side


var getEmployeeFields = Class.create();


getEmployeeFields.prototype = Object.extendsObject(AbstractAjaxProcessor, {



getEmployeeData: function () {


var employee = this.getParameter('sysparm_emp');


var userTable = new GlideRecord('sys_user');


if (userTable.get(employee)) {



var results = {


'musername': userTable.getDisplayValue('manager.user_name'),


'memail': userTable.getDisplayValue('manager.email'),


'username': userTable.getValue('user_name'),


'displayname': userTable.getValue('u_display_name'),


'email': userTable.getValue('email')


};



return JSON.stringify(results);


}        


},



type: 'getEmployeeFields'


});



Client Side


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


    if (isLoading || newValue == '') {


          return;


    }



var employee = g_form.getValue('u_employee');



var ga = new GlideAjax('getEmployeeFields');


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


ga.addParam('sysparm_emp', employee);


ga.getXML(stateCallback);


}



function stateCallback(response) {


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


if (answer) {


var returneddata = JSON.parse(answer);


g_form.setValue('y_employee_username', returneddata.username);


g_form.setValue('y_employee_dn', returneddata.displayname);


g_form.setValue('y_employee_email', returneddata.email);


g_form.setValue('y_manager_username', returneddata.musername);


g_form.setValue('y_manager_email', returneddata.memail);


}



}


View solution in original post

6 REPLIES 6

I think, you are following the right way to achieve your requirement. There are different ways to get the reference field value and one of them is getDisplayValue(). If you will use getValue() that will land up giving you the sys_id instead of display value.



Glad your issue is fixed now.


Thank you for your encouragement!