Previous Value in Mail Script

Mark_Didrikson
ServiceNow Employee
ServiceNow Employee

Hello,

Is it possible to output a previous field value in a mail script? Something like this:



<mail_script>
var pdd = previous.due_date.getDisplayValue();
template.print('previous due ' + pdd);
</mail_script>


Thanks!

Mark Didrikson

6 REPLIES 6

dressman
Kilo Expert

This code is mostly credited to Service-Now Guru (http://www.servicenowguru.com/scripting/business-rules-scripting/checking-modified-fields-script/)

Load your changed fields up in an array into the parm2 through the table BR after update such as:



if (current.operation() == 'update'){

//Collect all changed fields
if (typeof GlideScriptRecordUtil != 'undefined')
var gru = GlideScriptRecordUtil.get(current);
else
var gru = Packages.com.glide.script.GlideRecordUtil.get(current);
var fields = gru.getChangedFieldNames(); //Get changed fields with database names
//Convert to JavaScript Array
gs.include('j2js');
fields = j2js(fields);

//Make sure to remove the useless variable changes.
fields = String(fields).replace(/variables,/g, "").replace(/,variables/g, "")

//filter out the addtl comment changes, they have their own email.
if(fields != 'comments' &amp;&amp; fields != '')

gs.eventQueue('group.notify.update', current, itemSysID, fields);
}


then in your email you can use:



<mail_script>
//Process the changed fields
var fields = event.parm2.split(',');
for(x in fields){
//Get the field label, value, and type
var field = fields[x];
var fieldLabel = eval('current.' + field + '.getLabel()');
var fieldVal = eval('current.' + field+ '.getDisplayValue()');
var fieldType = eval('current.' + field + '.getED().getType()');
if(fieldType == -1){
//Print out last journal entry for journal fields
template.print('<b>Notes:</b> ' + eval('current.' + field + '.getJournalEntry(1)'));
}
else{
template.print('<b>' + fieldLabel + ':</b> ' + fieldVal + '\n');
}
}
</mail_script>


Hi,

I have similar kinda requirement. Actually i have a scheduled workflow where i'll collect all the requested items and provide summary of it including last changed service catalog variable value. So, instead of passing 'current', is there anyway that i can pass my requested item and acheive this?


rsanon
Tera Contributor

Thanks for sharing the modified script @dressman.

I did notice that on the email notification, we get two emails, one includes the updated/change fields, and the other is empty.

Both are trigger from the same email notification.

Any ideas why that may be?


The reason you could be getting the duplicates is that any sla's you have running will later update the sla_due field which will kick off the event again. I forgot to mention I have a condition on the business rule '!current.sla_due.changes()' that will prevent an event from being created if it is due to this.