- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2023 07:41 AM - edited 02-20-2023 04:21 AM
In the Catalog items i have a field name incident (
using the script inlcude what i select in the incident field --> that particular caller_ID should be seen in the callerid field.
but the caller_id of the incident is not coming. what is wrong with the script?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2023 01:28 AM
Script include :
var getUserUtils = Class.create();
getUserUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCaller: function() {
// Create Object to get Incident number and caller ID from incident
var users = [];
// get Incident sys Ids from client script
var incidentSysIDs = this.getParameter('sysparm_incidentSysIDs');
// glide record on incidnet table
var grINC = new GlideRecord('incident');
grINC.addEncodedQuery('sys_idIN' + incidentSysIDs); //query with incident sys id
grINC.query();
while (grINC.next()) {
var info ={
incident: '',
callerID: '',
// if you have a called field on catalog form as reference then use sysId
sysId:'',
};
//store values in users object
info.incident = grINC.getValue('number').toString();
info.callerID = grINC.getDisplayValue('caller_id').toString();
info.sysId = grINC.getValue('caller_id').toString();
users.push(info);
}
gs.log('getUserUtils:Information= ' + JSON.stringify(users));
return JSON.stringify(users);
},
Client Script :
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var incidents = g_form.getValue('incidents');
var gaINCUsers = new GlideAjax('getUserUtils'); // Glide Ajax
gaINCUsers.addParam('sysparm_incidentSysIDs', incidents); // variable values passed to script include
gaINCUsers.addParam('sysparm_name', 'getCaller'); // function name in script include
gaINCUsers.getXMLAnswer(callback);
function callback(answer) {
var results = JSON.parse(answer);
var storeValues =[];
for (var i = 0; i < results.length; i++) {
storeValues.push('Incident:' + results[i].incident + ' ' + 'Caller = ' + results[i].callerID );
}
g_form.setValue('fields', storeValues);
}
}
This will definitely work.
output :
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2023 07:49 AM
Caller ID is a reference field so instead of taking the Display Value, try the sys_id only.
Something like this:
grSysUser.getValue('caller_id')
Let us know the outcome.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2023 08:46 AM
Hello @instance
Can you please modify user script include little bit mentioned in bold text
script include:
var getIncident = Class.create();
getIncident.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getRecords:function()
{
var groupSysId = this.getParameter('sysparm_groupId');
//var obj = {}
var caller;
var grSysUser = new GlideRecord('incident');
grSysUser.addQuery('number', groupSysId);
grSysUser.query();
while (grSysUser.next()) {
caller = grSysUser.getValue('caller_id').toString();
}
//gs.addInfoMessage(grSysUser.user.toString());
return JSON.stringify(caller);
},
type: 'getIncident'
});
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2023 07:07 PM - edited 02-20-2023 04:22 AM
@VishalBirajd
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2023 09:01 PM
Hello @instance ,
Can you please try below Script include and Client script
You can change the variable names with your variable
I have created two Variables :
1.Variable Name : Incidents
Type : List Collector
Reference Table : incident
2.Variable Name : Fields
Type : Multi-line Text
Script Include :
var getUserUtils = Class.create();
getUserUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCaller: function() {
// Create Object to get Incident number and caller ID from incident
var users = {
incident: '',
callerID: '',
// if you have a called field on catalog form as reference then use sysId
sysId:'',
};
// get Incident sys Ids from client script
var incidentSysIDs = this.getParameter('sysparm_incidentSysIDs');
// glide record on incidnet table
var grINC = new GlideRecord('incident');
grINC.addEncodedQuery('sys_idIN' + incidentSysIDs); //query with incident sys id
grINC.query();
while (grINC.next()) {
//store values in users object
users.incident = grINC.getValue('number').toString();
users.callerID = grINC.getDisplayValue('caller_id').toString();
users.sysId = grINC.getValue('caller_id').toString();
}
//gs.log('GetUser' + JSON.stringify(users));
return JSON.stringify(users);
},
Client Script :
On Change of Variable - 'incidents'
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var incidents = g_form.getValue('incidents');
var gaINCUsers = new GlideAjax('getUserUtils'); // Glide Ajax
gaINCUsers.addParam('sysparm_incidentSysIDs', incidents); // variable values passed to script include
gaINCUsers.addParam('sysparm_name', 'getCaller'); // function name in script include
gaINCUsers.getXMLAnswer(callback);
function callback(answer) {
var results = JSON.parse(answer);
// set the values in field
g_form.setValue('fields','Incident= ' + results.incident + ' ' + 'Caller = ' + results.callerID);
//if you have another variable as caller id and is reference variable
//g_form.setValue('caller_name',results.sysId);
}
}
Here is the Output I get :
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates