- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2020 10:12 AM
Hello all,
I am trying to convert this client script into a Glide Ajax call. This is located on this incident table and the incident form is taking a long time to load when updating the fields. I think a Glide Ajax call to a Script Include might help.
Here is the Client Script below.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading)
return;
//make work notes mandatory as appropriate
if(newValue == oldValue)
g_form.setMandatory('work_notes', false);
else
g_form.setMandatory('work_notes', true);
//clear dependent values
g_form.setValue('assigned_to', null);
g_form.setValue('u_field3', null);
g_form.setValue('u_field2', null);
g_form.setValue('u_field1', null);
//repopulate dependent values
var group = g_form.getReference('assignment_group', setQueue);
}
function setQueue(group) {
if (group)
{
g_form.setValue('u_field1', group.u_field1);
g_form.setValue('u_field2', group.u_field2);
g_form.setValue('u_field3', group.u_field3);
}
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2020 10:35 AM
Hi Maxwell,
That's correct using GlideAjax is better than using getReference with callback in terms of performance
Script Include: it should be client callable
var checkRecords = Class.create();
checkRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkRecordPresent: function(){
var obj = {};
var group = this.getParameter('sysparm_group');
var groupRec = new GlideRecord('sys_user_group');
groupRec.addQuery('sys_id', group);
groupRec.query();
if(groupRec.next()){
obj['u_field1'] = groupRec.u_field1;
obj['u_field2'] = groupRec.u_field2;
obj['u_field3'] = groupRec.u_field3;
}
return JSON.stringify(obj);
},
type: 'checkRecords'
});
Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading)
return;
//make work notes mandatory as appropriate
if(newValue == oldValue)
g_form.setMandatory('work_notes', false);
else
g_form.setMandatory('work_notes', true);
//clear dependent values
g_form.setValue('assigned_to', null);
g_form.setValue('u_field3', null);
g_form.setValue('u_field2', null);
g_form.setValue('u_field1', null);
var ga = new GlideAjax('checkRecords');
ga.addParam('sysparm_name', "checkRecordPresent");
ga.addParam('sysparm_group', g_form.getValue('assignment_group'));
ga.getXMLAnswer(function(answer){
if(answer != ''){
var answerObj = JSON.parse(answer);
g_form.setValue('u_field1', answerObj.u_field1);
g_form.setValue('u_field2', answerObj.u_field2);
g_form.setValue('u_field3', answerObj.u_field3);
}
});
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2020 05:10 PM
Thank you Ankur for your reply.
The script you provided came very close to a solution but unfortunately, it did not produce any values. In any other scenarios, I am sure it would've worked. Just an FYI, I had to make some adjustments. Below is the updated script, please let me know if this is ServiceNow best practice. I had to just keep tweaking it until it worked.
getRef: function() {
var group = this.getParameter('sysparm_group');
var groupRec = new GlideRecord('sys_user_group');
groupRec.addQuery('sys_id', group);
groupRec.query();
if (groupRec.next()) {
var obj = {};
//obj['u_field1'] = groupRec.u_field1;
//obj['u_field2'] = groupRec.u_field2;
//obj['u_field3'] = groupRec.u_field3;
//Updated script below
obj.u_field1 = groupRec.getDisplayValue('u_field1');
obj.u_field2 = groupRec.getDisplayValue('u_field2');
obj.u_field3 = groupRec.getDisplayValue('u_field3');
}
return JSON.stringify(obj);
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2020 11:02 PM
Hi Maxwell,
Script looks fine
small change ; declare the obj variable outside the scope of if statement
getRef: function() {
var obj = {};
var group = this.getParameter('sysparm_group');
var groupRec = new GlideRecord('sys_user_group');
groupRec.addQuery('sys_id', group);
groupRec.query();
if (groupRec.next()) {
//obj['u_field1'] = groupRec.u_field1;
//obj['u_field2'] = groupRec.u_field2;
//obj['u_field3'] = groupRec.u_field3;
//Updated script below
obj.u_field1 = groupRec.getDisplayValue('u_field1');
obj.u_field2 = groupRec.getDisplayValue('u_field2');
obj.u_field3 = groupRec.getDisplayValue('u_field3');
}
return JSON.stringify(obj);
},
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2020 05:48 PM
Be sure to return the display value of the reference field, otherwise your code is not much better than getReference().
If you don't, ServiceNow automatically makes a synchronous GlideAjax call back to the server to get the display value.
If you are lazy, download my share app that is the same as getReference but returns a display value
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2020 06:23 PM
Hello Paul,
Thank you for providing this link. It makes sense, I updated the script as follows, but I still get the same result.
g_form.setValue('u_field1', answerObj.u_field1, answerObj.getDisplayValue('u_field1'));
g_form.setValue('u_field2', answerObj.u_field2, answerObj.getDisplayValue('u_field2'));
g_form.setValue('u_field3', answerObj.u_field3, answerObj.getDisplayValue('u_field3'));

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2020 01:47 AM
Could be an ACL issue, that the user does not have read access to that record or field.
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022