How add variable from email script to email notification

String
Kilo Sage

Hi Team ,

Am trying to add variable from email script to email notification dynamically :

 

Below is my code for email script :name:Test Inc

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,

    /* Optional EmailOutbound */

    email, /* Optional GlideRecord */ email_action,

    /* Optional GlideRecord */

    event) {

    var number = current.incident;

    var getInc = new GlideRecord('incident');

    getInc.addQuery("sys_id", number);

      getInc.query();

  if (getInc.next()) {

gs.info('enter in email script');

        email.addVariable('Inc Number', 'true');

} else {

     email.addVariable('Inc Number', 'false');

}

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

 

Below is my notifications code (message HTML):

State: ${state}

Priority: ${priority}

Inc Number : ${mail_script:Test Inc}

 

I can see the log enter in email script .

 

am not getting the Inc Number value in notifications 

State: open

Priority: 2.high

Inc Number : 

Please guide me the best practices .

 

3 ACCEPTED SOLUTIONS

Kristen Ankeny
Kilo Sage

Rather than email.addVariable, you can just do template.print(). When you reference the mail script it will print what you told it to do in the mail script.

View solution in original post

Sainath N
Mega Sage
Mega Sage

@String : "template.print" is used to print the information from the email script to the notification. In this case, replace your [email.addVariable('Inc Number', 'true')] statement with [template.print(number)] which prints the incident number.

 

If it's still not working, please update your email script name to Test_Inc in both the email script and in your notification. The system treats Test Inc. as different words and might not work as expected.

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

View solution in original post

Aniket Chavan
Tera Sage
Tera Sage

Hello @Kristen Ankeny ,

Please give a try to the modified script below and let me know how it works for you.

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

    var number = current.incident;

    var getInc = new GlideRecord('incident');
    getInc.addQuery("sys_id", number);
    getInc.query();

    if (getInc.next()) {
        gs.info('Entered in email script');
        // Use template.print() to directly print the value in the email body
        template.print(getInc.number);
    } else {
        // Handle the case where the incident record is not found
        gs.info('Incident record not found');
        template.print('N/A');
    }

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

 

And in your email notification template:

 

State: ${state}
Priority: ${priority}
Inc Number: ${mail_script:Test Inc}

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket

View solution in original post

3 REPLIES 3

Kristen Ankeny
Kilo Sage

Rather than email.addVariable, you can just do template.print(). When you reference the mail script it will print what you told it to do in the mail script.

Sainath N
Mega Sage
Mega Sage

@String : "template.print" is used to print the information from the email script to the notification. In this case, replace your [email.addVariable('Inc Number', 'true')] statement with [template.print(number)] which prints the incident number.

 

If it's still not working, please update your email script name to Test_Inc in both the email script and in your notification. The system treats Test Inc. as different words and might not work as expected.

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Aniket Chavan
Tera Sage
Tera Sage

Hello @Kristen Ankeny ,

Please give a try to the modified script below and let me know how it works for you.

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

    var number = current.incident;

    var getInc = new GlideRecord('incident');
    getInc.addQuery("sys_id", number);
    getInc.query();

    if (getInc.next()) {
        gs.info('Entered in email script');
        // Use template.print() to directly print the value in the email body
        template.print(getInc.number);
    } else {
        // Handle the case where the incident record is not found
        gs.info('Incident record not found');
        template.print('N/A');
    }

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

 

And in your email notification template:

 

State: ${state}
Priority: ${priority}
Inc Number: ${mail_script:Test Inc}

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket