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

So I am trying to take your advice and create a Custom Action (this could be useful in the future for other work that we do too).  But I am having issues getting it to work.  There seems to be something wrong with my script.  Perhaps you can spot if I have made some glaring mistake.

 

I am feeding in the table name and sys id of the record as my parameters, and looking get an output of number of days (integer value).

Here is my code:

 

(function execute(inputs, outputs) {

    var tbl = inputs.snow_table_name;
    var sid = inputs.rec_sys_id;
    var creDate = new GlideDateTime();

    //do Glide Record to get created date
    var gr = new GlideRecord(tbl);
    gr.addQuery('sys_id',sid);
    gr.query();

    with(gr.next()){
        //get created date
        creDate=gr.sys_created_on;
    }

    //calculate difference between created date and current date
    var curDate = new GlideDateTime();
    var age = GlideDateTime.subtract(creDate, curDate);

    outputs.rec_age = age.getDayPart();

})(inputs, outputs);

 

 

And here is the error message I get when I test it.

 

Error: Can't find method com.glide.glideobject.GlideDateTime.subtract(java.lang.String,com.glide.glideobject.GlideDateTime). (Process Automation.1102859f1b62c2148decc9da234bcb91; line 19)

 

 

Do you see any obvious mistakes I may have made in my code?

 

Thanks

 

Edit: By the way, I did run the glide record in a background script and confirm that it is finding a record and pulling back a date!

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.