Flow Designer: Return the Age of a Record in a Send Email Action

jmiskey
Kilo Sage

I am sending an email via Flow Designer.  I need to include on the email the number of days old the record is.

The logic/math behind it is subtract the "Created" date on the record from the current date (dynamic), and return the integer portion of that (drop the decimals so only left with whole days).

 

I am not sure if that can be done right in the email itself, or whether I should do it in a Flow Variable, and include that Flow Variable on the email.  But the bigger issue is that I know that working with date math in ServiceNow can be a big pain, and was wondering what is the easiest way of doing that calculation in my Flow.

 

Thanks

1 ACCEPTED SOLUTION

I was making my Custom Action more complex than it needed to be - I don't need a GlideRecord, as I can just pull the Created On date right from the Data Pill.  So I simplified the script of my Custom Action to this:

(function execute(inputs, outputs) {

    var sdt = inputs.st_dte;
    var cdt = new GlideDateTime();

    //calculate difference between start date and current date
    var age = GlideDateTime.subtract(sdt, cdt);

    outputs.rec_age = age.getDayPart();

})(inputs, outputs);
and it works perfectly!  I just input the date, and it tells me how many days have passed between then and now.

View solution in original post

6 REPLIES 6

Kieran Anson
Kilo Patron

Hey,

If you're using the 'Send Notification' action which triggers an email notification record, you can achieve your requirement using an email script. 

 

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

    var startDateField = 'sys_created_on';
    if (current.instanceOf('task'))
        startDateField = 'opened_at';

    if (!current.isValidField(startDateField))
        return;

    if (current.getElement(startDateField).nil())
        return;

	var now = new GlideDateTime();
	var startDateGDT = new GlideDateTime(current.getValue(startDateField));
	var duration = GlideDateTime.subtract(startDateGDT , now);

	template.print(duration.getDayPart()); //will output the number of days (not rounded)
	

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

No, I am using the "Send Email" action, as mentioned in my title.

Which is why I was asking how to calculate it in the Flow (or Send Email action) itself.

You'd have to create a custom action to calculate the date/time difference (similar to the script I provided). 

 

P.s the reason for the above is that the 'send notification' is a preference when it comes to troubleshooting emails. It means all email content is housed in one central table, rather than in separate locations.

P.s the reason for the above is that the 'send notification' is a preference when it comes to troubleshooting emails. It means all email content is housed in one central table, rather than in separate locations.

Perhaps I am not understanding something.  If i do it this way, there seems to be three different things needed:

1. Flow

2. Email Notification

3. Mail Script

Whereas if I do it in the Flow in a Send Email action, all I need is 1 thing, the Flow.  That later option seems more "contained" than the first, but perhaps I am not fully understanding what you mean.

 

A few complicating factors are that I also need the following (which I have been able to figure out how to do in Flow, but not in a Notification).

1.  One of the recipients of the email is NOT in the base table, but in a related table.  I do not seem to see any scripting options for email recipients in Notifications, nor does it appear that I can drill down fields in the main table to get to related-table fields.

2.  I need a create a link to the custom table record in the email body.  We already had created a Custom Action for building the link in Flow Designer for something else, so we can re-use that.  I suppose there probably is a way to do it in the Notification item too, but I am just not sure what that looks like.