Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Flow Designer Custom Flow Action Script

Manny11
Tera Expert

I created a Flow Designer Custom Action with the goal of comparing two glidedatetimes and determining if 50% of the time had been exceed. 

 

When I test with the My script keeps returning false no matter what dates input.

(function execute(inputs, outputs) {

//inputs
var start_date = new GlideDateTime(inputs.start_date);
var end_date = new GlideDateTime(inputs.end_date);

//get current date time
var now = new GlideDateTime();

//Calculate total duration between start_date and end_date
var totalDuration = GlideDateTime.subtract(end_date,start_date).getNumericValue();

//Calculate elapsed time between start_date and current time
var elapsedTime = GlideDateTime.subtract(now,start_date).getNumericalValue();

//Check if 50% of time has passed
if(elapsedTime >= totalDuration / 2){

    gs.info(elapsedTime)
   
    outputs.duration_exceeded ='true';
  
} else{
 gs.info(elapsedTime)
   outputs.duration_exceeded = 'false';
 
}
})(inputs, outputs);
1 ACCEPTED SOLUTION

Juhi Poddar
Kilo Patron
Kilo Patron

Hello @Manny11 

 

The script you've provided has a few small issues, but it can be easily fixed. Below are some key changes to improve the functionality:

  1. There are inconsistencies in the method names. It should be getNumericValue() instead of getNumericalValue().
  2. GlideDateTime.subtract() returns a GlideDuration object, and it should be used properly for numerical comparison.

Here's an updated version:

 

(function execute(inputs, outputs) {

// Inputs
var start_date = inputs.start_date; // Assuming this is a string in "yyyy-MM-dd HH:mm:ss" format
var end_date = inputs.end_date; // Same format as above

// Calculate the time difference in seconds
var timeDifferenceInSeconds = gs.dateDiff(start_date, end_date, true);

// Calculate if 50% of the time has elapsed
var currentDate = new GlideDateTime();
var currentDateStr = currentDate.toString(); // Convert to string for gs.dateDiff

var elapsedSeconds = gs.dateDiff(start_date, currentDateStr, true);
if (elapsedSeconds >= timeDifferenceInSeconds / 2) {
    outputs.duration_exceeded = 'true';
} else {
    outputs.duration_exceeded = 'false';
}

})(inputs, outputs);

 

I hope this helps resolve the issues with your script.

 

If you found my answer helpful, please give it a like and mark it as the accepted solution. It helps others find the solution more easily and supports the community!

 

Thank You

Juhi Poddar

 

View solution in original post

2 REPLIES 2

Manny11
Tera Expert

The flow trigger is going to check requested Item Records every hour

 

Juhi Poddar
Kilo Patron
Kilo Patron

Hello @Manny11 

 

The script you've provided has a few small issues, but it can be easily fixed. Below are some key changes to improve the functionality:

  1. There are inconsistencies in the method names. It should be getNumericValue() instead of getNumericalValue().
  2. GlideDateTime.subtract() returns a GlideDuration object, and it should be used properly for numerical comparison.

Here's an updated version:

 

(function execute(inputs, outputs) {

// Inputs
var start_date = inputs.start_date; // Assuming this is a string in "yyyy-MM-dd HH:mm:ss" format
var end_date = inputs.end_date; // Same format as above

// Calculate the time difference in seconds
var timeDifferenceInSeconds = gs.dateDiff(start_date, end_date, true);

// Calculate if 50% of the time has elapsed
var currentDate = new GlideDateTime();
var currentDateStr = currentDate.toString(); // Convert to string for gs.dateDiff

var elapsedSeconds = gs.dateDiff(start_date, currentDateStr, true);
if (elapsedSeconds >= timeDifferenceInSeconds / 2) {
    outputs.duration_exceeded = 'true';
} else {
    outputs.duration_exceeded = 'false';
}

})(inputs, outputs);

 

I hope this helps resolve the issues with your script.

 

If you found my answer helpful, please give it a like and mark it as the accepted solution. It helps others find the solution more easily and supports the community!

 

Thank You

Juhi Poddar