How to fetch Catalog task variables in email script for resquested item notification

RajC59005800307
Tera Guru

Below script helped me to achieve this

 

(function runMailScript(current, template, email, email_action, event) {

    // On sc_req_item, 'current' IS the Requested Item.

    // We don't need a GlideRecord lookup for 'item'.

   

    var closeNote = current.close_notes ? current.close_notes.getDisplayValue() : "";

   

    var set = new GlideappVariablePoolQuestionSet();

    set.setRequestID(current.sys_id); // Pass the RITM SysID here

    set.load();

   

    var vs = set.getFlatQuestions();

   

    if (vs.size() > 0) {

        template.print("<hr style='width: 98%;' /><p><b><u>Task Details</u></b></p>");

       

        // Only print Close Notes if they aren't empty

        if (closeNote) {

            template.print("<p><b>Close Notes</b>: " + closeNote + "</p>");

        }

 

        for (var i = 0; i < vs.size(); i++) {

            var variable = vs.get(i);

            var label = variable.getLabel();

            var displayValue = variable.getDisplayValue();

 

            // Logic: Don't print empty variables or 'false' (empty checkboxes)

            if (label && displayValue && displayValue != "" && displayValue != "false") {

                template.print("<p><b>" + label + "</b>: " + displayValue + "</p>");

            }

        }

    }

})(current, template, email, email_action, event);

Kindly mark it correct and helpful if it is applicable.

Thanks,

Raj
4 REPLIES 4

ChallaR
Giga Guru

Hi @RajC59005800307 ,

 

Please find the below updated script -

NOTE -

Use GlideappVariablePoolQuestionSet to fetch catalog variables in email notifications.
  • Notification on Requested Item (sc_req_item) → use current.sys_id
  •  Notification on Catalog Task (sc_task) → use current.request_item.sys_id
(function runMailScript(current, template, email, email_action, event) {

    // current is sc_task
    // Get the RITM reference
    var ritmId = current.request_item ? current.request_item.toString() : "";
    if (!ritmId) {
        template.print("<p><b>No Requested Item found for this task.</b></p>");
        return;
    }

    var set = new GlideappVariablePoolQuestionSet();
    set.setRequestID(ritmId);
    set.load();

    var vs = set.getFlatQuestions();

    if (vs.size() > 0) {
        template.print("<hr style='width:98%;'/>");
        template.print("<p><b><u>Task / Request Variables</u></b></p>");

        for (var i = 0; i < vs.size(); i++) {
            var variable = vs.get(i);
            var label = variable.getLabel();
            var displayValue = variable.getDisplayValue();

            if (label && displayValue && displayValue != "" && displayValue != "false") {
                template.print("<p><b>" + label + "</b>: " + displayValue + "</p>");
            }
        }
    } else {
        template.print("<p><i>No variables found.</i></p>");
    }

})(current, template, email, email_action, event);​

 

If this solution is helpful please accept it and close the thread. 

Thanks,

Rithika.ch

 

Ankur Bawiskar
Tera Patron

@RajC59005800307 

use this to fetch variables and then enhance your email script

(function runMailScript(current, template, email, email_action, event) {

	// Add your code here

	template.print('RITM Variables: <br/>');
	var ritm = current.request_item.getRefRecord();
	var variables = ritm.variables.getElements(); 
	for (var i=0;i<variables.length;i++) { 
		var question = variables[i].getQuestion();
		var label = question.getLabel();
		var value = question.getDisplayValue();
		if(label != '' && value != ''){
			template.space(4);
			template.print('  ' + label + " = " + value + "<br/>");
		}
	} 

})(current, template, email, email_action, event);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@RajC59005800307 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Its_Sagnic
Giga Guru

Hi @RajC59005800307 ,

Please use the code below to fetch the variable from catalog item and use in your email script.

(function runMailScript(current, template, email, email_action, event) {

    // Get RITM SysID (Handles both sc_task and sc_req_item contexts)
    var ritmId = current.request_item ? current.request_item.toString() : current.sys_id.toString();
    
    if (!ritmId) return; // Exit silently if no record found

    // Initialize Variable Set
    var set = new GlideappVariablePoolQuestionSet();
    set.setRequestID(ritmId);
    set.load();

    var vs = set.getFlatQuestions();

    //Process and Print Variables
    if (vs.size() > 0) {
        template.print("<div style='font-family: Arial; font-size: 12pt;'>");
        template.print("<hr style='border: 0; border-top: 1px solid #ccc; width: 100%;' />");
        template.print("<p style='margin-bottom: 10px;'><b><u>Request Details</u></b></p>");

        for (var i = 0; i < vs.size(); i++) {
            var variable = vs.get(i);
            var label = variable.getLabel();
            var displayValue = variable.getDisplayValue();

            // OPTIMIZED LOGIC: Filter out noise (Empty, False, and default "None" values)
            if (label && displayValue && displayValue != "" && displayValue != "false" && displayValue != "-- None --") {
                template.print("<div style='margin-bottom: 5px;'><b>" + label + "</b>: " + displayValue + "</div>");
            }
        }
        template.print("</div>");
    }

})(current, template, email, email_action, event);


Please use the code pasted above .. It may help you to solve the problem.

If you find it helpful for you please mark it as helpful and accept the solution to close the thread.

Regards,

Sagnic