The Zurich release has arrived! Interested in new features and functionalities? Click here for more

email script for Changes to a field.

Walter Toney
Tera Expert

I have a requirement to send a notification. in this notification will be alot of information so im building a email script for one part of this notification.

 

requirement - If "Description" changes i want to see what it was prior and after the change.

 

so i built a email script to put in the notification but its not working as i think it should. can someone assist

 

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

 

if (previous.description != current.description ) {

template.print ( "CTASK Description was changed from: " + previous.description);

template.print ( "CTASK description was changed to: " + current.description);

}

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

 

I would like to keep this as a script can someone see what i'm missing or doing wrong to get the data I desire.

1 ACCEPTED SOLUTION

ashishdevsingh
Tera Expert

Hi @Walter Toney ,

 

In email Script, did you modify the function to get previous value ? because I don't see if you can use previous value like that. ServiceNow can be used like: 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {
          // Add your code here
 
})(current, template, email, email_action, event);
 
To Achieve your requirement, I would suggest you:
1. to create a After Business rule, where you can execute the Event using
gs.eventQueue("event_name", current, previous.short_description, ""); // you can user parm2 also if you need it.
2. then on event execution, you can trigger the notification. You can see the option to select the event in notification "when to send" section.
3. Write email script: and get the value using event.parm1. // here you will get previous value. 
4. use this script in your notification by using ${mail_script:your_script_name}
 
Please let me know if you have any query. I have tried and it works. Please mark if it helps you. 
PS: attaching screenshot for your reference. 

View solution in original post

3 REPLIES 3

SanjivMeher
Kilo Patron
Kilo Patron

I dont think previous is accessible in email script.

If you want to send current and previous, you need to create an onAfter Business Rule.

In the onAfter Business Rule, trigger an event and pass the old description as one of the event parameters. 

The event should be linked to a notification.

Then you can access that parameter in the notification itself. You will not have to create a email script also.


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

Saurabh Gupta
Kilo Patron
Kilo Patron

Hi @Walter Toney 
The object "previous" is not available in mail scripts.
You need to write a onAfter BR and fire an event with old and new description to have in the notification.
You can refer the below post to achieve the same.
Solved: How to get previous value in Email Notification? - ServiceNow Community

 

C

If the provided solution meets your needs, kindly consider marking it as helpful and accepting it as the solution. This helps others who may have similar questions.

 


Thanks and Regards,

Saurabh Gupta

ashishdevsingh
Tera Expert

Hi @Walter Toney ,

 

In email Script, did you modify the function to get previous value ? because I don't see if you can use previous value like that. ServiceNow can be used like: 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {
          // Add your code here
 
})(current, template, email, email_action, event);
 
To Achieve your requirement, I would suggest you:
1. to create a After Business rule, where you can execute the Event using
gs.eventQueue("event_name", current, previous.short_description, ""); // you can user parm2 also if you need it.
2. then on event execution, you can trigger the notification. You can see the option to select the event in notification "when to send" section.
3. Write email script: and get the value using event.parm1. // here you will get previous value. 
4. use this script in your notification by using ${mail_script:your_script_name}
 
Please let me know if you have any query. I have tried and it works. Please mark if it helps you. 
PS: attaching screenshot for your reference.