List Collector variables in REST message

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2024 01:40 AM - edited 01-09-2024 02:05 AM
Hi,
I am struggling with a script supposed to send a REST message regarding List Collector variables from a Requested Item. Here is the script:
// Define variables
var variableMappings = {
'environments': {
table: 'question_choice',
referenceQualifier: 'question=50cfee5d976a999078dcb3e3f153afb2'
},
'landing_zone_owners': 'sys_user', // List Collector, table: sys_user
'user_group_owners': 'sys_user', // List Collector, table: sys_user
};
// Loop through variables and add them to payload if they are filled
for (var variableName in variableMappings) {
if (variableMappings.hasOwnProperty(variableName)) {
var variableValue = current.variables[variableName];
// Check if the variable is filled before adding it to payload
if (variableValue != null && variableValue != '' && (variableName === 'landing_zone_owners' || variableName === 'user_group_owners' || variableName === 'environments')) {
if (variableMappings[variableName]) {
// Translate sys_id to display value for List Collector variables
if (variableValue instanceof Array) {
var displayValues = [];
for (var i = 0; i < variableValue.length; i++) {
var referenceRecord = new GlideRecord(variableMappings[variableName]);
referenceRecord.addQuery('sys_id', variableValue[i]);
referenceRecord.query();
while (referenceRecord.next()) {
displayValues.push(referenceRecord.getDisplayValue());
}
}
variableValue = displayValues.join(', ');
} else if (typeof variableMappings[variableName] === 'object') {
// For List Collector with referenceQualifier
if (variableValue instanceof Array) {
var displayValues = [];
for (var j = 0; j < variableValue.length; j++) {
var referenceRecord = new GlideRecord(variableMappings[variableName].table);
if (variableMappings[variableName].referenceQualifier) {
referenceRecord.addEncodedQuery(variableMappings[variableName].referenceQualifier);
}
referenceRecord.addQuery('sys_id', variableValue[j]);
referenceRecord.query();
if (referenceRecord.next()) {
displayValues.push(referenceRecord.getDisplayValue());
}
}
variableValue = displayValues.join(', ');
} else {
var referenceRecord = new GlideRecord(variableMappings[variableName].table);
if (variableMappings[variableName].referenceQualifier) {
referenceRecord.addEncodedQuery(variableMappings[variableName].referenceQualifier);
}
referenceRecord.addQuery('sys_id', variableValue);
referenceRecord.query();
if (referenceRecord.next()) {
variableValue = referenceRecord.getDisplayValue();
}
}
}
}
payload[variableName] = variableValue.toString();
logEntries.push('Variable: ' + variableName + ', Value: ' + variableValue);
}
}
}
break;
When only one choice is selected in each of the defined List Collector variables, I get the correct name in the payload, but if more than one is selected, the payload contains sys_ids separated by commas instead of the names. Do you know how to fix that?
Best regards
Thomas
- Labels:
-
Request Management

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2024 02:02 AM - edited 01-09-2024 05:38 AM
Sorry for posting in the Virtual Agent Forum. I haven't noticed that until now but can't seem to move it to another forum.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2024 06:30 AM
Hi,
Try below:
(function executeRule(current, previous /*null when async*/) {
// Check if the current record is a Requested Item
if (current.getValue('cat_item')) {
// Get the List Collector variable from the Requested Item
var listCollectorVariable = current.variables.list_collector_variable_name;
// Check if the List Collector variable has a value
if (listCollectorVariable) {
// Create a payload for the REST message
var payload = {
"requestedItem": current.getValue('cat_item'),
"listCollectorValue": listCollectorVariable.toString()
};
// Send the REST message
sendRestMessage(payload);
}
}
})(current, previous);
function sendRestMessage(payload) {
// Set up REST message parameters
var restMessage = new sn_ws.RESTMessageV2();
restMessage.setHttpMethod("POST");
restMessage.setEndpoint("your_rest_endpoint_url"); // Replace with your REST endpoint URL
restMessage.setRequestHeader("Content-Type", "application/json");
// Set the request payload
restMessage.setRequestBody(JSON.stringify(payload));
// Send the REST message
var response = restMessage.execute();
var responseBody = response.getBody();
// Log the response
gs.info("REST Message Response: " + responseBody);
}
Regards,
Shoheb