Date Calculations in Flow Designer for record lookup

Jim Wright
Kilo Contributor

I am having some issues getting date calculations and lookups to work as expected in Flow Designer.

I have a requirement to empty a users shopping cart (basket) if they have not checked out within a given number of days. I can do this quite simply in flow designer, having a daily flow which checks for records in the sc_cart_item table where last update was x days ago, and if any were found then delete them.

However, for flexibility and ease, it was suggested that we have a property that defines the number of days after which the cart should be emptied, and use this in the flow.

I have created an action which looks up the property to find the number of days, converts this into milliseconds, and then deducts that from the current time (see script below).

(function execute(inputs, outputs) {

var cartEmpty = gs.getProperty("basket.empty.time");//get property for number of days

var nowGDT = new GlideDateTime();//get now

var cartTime = (cartEmpty*86400000);//convert to milliseconds

nowGDT.add(-cartTime);

outputs.empty = nowGDT;

})(inputs, outputs);

When I test this script from the action, it works fine, calculating exactly the date expected.

However, when I try to use this in a flow, using the date output from the custom action, and looking for records that were updated before the calculated date to then delete the records in the sc_cart_item table, it does not work.

The dates seem to be being passed correctly, but it does not detect the items that it should from the lookup, and therefore does not remove them.

I have tried doing lookups using created date and updated date, and used before, at or before, relative and all do not provide the desired outcome.

Any ideas?

1 ACCEPTED SOLUTION

Hi,

Can you try calling the system property in the filter condition itself using javascript:

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

10 REPLIES 10

Strange.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

I agree 🙂

Hi,

Can you try calling the system property in the filter condition itself using javascript:

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

I have added a script to the conditions instead of using the custom action and pill from that action. This seems to work as desired now.

Script added was as below:

var daysAgo = gs.getProperty('basket.empty.time');
return "sys_updated_onRELATIVELT@dayofweek@ago@" + daysAgo
 
Thanks for your help with getting me in the right direction as well.

@Jim Wright 

Glad to help.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader