How Change CheckBox Display Value from TRUE/FALSE to YES/NO

JayAdmin_16
Mega Sage

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);
})();

 

1 ACCEPTED SOLUTION

JayAdmin_16
Mega Sage

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(); 
		}




View solution in original post

10 REPLIES 10

@JayAdmin_16 

did you print in logs?

I updated these lines, please check

(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();

        //REPLACED WITH CORRECT VALUES
        if (["CheckBox1", "CheckBox2", "CheckBox3"].indexOf(label) !== -1) {
            var rawValue = displayValue;
            var getValue = (rawValue === 'true') ? 'Yes' : (rawValue === 'false') ? 'No' : 'Unknown';
            gs.info('Inside variable' + label + ' value is' + getValue);
            labelValueMap[label] = getValue;
        } else {
            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);
})();

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@JayAdmin_16 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@JayAdmin_16 

I believe I also answered the question.

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

YaswanthKurre
Tera Guru

Hi @JayAdmin_16 ,

 

The logic now explicitly sets displayValue = 'YES' or displayValue = 'NO' for checkboxes

 

(function() {
    var ritmSysId = current.sys_id;
    var set = new GlideappVariablePoolQuestionSet();
    set.setRequestID(ritmSysId);
    set.load();
    var vs = set.getFlatQuestions();

    var desiredLabels = [
        "Variable01", "Variable02", "Variable03", "Variable04",
        "Variable05", "Variable06", "Variable07",
        "Variable08", "Variable09", "Variable10", "Variable11",
        "CheckBox1", "CheckBox2", "Checkbox3"
    ];

    var labelValueMap = {};
    for (var i = 0; i < vs.size(); i++) {
        var question = vs.get(i);
        var label = question.getLabel();
        var displayValue; // Declare displayValue here, but don't initialize yet

        // Determine the value based on the variable type/label
        if (["CheckBox1", "CheckBox2", "Checkbox3"].indexOf(label) !== -1) {
            // This is a checkbox variable
            var rawValue = question.getValue(); // Will be 'true' or 'false' (string)
            if (rawValue === 'true') {
                displayValue = 'YES';
            } else if (rawValue === 'false') {
                displayValue = 'NO';
            } else {
                // Handle cases where the checkbox might not have a value (e.g., if not checked/set)
                // or if it's an unexpected state. 'No' or '' is a common default.
                displayValue = 'NO'; // Default to 'NO' if not 'true'
            }
        } else {
            // For all other variables, use their default display value
            displayValue = question.getDisplayValue();
        }

        // Store the determined displayValue in the map
        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
        // Use labelValueMap[lbl] or default to an empty string if not found
        values.push(labelValueMap[lbl] || ""); 
    }

    // 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);
})();

 

Mark this as helpful and correct if this solves your question.

 

Thanks,

Yaswanth

RohitGeorgV
Mega Guru

@JayAdmin_16

if (["CheckBox1", "CheckBox2", "CheckBox3"].indexOf(label) !== -1) {
    var rawValue = question.getValue();
    displayValue = (rawValue === 'true') ? 'YES' : (rawValue === 'false') ? 'NO' : 'Unknown';
}

labelValueMap[label] = displayValue;

in your current code, make the variable naming change and try.