How to Get Reference field values

Pratik Parikh
Tera Contributor

name  & approver Variable are  Reference field from sys_user.

how can I get  All Department head name as approver for choice onChange client Script?

 

find_real_file.png

Here I teste GlideAjax but i get only one Approver but i can not get other Department Head.

 

var Print_Approver = Class.create();
Print_Approver.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    ApproverAjax: function() {
        var grUser = new GlideRecord('sys_user');
        var userSysID = this.getParameter('sysparm_Test1');
        grUser.addQuery('sys_id', userSysID);
        grUser.query();

        
        while (grUser.next()) {
        
            return grUser.department.dept_head.getValue('sys_id');

        }

    },
    type: 'Print_Approver'
});

 

 

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

    var ga = new GlideAjax('Print_approver');
    ga.addParam('sysparm_name', 'ApproverAjax');
    ga.addParam('sysparm_Test1', g_form.getValue('name'));
    ga.getXML(TestCallBack);

    function TestCallBack(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        g_form.setValue('approver',answer);
    }

}

 

1 ACCEPTED SOLUTION

Mohith Devatte
Tera Sage
Tera Sage

hello @Pratik Parikh ,

you want to return the head of the department for the selected name in the name field right ?

is that your requirement ?

if yes what you have done is correct .

but can you explain in detail about this below statement?

but i get only one Approver but i can not get other Department Head.

 

View solution in original post

5 REPLIES 5

Mohith Devatte
Tera Sage
Tera Sage

hello @Pratik Parikh ,

you want to return the head of the department for the selected name in the name field right ?

is that your requirement ?

if yes what you have done is correct .

but can you explain in detail about this below statement?

but i get only one Approver but i can not get other Department Head.

 

I need the names of all department heads in the Approver field. (Like name field nut only Dept_head)

 

find_real_file.pngfind_real_file.png

@Pratik Parikh ,

then try this you need to create an array and push all sys_ids and then return that array as our are expecting multiple values

var Print_Approver = Class.create();
Print_Approver.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    ApproverAjax: function() {
var arr=[];
    var userSysID = this.getParameter('sysparm_Test1');
        var grUser = new GlideRecord('sys_user');
        grUser.addQuery('sys_id', userSysID);
        grUser.query();
      if (grUser.next()) {
       var gr = new GlideRecord('sys_user');
gr.addQuery('department.dept_head',grUser.department.dept_head);
gr.query();
while(gr.next())
{        
          arr.push( gr.department.dept_head.toString());
}

        }
return "sys_idIN"+arr;

    },
    type: 'Print_Approver'
});

MARK THIS ANSWER CORRECT IF IT HELPS YOU

 

Maik Skoddow
Tera Patron
Tera Patron

Hi

the reason for the current behavior is the following code. Here you already return after the first loop:

while (grUser.next()) {
  return grUser.department.dept_head.getValue('sys_id');'
}

Instead you have to collect all Sys IDs and return after the looping.

Instead write:

var arrSysIDs = [];

while (grUser.next()) {

  arrSysIDs.push(grUser.department.dept_head.getValue('sys_id'));
}

return arrSysIDs.join(',');

 

On client side you have to split the returned string to get all Sys IDs as single values.

Maik