Returning Record from the Script Include

NehaSNOW
Tera Guru

Dear Members,

 

I have designed the Script Include which is returning the User data to the Catalog Client Script. After selecting the Employee Id in the Catalog Item it is showing the Employee Name as [object Object] instead of User name.

Please suggest.

 

Catalog Client Script: -

 

function onChange(control, oldValue, newValue, isLoading) {
    if (!isLoading) {
        g_form.addInfoMessage("TEST VALUE1 "+ newValue);

        var ga = new GlideAjax('UserDetails');
        ga.addParam('sysparm_name', 'DetailsofUser');
        ga.addParam('sysparm_user', newValue);
        ga.getXMLAnswer(EmpRecord);

        function EmpRecord(response) {
            var emp = JSON.parse(response);
            if(emp){
                g_form.addInfoMessage("TEST VALUE2 " + emp.name);
                g_form.setValue('employee_name', emp.name);
            }
        }
    }
}
 
Script Include: -
 
var UserDetails = Class.create();
UserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    DetailsofUser: function(){
    var obj = {};
    var usr = this.getParameter('sysparm_user');
    var grUser = new GlideRecord('sys_user');
    grUser.addQuery('sys_id', usr);
    grUser.query();

    if(grUser.next()){
        obj.name = grUser.name;
        obj.email = grUser.email;
        obj.department = grUser.department;
    }
    return JSON.stringify(obj);
 },

    type: 'UserDetails'
});
 
Thanks,
Neha Pateria
3 REPLIES 3

AshishKM
Kilo Patron
Kilo Patron

Hi @NehaSNOW ,

 

You don't need to write GlideAjax, instead use the Auto-populate feature and get the desire column value of selected user record.

 

If you have 3 different field on form, open the field definition page and select the Auto-Populate tab and select the User Field name in Dependent Question, first two field will be same for all 3 field only, change the dot walk path for each one.

 

AshishKM_1-1743537297199.png

 

 

-Thanks,

AshishKM

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Bert_c1
Kilo Patron

Hi @NehaSNOW 

 

In my example that does something similar, my client script call-back function has:

 

function EmployeeDetailsLookup(response) {
	var answer = response.responseXML.documentElement.getAttribute("answer");
	var result = JSON.parse(answer);
	g_form.setValue('requested_for',result.name);
	g_form.setValue('cost_center',result.cc);
//	alert("User: " + result.name + " Cost Center = " + result.cc + '\nDepartment: ' + result.dept + ', email: ' + result.email);
}

I needed line 2 in the above, you can test using the commented-out 'alert()' there to see what is being returned.

 

I converted the values in the object to String. Your script include can do the same

   if(grUser.next()){
        obj.name = grUser.name.toString();
        obj.email = grUser.email.toString();
        obj.department = grUser.department.toString();
    }

 

Vishal Jaswal
Giga Sage

Hello @NehaSNOW 

There are two ways to achieve it:

1. onChange Catalog Client Script and Script include -- which you did however just need to make one correction in your script include and one correction in Client Script:

    obj.name = grUser.getValue('name');
        obj.email = grUser.getValue('email');
        obj.department = grUser.getDisplayValue('department');
   g_form.addInfoMessage("TEST VALUE2 " + emp.name);
                g_form.setValue('employee_name', emp.name);
                g_form.setValue('email', emp.email);
                g_form.setValue('department', emp.department.toString());

 
Here are the validation results:


Before:

 

VishalJaswal_2-1743539181987.png

After:

VishalJaswal_0-1743540085318.png

 

 

2. The other way is to achieve it without coding i.e using Auto populate feature of the variables: The only requirement is that all these variables have to be type: Reference

An example of Employee Name Reference Type variable:

VishalJaswal_5-1743539709934.png

 

VishalJaswal_4-1743539622202.png


Validation Results:

VishalJaswal_6-1743539747312.png


Hope that helps!