Scripted Timeout duration in Flow not working "An error occurred while parsing the duration value"

HarI98
Tera Expert

The goal:

Calculate the difference in time between Users end date (Date Field) and today.
Take that difference and put it as a timeout duration in the Flow action (Submit catalog item request).

With that Timeout enabled the request will either complete before the timeout expires or it will timeout and do certain things.

 

The error (after flow runs and executes that part):

"An error occurred while parsing the duration value"

 

The script attempts I tried and gave the same result:

var endD = fd_data.subflow_inputs.user_record.getValue("u_end_date");
//also tried the above line but instead of using getValue I just dot walk directly to u_end_date

var endDate = new GlideDateTime(endD);
var today = new GlideDateTime();

var difference = GlideDateTime.subtract(today, endDate);
var duration = new GlideDuration(difference);

return duration.getDurationValue();

 

This script will work in background scripts but not in the flow:

var userId = fd_data.subflow_inputs.user_record.sys_id;
var gr = new GlideRecord("sys_user");
gr.get(userId);

var endD = gr.getValue("u_end_date");

var endDate = new GlideDateTime(endD);
var today = new GlideDateTime();

var difference = GlideDateTime.subtract(today, endDate);
var duration = new GlideDuration(difference);

return duration.getDurationValue();

 

If someone managed to point out what am I doing wrong here, it would be amazing as I am out of ideas at the moment.

 

Thanks in advance!

 

1 ACCEPTED SOLUTION

Okay finally found the solution. This took me far too long than it should have but I also blame ServiceNow a bit for inconsistent behavior. Hope whoever needs a similar solution finds this and saves their precious time.

 

I created a Flow variable (string type) and at the beginning of the flow used a "Set Flow variables" action.

In this created variable I turned on the scripting and pasted this below script into it.

 

 

//Collect end date from User record data pill
var endD = fd_data.subflow_inputs.user_record.u_end_date;

//Instantiate it as a GlideDateTime format
var endDate = new GlideDateTime(endD);

//Get todays date in the same format
var today = new GlideDateTime();

//Collect the difference in time between these 2 dates in miliseconds
var difference = GlideDateTime.subtract(today, endDate);

//Feed the miliseconds into the GlideDuration object
var duration = new GlideDuration(difference);

//return the results
return duration;

 

 

 

Then in the "Submit Catalog Item Request" action when I checked the "Wait for Completion" and "Enable timeout" boxes, for the Duration field I set the above variable as the data pill value for this field, and it worked.

 

Hope whoever needs a similar solution finds this and saves their precious time.

View solution in original post

3 REPLIES 3

HarI98
Tera Expert

I think part of the issue is that the Duration field in Flow does not have amount for days, only hours, minutes and second. Where as for example in task_sla table there is also for days. So when end day is more than 24 hours thats when it breaks, as I noticed it works for when end day is less than 24 hours it managed to set the value in there.

 

So now I will try converting the day into hours and setting that value.

Hardcoding a value for example "45:22:14" did not work for the flow duration field, but did for the normal duration field where there is a day field included.

Any way of making this day field shown in the Flows enable timeout duration field?

Okay finally found the solution. This took me far too long than it should have but I also blame ServiceNow a bit for inconsistent behavior. Hope whoever needs a similar solution finds this and saves their precious time.

 

I created a Flow variable (string type) and at the beginning of the flow used a "Set Flow variables" action.

In this created variable I turned on the scripting and pasted this below script into it.

 

 

//Collect end date from User record data pill
var endD = fd_data.subflow_inputs.user_record.u_end_date;

//Instantiate it as a GlideDateTime format
var endDate = new GlideDateTime(endD);

//Get todays date in the same format
var today = new GlideDateTime();

//Collect the difference in time between these 2 dates in miliseconds
var difference = GlideDateTime.subtract(today, endDate);

//Feed the miliseconds into the GlideDuration object
var duration = new GlideDuration(difference);

//return the results
return duration;

 

 

 

Then in the "Submit Catalog Item Request" action when I checked the "Wait for Completion" and "Enable timeout" boxes, for the Duration field I set the above variable as the data pill value for this field, and it worked.

 

Hope whoever needs a similar solution finds this and saves their precious time.