- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2025 08:16 AM
I'm working on a scripted REST API and I am looking to query incidents closed yesterday and return specific fields.
I can achieve this by listing all fields out in the code however I am looking to make use of a sys_property to store the fields and use these to return the fields from the query.
My code is below, can this be amended to make use of a sys property?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2025 09:49 AM
Steps
1) create system property of type string and add below fields as comma separated value
number,opened_at,resolved_at,parent,parent_incident,short_description,description,correlation_display,caller_id,contact_type,priority,state,category,assignment_group,assigned_to,sys_updated_on,sys_updated_by
2) update your scripted REST API script as this
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var results = [];
var fieldsProp = gs.getProperty('api.fields');
if (!fieldsProp) {
response.setStatus(500);
response.setBody({
error: 'Property x_yournamespace.incident_fields not found'
});
return;
}
var fields = [];
var fieldsSplit = fieldsProp.split(',');
for (var i = 0; i < fieldsSplit.length; i++) {
fields.push(fieldsSplit[i].trim());
}
var grIncident = new GlideRecordSecure('incident');
grIncident.addEncodedQuery('closed_atONYesterday@javascript:gs.beginningOfYesterday()@javascript:gs.endOfYesterday()');
grIncident.query();
while (grIncident.next()) {
var recordObj = {};
for (var j = 0; j < fields.length; j++) {
var field = fields[j];
var displayVal = grIncident.getDisplayValue(field);
var valueVal = grIncident.getValue(field);
recordObj[field] = (displayVal !== valueVal) ? displayVal : valueVal;
}
results.push(recordObj);
}
response.setBody(results);
})(request, response);
If my response helped please mark it correct and close the thread so that it benefits future readers.
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-26-2025 09:20 AM
Here's one way you can do that. I believe you would need two system properties to differentiate between your intended getValue and getDisplayValue. In this simplified example I created 2 system properties, retrieving 2 fields each:
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var results = [];
var resultsObj = {};
var gv = gs.getProperty('bkb.inc.gv').split(',');
var gdv = gs.getProperty('bkb.inc.gdv').split(',');
var grIncident = new GlideRecordSecure('incident');
grIncident.addEncodedQuery('
closed_atONYesterday@javascript:gs.beginningOfYesterday()@javascript:gs.endOfYesterday()
');
grIncident.query();
while (grIncident.next()) {
for (var i=0; i<gv.length; i++) {
resultsObj[gv[i]] = grIncident.getValue(gv[i]);
}
for (var j=0; j<gdv.length; j++) {
resultsObj[gdv[j]] = grIncident.getDisplayValue(gdv[j]);
}
results.push(JSON.stringify(resultsObj));
}
return results;
})(request, response);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2025 09:49 AM
Steps
1) create system property of type string and add below fields as comma separated value
number,opened_at,resolved_at,parent,parent_incident,short_description,description,correlation_display,caller_id,contact_type,priority,state,category,assignment_group,assigned_to,sys_updated_on,sys_updated_by
2) update your scripted REST API script as this
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var results = [];
var fieldsProp = gs.getProperty('api.fields');
if (!fieldsProp) {
response.setStatus(500);
response.setBody({
error: 'Property x_yournamespace.incident_fields not found'
});
return;
}
var fields = [];
var fieldsSplit = fieldsProp.split(',');
for (var i = 0; i < fieldsSplit.length; i++) {
fields.push(fieldsSplit[i].trim());
}
var grIncident = new GlideRecordSecure('incident');
grIncident.addEncodedQuery('closed_atONYesterday@javascript:gs.beginningOfYesterday()@javascript:gs.endOfYesterday()');
grIncident.query();
while (grIncident.next()) {
var recordObj = {};
for (var j = 0; j < fields.length; j++) {
var field = fields[j];
var displayVal = grIncident.getDisplayValue(field);
var valueVal = grIncident.getValue(field);
recordObj[field] = (displayVal !== valueVal) ? displayVal : valueVal;
}
results.push(recordObj);
}
response.setBody(results);
})(request, response);
If my response helped please mark it correct and close the thread so that it benefits future readers.
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-27-2025 07:54 AM
Hope you are doing good.
Did my reply answer your question?
If my response helped please mark it correct and close the thread so that it benefits future readers.
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-27-2025 09:07 AM
Thank you for marking my response as helpful.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader