Create a Scripted Rest API to fetch RITM Variables
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
Requirement is to "Create a Scripted Rest API to fetch RITM Variables"
API should accept RITM number or sys_id as input.
API should fetch all associated catalog variables and values.
response should include : 1. variable name 2. variable label 3. variable value.
To implement this we have used multiple scripts, but when in case of variable type as reference, select box or list collector it is fetching the sys_id instead of display value.
First script :
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var ritmNumber = request.queryParams.ritm_number;
var ritmSysId = request.queryParams.ritm_sys_id;
// Validate input
if (!ritmNumber && !ritmSysId) {
response.setStatus(400);
response.setBody({
error: "Missing required parameter: ritm_number or ritm_sys_id"
});
return;
}
// Retrieve the RITM record
var ritmGR = new GlideRecord('sc_req_item');
if (ritmSysId) {
ritmGR.addQuery('sys_id', ritmSysId);
ritmGR.query();
if (!ritmGR.next()) {
response.setStatus(404);
response.setBody({
error: "RITM not found for sys_id: " + ritmSysId
});
return;
}
} else if (ritmNumber) {
ritmGR.addQuery('number', ritmNumber);
ritmGR.query();
if (!ritmGR.next()) {
response.setStatus(404);
response.setBody({
error: "RITM not found for number: " + ritmNumber
});
return;
}
}
// Fetch ONLY variables
var varList = [];
var grVar = new GlideRecord('sc_item_option_mtom');
grVar.addQuery('request_item', ritmGR.sys_id);
grVar.query();
while (grVar.next()) {
varList.push({
name: grVar.sc_item_option.item_option_new.name.toString(),
label: grVar.sc_item_option.item_option_new.question_text.toString(),
value: grVar.sc_item_option.value.getDisplayValue()
});
}
response.setStatus(200);
if (ritmSysId) {
response.setBody({
ritm_sys_id: ritmGR.getUniqueValue(),
variables: varList
});
} else if (ritmNumber) {
response.setBody({
ritm_number: ritmGR.getValue('number'),
variables: varList
});
}
})(request, response);
-------------------------------------------------------------------------------------------------------
Second script :
-------------------------------------------------------------------
First script is returning sys_id instead of display value completely in case of reference fields or list collector.
Second script is returning display value in some cases, and failing in some cases of reference fields and list collector as well.
Please help identify, what changes are required in the script to fetch display value instead of sys_id.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
check this link
SN Pro Tips — Scripted REST APIs & Retrieving RITM Variables via SRAPI
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
But the example script is returning sys_id for requested_for field in the article you mentioned.
we need display value of it.
please check screenshot
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
enhance the logic to get the display value
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
this line returning sys _id
displayValue = refGR.getDisplayValue();
Instead of it mention field name
displayValue = refGR.getDisplayValue('number'); //display value of required field
refer: How to use getDisplayValue() and getValue() for GlideRecord or GlideElement
