Mail notification to show variable values if equals yes

Bidduam
Tera Guru

So I am creating a mail notification that is generated from a RITM, it has some standard text, some variable values and that is all working fine.

The part I'm having issues with is where our users have chosen yes or no to a list of questions - I want to show all the questions they have answered yes to. 

Please Note: This is not a standard yes no field, we have used multiple choice for formatting reasons, so it's not true false, it is yes or no as the answer.

 

I figured the best way would be a mail script, but I'm getting an error in the notification advising that:

"Email script render error: email script [ variables_yes ] does not exist"

 

If someone knows how I can do this I'd really appreciate the help please.

 

The email script I'm trying is like this:

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

    if (current.variables.question1 == 'yes') {
        template.print('*Are you going to attend?');
    }

    if (current.variables.question2 == 'yes') {
        template.print('*Is the sky blue?');
    }

    if (current.variables.question3 == 'yes') {
        template.print('*Do you like to swim?');
    }

    if (current.variables.question4 == 'yes') {
        template.print('*Are you doing something on the weekend?');
    }

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

 

1 ACCEPTED SOLUTION

Aman Kumar S
Kilo Patron

Hi @Bidduam 

Can you share the screenshot of your notification body?

How are you testing the notifications?

The format looks alright.

 

Best Regards
Aman Kumar

View solution in original post

13 REPLIES 13

Worked it out.

Top left of the "What will it contain" tab, I changed the context type from HTML Only, to HTML and plain text.

Bidduam_0-1707973716581.png

 

Turns out if I'd done that at the start I wouldn't have had the issue!

Thank you for your help @Aman Kumar S 

Amit Verma
Kilo Patron
Kilo Patron

Hi @Bidduam

 

It could be possible that the email notification is inserting html <span> tags into your mail script calls. Please follow below steps and check if span tag is the culprit :

 

1. Go into the notification that is generating the error and click the < > button on the editor to open the source code editor.

 

2. Look for the tag with your mail script name like ${mail_script_name}. It likely has span tags in it so it looks like this:   ${mail_script_name</span><span>}, which is causing the notification to be unable to find the mail script and run it.

 

3.Remove the span tags and retry.

 

Refer below posts for reference :

https://www.servicenow.com/community/developer-forum/email-script-render-error-email-script-live-fee...

https://www.servicenow.com/community/developer-forum/email-script-render-error-email-script-survey-u...

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

Amit Gujarathi
Giga Sage
Giga Sage

HI @Bidduam ,
I trust you are doing great.
Please find the updated version of your script :

(function runMailScript(current, template, email, email_action, event) {
    // Assuming 'variables' is a correctly set up variable set on the RITM form
    // and each question is a variable within that set.
    // It's also assumed that 'question1', 'question2', etc., are the internal names of the variables.

    var questions = [
        {variable: 'question1', text: 'Are you going to attend?'},
        {variable: 'question2', text: 'Is the sky blue?'},
        {variable: 'question3', text: 'Do you like to swim?'},
        {variable: 'question4', text: 'Are you doing something on the weekend?'}
    ];

    questions.forEach(function(question) {
        var answer = current.variables[question.variable];
        if (answer && answer.getDisplayValue() === 'yes') {
            template.print('*' + question.text + '\n');
        }
    });
})(current, template, email, email_action, event);

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



@Amit Gujarathi 

Unfortunately this doesn't show anything - no error, but no values or text either