We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

Email Script to display all populated Variables from the [sc_request]

Scott29
Kilo Sage

I have an email script I use for RITM and SC_TASK (below). How can I run this on an email notification targeting the [sc_request] table? assuming the request only has 1 RITM.

 

 

 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {

    template.print('<span style="font-size: 14pt; font-weight: 600">Request Details</span><br/><br/>');
    var variables = current.variables.getElements();
    for (var i = 0; i < variables.length; i++) {
        var question = variables[i].getQuestion();
        var label = question.getLabel();
        var value = question.getDisplayValue()

        if(value!= '' && value != 'false'){
            template.space(4);
            template.print(label + ': <span style="font-weight: 600">' + value + '</span><br/>');
        }
    }
})(current, template, email, email_action, event);
3 ACCEPTED SOLUTIONS

Medi C
Giga Sage

@Scott29 

If your email script is being used on a notification which is on sc_request table, you would need to get the related requested item in order to get the variables.

 

Please use and adjust the following as per your requirements:

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {
    var grRequest = new GlideRecord("sc_request");
    if (grRequest.get(grRequest.sys_id)) {
        var grReqItem = new GlideRecord("sc_req_item");
        grReqItem.addQuery("request=" + grRequest.sys_id);
        grReqItem.query();
        if (grReqItem.next()) {
            template.print('<span style="font-size: 14pt; font-weight: 600">Request Details</span><br/><br/>');
            var variables = grReqItem.variables.getElements();
            for (var i = 0; i < variables.length; i++) {
                var question = variables[i].getQuestion();
                var label = question.getLabel();
                var value = question.getDisplayValue();
                if (value != '' && value != 'false') {
                    template.space(4);
                    template.print(label + ': <span style="font-weight: 600">' + value + '</span><br/>');
                }
            }
        }
    }
})(current, template, email, email_action, event);

Thanks & Best regards,
Medi

View solution in original post

vermaamit16
Kilo Patron

Hi @Scott29 

 

Try with below email script. Ensure that your notification is running on sc_request table.

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    // Add your code here
    template.print("Summary of Requested items:<br />");
    var item = new GlideRecord("sc_req_item");
    item.addQuery("request", current.sys_id);
    item.query();
    while (item.next()) {
        template.print(item.number + ":  " + item.quantity + " X " + item.cat_item.getDisplayValue() + " at " + item.cat_item.price.getDisplayValue() + " each <br />");
        template.print("    Options:<br />");
        var keys = [];
        var set = new GlideappVariablePoolQuestionSet();
        set.setRequestID(item.sys_id);
        set.load();
        var vs = set.getFlatQuestions();
        for (var i = 0; i < vs.size(); i++) {
            if (vs.get(i).getLabel() != '') {
                template.space(4);
                template.print('     ' + vs.get(i).getLabel() + " = " + vs.get(i).getDisplayValue() + "<br />");
            }
        }
    }
})(current, template, email, email_action, event);

 

Thanks and Regards

Amit Verma

Thanks and Regards
Amit Verma

View solution in original post

Ankur Bawiskar
Tera Patron

@Scott29 

update as this

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {
    var ritmRec = new GlideRecord("sc_req_item");
    if (ritmRec.get('request', current.sys_id)) {
            template.print('<span style="font-size: 14pt; font-weight: 600">Request Details</span><br/><br/>');
            var variables = ritmRec.variables.getElements();
            for (var i = 0; i < variables.length; i++) {
                var question = variables[i].getQuestion();
                var label = question.getLabel();
                var value = question.getDisplayValue();
                if (value != '' && value != 'false') {
                    template.space(4);
                    template.print(label + ': <span style="font-weight: 600">' + value + '</span><br/>');
                }
            }
    }
})(current, template, email, email_action, event);

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

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

View solution in original post

3 REPLIES 3

Medi C
Giga Sage

@Scott29 

If your email script is being used on a notification which is on sc_request table, you would need to get the related requested item in order to get the variables.

 

Please use and adjust the following as per your requirements:

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {
    var grRequest = new GlideRecord("sc_request");
    if (grRequest.get(grRequest.sys_id)) {
        var grReqItem = new GlideRecord("sc_req_item");
        grReqItem.addQuery("request=" + grRequest.sys_id);
        grReqItem.query();
        if (grReqItem.next()) {
            template.print('<span style="font-size: 14pt; font-weight: 600">Request Details</span><br/><br/>');
            var variables = grReqItem.variables.getElements();
            for (var i = 0; i < variables.length; i++) {
                var question = variables[i].getQuestion();
                var label = question.getLabel();
                var value = question.getDisplayValue();
                if (value != '' && value != 'false') {
                    template.space(4);
                    template.print(label + ': <span style="font-weight: 600">' + value + '</span><br/>');
                }
            }
        }
    }
})(current, template, email, email_action, event);

Thanks & Best regards,
Medi

vermaamit16
Kilo Patron

Hi @Scott29 

 

Try with below email script. Ensure that your notification is running on sc_request table.

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    // Add your code here
    template.print("Summary of Requested items:<br />");
    var item = new GlideRecord("sc_req_item");
    item.addQuery("request", current.sys_id);
    item.query();
    while (item.next()) {
        template.print(item.number + ":  " + item.quantity + " X " + item.cat_item.getDisplayValue() + " at " + item.cat_item.price.getDisplayValue() + " each <br />");
        template.print("    Options:<br />");
        var keys = [];
        var set = new GlideappVariablePoolQuestionSet();
        set.setRequestID(item.sys_id);
        set.load();
        var vs = set.getFlatQuestions();
        for (var i = 0; i < vs.size(); i++) {
            if (vs.get(i).getLabel() != '') {
                template.space(4);
                template.print('     ' + vs.get(i).getLabel() + " = " + vs.get(i).getDisplayValue() + "<br />");
            }
        }
    }
})(current, template, email, email_action, event);

 

Thanks and Regards

Amit Verma

Thanks and Regards
Amit Verma

Ankur Bawiskar
Tera Patron

@Scott29 

update as this

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {
    var ritmRec = new GlideRecord("sc_req_item");
    if (ritmRec.get('request', current.sys_id)) {
            template.print('<span style="font-size: 14pt; font-weight: 600">Request Details</span><br/><br/>');
            var variables = ritmRec.variables.getElements();
            for (var i = 0; i < variables.length; i++) {
                var question = variables[i].getQuestion();
                var label = question.getLabel();
                var value = question.getDisplayValue();
                if (value != '' && value != 'false') {
                    template.space(4);
                    template.print(label + ': <span style="font-weight: 600">' + value + '</span><br/>');
                }
            }
    }
})(current, template, email, email_action, event);

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

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