Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

using the script include to access the incident table

instance
Tera Contributor

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?

 

 

1 ACCEPTED SOLUTION

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 : 

VishalBirajdar7_0-1676626061290.png

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

View solution in original post

8 REPLIES 8

Prasad Dhumal
Mega Sage

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.

Vishal Birajdar
Giga Sage

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

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

@VishalBirajd

1702a.jpg

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

VishalBirajdar7_0-1676609570101.png

 

 

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

}

}

 

VishalBirajdar7_1-1676609674062.png

 

Here is the Output I get :

 

VishalBirajdar7_2-1676609791905.png

 

 

 

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates