Email script to show SCTASK-level variables in notification

JGuerrero0323
Tera Expert

Hi ServiceNow Community,

I’m looking for some help or guidance on how to create an email script.

What I want to achieve is to include all the variables that are shown at the SCTASK level in the email — either as a summary or in a detailed format. Basically, I’d like to replicate what the requester sees in the catalog task, so that it’s also visible in the email.

Has anyone done something similar or could point me in the right direction to script this?

Thanks in advance!

1 ACCEPTED SOLUTION

@JGuerrero0323 

yes, you can create 1 email script and use in the sc_task notification level

Then use below script to show all variables of that catalog item

It will work for any catalog item

Note: you will have to enhance this for printing MRVS

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

    // Add your code here
    template.print('RITM Variables: <br/>');
    var ritmRecord = current.request_item.getRefRecord();
    var variables = ritmRecord.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 != '') {
            template.space(4);
            template.print('  ' + label + " = " + value + "<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  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

5 REPLIES 5

rahulp634206051
Tera Contributor

Hello,
You can use ${variables."Your Variable Name"} it will work if notification is built on same table. If not, you need to write an email script.
For script you can refer: https://www.servicenow.com/community/itsm-forum/i-want-to-display-all-catalog-variables-on-notificat...

 

 

rahulp634206051
Tera Contributor

Hello,
You can use ${variables.NameOfYourVariable} or you can write a email script as shared in this post.
https://www.servicenow.com/community/itsm-forum/i-want-to-display-all-catalog-variables-on-notificat...

Ankur Bawiskar
Tera Patron
Tera Patron

@JGuerrero0323 

you would know which all variables are made available on sc_task level

simply use the below script and show those variables in email script, notification on sc_task level

Use the variable labels you want to show in the array

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

    // Add your code here

    template.print('RITM Variables: <br/>');

    var ritmRecord = current.request_item.getRefRecord();
    var variablesToBeShown = ['variableLabel1', 'variableLabel2', 'variableLabel3'];

    var variables = ritmRecord.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 != '' && variablesToBeShown.indexOf(label) > -1) {
            template.space(4);
            template.print('  ' + label + " = " + value + "<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  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

Thanks for the script — let me expand a bit more on my use case...

Right now I have around 20 catalog items, and the idea is to show the details of each catalog item in the task-level email notification. Is that possible or feasible in some way? Something like showing a request details summary or similar, depending on the RITM the task belongs to?

Appreciate your input — thanks again!

Best regards,
Juan