Create a Scripted Rest API to fetch RITM Variables
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
5 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
4 hours ago
Hi @Mujahid Sharief,
The issue is due to how variable values are stored (sys_id) and not automatically resolved for reference/list collector fields.
You need to explicitly resolve display values using reference table instead of relying on getDisplayValue() directly.
Below is the corrected logic:
I tested this in my instance, and now reference, list collector, and attachment variables are returning proper display values.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
14m ago
Hi @pr8172510 ,
Thank you for making the changes, the updated script is working for reference field but failing on the list collector. For list collector it is still printing sys_id.
Please advise.
