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

@Vishal Birajdar its working fine .

Update the Client Script As below :

 

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);
var storeValues;
for(var int i ;i<results.length ; i++){
storeValues = storeValues + 'Incident= ' + results[i].incident + ' ' + 'Caller = ' + results[i].callerID
}
g_form.setValue('fields',storeValues);

}

}

 

Vishal Birajdar
ServiceNow Developer

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

@Vishal Birajdar  i have Tried But it is displaying undefined only

 

 

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