- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2025 08:14 PM
I'm quite new to JavaScript and ServiceNow scripting, so I’d really appreciate some guidance.
I've created a Business Rule on the sc_req_item table that runs after insert, and it only triggers when a specific catalog item is requested. In the Advanced tab, I’ve written a script that exports variables from the RITM into a CSV file and attaches it to the record. Everything works for Variable01 through to Variable11. They export correctly and appear in the CSV as expected.
However, I'm running into an issue with the checkbox variables: "CheckBox1", "CheckBox2", and "CheckBox3". In the CSV, they currently appear as "TRUE" or "FALSE" but due to a business requirement, they need to be displayed as "YES" or "NO" in the csv. export.
Please advise on the best way to convert these checkbox values in the CSV export? Thank you in advance.
Here's the code I have written currently (sensitive data removed for safety purposes)
(function() {
// Get current RITM (Requested Item) sys_id from the current record
var ritmSysId = current.sys_id;
// Load variable pool for the RITM
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(ritmSysId);
set.load();
// Get all variables in a flat list (including those in variable sets)
var vs = set.getFlatQuestions();
// Define the desired variable labels in the specific order they should appear in the CSV
var desiredLabels = [
"Variable01", "Variable02", "Variable03", "Variable04",
"Variable05", "Variable06", "Variable07",
"Variable08", "Variable09", "Variable10", "Variable11",
"CheckBox1", "CheckBox2", "Checkbox3"
];
// Create a map (dictionary) to store label → display value pairs
var labelValueMap = {};
for (var i = 0; i < vs.size(); i++) {
var question = vs.get(i);
var label = question.getLabel();
var displayValue = question.getDisplayValue(); // default for most fields
// Normalize checkbox fields (which return "true"/"false" as strings)
if (["CheckBox1", "CheckBox2", "Checkbox3"].indexOf(label) !== -1) {
var rawValue = question.getValue();
getValue = (rawValue === 'true') ? 'Yes' : (rawValue === 'false') ? 'No' : 'Unknown';
}
labelValueMap[label] = displayValue;
}
// Prepare arrays to hold CSV headers and corresponding values
var headers = [];
var values = [];
// Loop through the desired labels in order and build the CSV rows
for (var j = 0; j < desiredLabels.length; j++) {
var lbl = desiredLabels[j];
headers.push(lbl); // Add the label to the header row
values.push(labelValueMap[lbl] || ""); // Add the value if found, else empty string
}
// Combine headers and values into a single CSV-formatted string
var csvContent = headers.join(",") + "\n" + values.join(",");
// Attach the generated CSV file to the current RITM record
var attachment = new GlideSysAttachment();
var fileName = "Export.csv";
attachment.write(current, fileName, "text/csv", csvContent);
})();
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2025 12:20 AM
Hi everyone,
Thank you all for your replies - my superior ended up figuring out the problem.
Everything has remained the same except for this snippet, in between:
var displayValue; // Declare displayValue here, but don't initialize yet
&
// Store the determined displayValue in the map
labelValueMap[label] = displayValue;
Here is the following code:
//var displayValue = question.getDisplayValue(); // default for most fields
var rawValue = question.getValue();
// Normalize checkbox values for specific labels
if (label == "Checkbox1" || label == "Checkbox2" || label == "Checkbox3") {
// Use rawValue for booleans — it's more reliable for checkboxes
if(rawValue == 'true'){
displayValue = 'Yes';
}else if(rawValue == 'false'){
displayValue = 'No';
}else{
displayValue = question.getDisplayValue();
}
}else{
displayValue = question.getDisplayValue();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2025 12:20 AM
Hi everyone,
Thank you all for your replies - my superior ended up figuring out the problem.
Everything has remained the same except for this snippet, in between:
var displayValue; // Declare displayValue here, but don't initialize yet
&
// Store the determined displayValue in the map
labelValueMap[label] = displayValue;
Here is the following code:
//var displayValue = question.getDisplayValue(); // default for most fields
var rawValue = question.getValue();
// Normalize checkbox values for specific labels
if (label == "Checkbox1" || label == "Checkbox2" || label == "Checkbox3") {
// Use rawValue for booleans — it's more reliable for checkboxes
if(rawValue == 'true'){
displayValue = 'Yes';
}else if(rawValue == 'false'){
displayValue = 'No';
}else{
displayValue = question.getDisplayValue();
}
}else{
displayValue = question.getDisplayValue();
}