Script display sysid instead of the display value

Luis Roman1
Tera Contributor

I am fair new to service now and scripting. I have the following script and but the "reqdetails.department" display the sysid instead of the value?

find_real_file.png

Can you share your expert scripting advise or guidance to change it so that I display the value instead. Your collaboration is really appreciated. 

Luis 

function onLoad() {
//Type appropriate comment here, and begin script below
//g_form.setValue('requester_name',g_user.getFullName());
g_form.setValue('requested_for',g_user.getFullName());

var reqdetails = new GlideRecord('sys_user');
reqdetails.addQuery('sys_id',g_user.userID);
reqdetails.query();
while (reqdetails.next());
{
g_form.setValue('approving_manager',reqdetails.manager);
g_form.setValue('profile','Busines' + reqdetails.department + '\nEmail: '+ reqdetails.email + '\nPhone:' +reqdetails.phone);

}

}

1 ACCEPTED SOLUTION

Now that I'm back at a proper computer.... if you really need to do something client-side, you can use the getReference() method to get the full account record and use a callback to do something with it. I recommend against this approach for a couple of reasons.

1. It's slow and the user will notice the delay.

2. It returns the entire record, which will contain more data than you need and may contain data that should not be sent to the client.

 

g_form.getReference( 'account', getName );

function getName( gr ){ alert( gr.name ); }

View solution in original post

4 REPLIES 4

John Dahl
Tera Guru

Because the Department field is not a string piece of data, but a reference to another field. The sys_id is the key on which the reference is based.

To get the display value, try:

reqDetails.department.getDisplayValue()

 

One thing to consider.... Getting extended values (i.e. dot-walking) is not an efficient activity on the client. I suggest one of two things...

1. Convert this into a GlideAjax call to gather the data server-side and return all of the data that you need.

2. If you know you will always need it (it's an onload script so I assume always), then create a Display business rule to populate the value into the scratchpad.

John try your suggestion and did not work. Ex. 

g_form.setValue('profile','Busines' +  '\nEmail: '+ reqdetails.email + reqdetails.department.getDisplayValue() +'\nPhone:' +reqdetails.phone);

Now it does not display anything.  

Maybe I need to do a GlideAjax, however it seems to me that the GlideAJax just return one value. 

Thank you for feedback and suggestion. 

 

Luis (Fort Worth - Texas)

Ok, that was my mistake, but doesn't surprise me... as I said, I go to GlideAjax or the Scratchpad for gathering extended data attributes. The data that is available at the client is limited and any other attributes that are required must be retrieved from the database in separate requests. You're better off making a server-side call. You can provide the details to the client either pre-fetching with a business rule and delivered by the scratchpad or post-fetching and delivered in the GlideAjax response. 

You are correct that a GlideAjax call will only return one value ( the response), but that response can be a complex object. So you can get and object that looks like:

{

    "departmentName": "Some department",

    "departmentManager": "Abel Tuter"

}

 

then you can access the data by response.departmentName or response.departmentManager.

 

 

 

Now that I'm back at a proper computer.... if you really need to do something client-side, you can use the getReference() method to get the full account record and use a callback to do something with it. I recommend against this approach for a couple of reasons.

1. It's slow and the user will notice the delay.

2. It returns the entire record, which will contain more data than you need and may contain data that should not be sent to the client.

 

g_form.getReference( 'account', getName );

function getName( gr ){ alert( gr.name ); }