Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Cannot get display value but keep showing sys_id instead

Simon88
Kilo Contributor

Hi, I got no issue displaying all the variables below except just cannot get 'Cost_Centre' to display its value.

The below code will only display sys_id for g_form.setValue('Cost_Centre',requestor.cost_center) on the form;

Then if I tried 'requestor.cost_center.getDisplayValue()' already, it just not showing anything on the form value at all.

What's wrong with this value?

---------------------------------------------------------------------------------

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
   return;
}

if(g_form.getValue('Employee_Name')!='')
   {
   //var requestor = g_user.userID;
   var requestor = g_form.getReference('Employee_Name', autopopulate);
   }
}

function autopopulate(requestor)
{
   if (requestor)
   {
      g_form.setValue('Line_Manager',requestor.manager);
      g_form.setValue('Job_Title',requestor.title);
      g_form.setValue('contact_number',requestor.phone);
      g_form.setValue('Department',requestor.department);
      g_form.setValue('site',requestor.location);
      g_form.setValue('Entity',requestor.u_entity);
      g_form.setValue('emp_number',requestor.employee_number);
      g_form.setValue('emp_status',requestor.active);
      g_form.setValue('Cost_Centre',requestor.cost_center);
   }
}

 

Regards,

Simon.

1 ACCEPTED SOLUTION

Alok Das
Tera Guru

Hi Simon,

I believe you want to get details of an employee from user table, if my understanding is correct then I would recommend you to use json parameter and get all the values from 'sys_user' table with the help of Glide Ajax and creating a script include. Since, getReference() is the least efficient function.

You could refer to the below link for more details on getReference

getReference() - best practice or not?

If you would like to use my approach you can use the scripts below:

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ga = new GlideAjax('Employee_Details');
    ga.addParam('sysparm_name', 'details');
    ga.addParam('sysparm_employee', newValue);
    ga.getXML(setDetails);
}

function setDetails(response) {

    var answer = response.responseXML.documentElement.getAttribute("answer");
    if (answer != 'false') {
        var requestor = answer.evalJSON();
        g_form.setValue('Line_Manager', requestor.manager);
        g_form.setValue('Job_Title', requestor.title);
        g_form.setValue('contact_number', requestor.phone);
        g_form.setValue('Department', requestor.department);
        g_form.setValue('site', requestor.location);
        g_form.setValue('Entity', requestor.u_entity);
        g_form.setValue('emp_number', requestor.employee_number);
        g_form.setValue('emp_status', requestor.active);
        g_form.setValue('Cost_Centre', requestor.cost_center);
    }
}

Script Include:

Name: Employee_Details

Client Callable: True(Check the checkbox)

var Employee_Details = Class.create();
Employee_Details.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    details: function() {
        var value = 'false';
        var gr = new GlideRecord('sys_user');
        if (gr.get(this.getParameter('sysparm_employee'))) {
            var object = {};
            object.manager = gr.manager;
            object.title = gr.title;
            object.phone = gr.phone;
            object.department = gr.department;
            object.location = gr.location;
            object.u_entity = gr.entity;
            object.employee_number = gr.employee_number;
            object.active = gr.active;
            object.cost_center = gr.getDisplayValue('cost_center');
            value = JSON.stringify(object);
        }
        return value;
    },
    type: 'Employee_Details'
});

Kindly mark my answer as Correct and Helpful based on the Impact.

Regards,

Alok

View solution in original post

8 REPLIES 8

change below

var grCostCentre = new GlideRecord('cmn_cost_center');
 

Done that but now it just not match and pre-select the correct cost centre value and just stay as -none-. Might have to raise this with HI, since we have other forms also pulling out user details and those cost centres are just a straight forward assignment and no issue, it is just this form.

Sorry, my bad. Sorry for not testing my code before posting. Was missing "()" after .getDisplayValue.

var getCostCenterName = Class.create();
getCostCenterName .prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getDisplayName: function() {
        var costCentreId = this.getParameter('sysparm_id');
        var grCostCentre = new GlideRecord('');
        if (grCostCentre.get(costCentreId)) {
            return grCostCentre.getDisplayValue();
        }
        return '';
    },
    type: 'getCostCenterName'
});

Alok Das
Tera Guru

Hi Simon,

I believe you want to get details of an employee from user table, if my understanding is correct then I would recommend you to use json parameter and get all the values from 'sys_user' table with the help of Glide Ajax and creating a script include. Since, getReference() is the least efficient function.

You could refer to the below link for more details on getReference

getReference() - best practice or not?

If you would like to use my approach you can use the scripts below:

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ga = new GlideAjax('Employee_Details');
    ga.addParam('sysparm_name', 'details');
    ga.addParam('sysparm_employee', newValue);
    ga.getXML(setDetails);
}

function setDetails(response) {

    var answer = response.responseXML.documentElement.getAttribute("answer");
    if (answer != 'false') {
        var requestor = answer.evalJSON();
        g_form.setValue('Line_Manager', requestor.manager);
        g_form.setValue('Job_Title', requestor.title);
        g_form.setValue('contact_number', requestor.phone);
        g_form.setValue('Department', requestor.department);
        g_form.setValue('site', requestor.location);
        g_form.setValue('Entity', requestor.u_entity);
        g_form.setValue('emp_number', requestor.employee_number);
        g_form.setValue('emp_status', requestor.active);
        g_form.setValue('Cost_Centre', requestor.cost_center);
    }
}

Script Include:

Name: Employee_Details

Client Callable: True(Check the checkbox)

var Employee_Details = Class.create();
Employee_Details.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    details: function() {
        var value = 'false';
        var gr = new GlideRecord('sys_user');
        if (gr.get(this.getParameter('sysparm_employee'))) {
            var object = {};
            object.manager = gr.manager;
            object.title = gr.title;
            object.phone = gr.phone;
            object.department = gr.department;
            object.location = gr.location;
            object.u_entity = gr.entity;
            object.employee_number = gr.employee_number;
            object.active = gr.active;
            object.cost_center = gr.getDisplayValue('cost_center');
            value = JSON.stringify(object);
        }
        return value;
    },
    type: 'Employee_Details'
});

Kindly mark my answer as Correct and Helpful based on the Impact.

Regards,

Alok