The CreatorCon Call for Content is officially open! Get started here.

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

Mike Patel
Tera Sage

.getDisplayValue() will not work on client script. You will have to use GlideAjax to do so. Use my article to create your SI and get all the fields you want.

https://community.servicenow.com/community?id=community_article&sys_id=67cdfcd3db4c985c2be0a851ca961...

Hitoshi Ozawa
Giga Sage
Giga Sage

Haven't tested this but something like below. 

Client Script:

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', _costcentrename(requestor.cost_center));
   }
}

function _costcentrename(costCenterId)
{
    var ajax = new GlideAjax('getCostCenterName');
    ajax.addParam('sysparm_name', 'getDisplayName');
    ajax.addParam('sysparm_id', costCenterId);
    ajax.getXMLAnswer(function(answer) {
            return answer;

    });
}

 

Script Include:

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'
});

Hi hozawa,

I have added the code and created a new script include, and I got these warnings & errors in console (also attached screenshot for better viewing)

Array(9) outputToConsole @ js_includes_sp.jsx?v=08-28-2020_1009&lp=Fri_Sep_04_00_43_46_PDT_2020&c=20_298:43877


Array(6) outputToConsole @ js_includes_sp.jsx?v=08-28-2020_1009&lp=Fri_Sep_04_00_43_46_PDT_2020&c=20_298:43877


getMessage (key="Attachment(s) are not added"): synchronous use not supported in Mobile or Service Portal unless message is already cached

getMessage (key="Please wait, attachment deletion in progress"): synchronous use not supported in Mobile or Service Portal unless message is already cached

getMessage (key="Remove {0} from {1}"): synchronous use not supported in Mobile or Service Portal unless message is already cached


Failed to load resource: the server responded with a status of 404 (Not Found)

Unhandled exception in GlideAjax.

Thx!

Sorry, forgot to add comment. Need to specify control centre table name to query.

var grCostCentre = new GlideRecord('<control centre table name>');