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.

Getting error while testing the flow designer

Prithvi Ramesh1
Mega Sage

PrithviRamesh1_0-1752233227154.png

PrithviRamesh1_1-1752233249885.png

Script written for the flow variable -

 

var startDT = fd_data._1__look_up_records.variables.start_date;
var startDateTime = new GlideDateTime(startDT);
var nowDateTime = new GlideDateTime();
var diffMillis = startDateTime.getNumericValue() - nowDateTime.getNumericValue();

// Check if current time is within one hour before start time
if (diffMillis > 0 && diffMillis <= 3600000) {
    return true;
}

 

3 REPLIES 3

Ehab Pilloor
Mega Sage

Hi @Prithvi Ramesh1,

 

You need to use Action number 3 instead of Action 1 in your Flow script.

You need to access variable from each record at a time instead of all records.

 

var startDT = fd_data.<action_3_name>.variables.start_date;
var startDateTime = new GlideDateTime(startDT);
var nowDateTime = new GlideDateTime();
var diffMillis = startDateTime.getNumericValue() - nowDateTime.getNumericValue();

// Check if current time is within one hour before start time
if (diffMillis > 0 && diffMillis <= 3600000) {
    return true;
}

 

Regards,

Ehab Pilloor

Shraddha Kadam
Giga Sage

Hello @Prithvi Ramesh1 ,

 

var startDT_raw = fd_data._1__look_up_records.variables.start_date;
gs.info("Flow Script - Raw start_date from data pill: " + startDT_raw);
gs.info("Flow Script - Type of start_date: " + typeof startDT_raw);

var startDateTime;
startDateTime = new GlideDateTime(startDT_raw);
if (!startDateTime.isValid()) {
    gs.error("Flow Script - ERROR: 'start_date' value could not be converted to a valid GlideDateTime object. Value: " + startDT_raw);
 //   return false; // Stop the flow/script due to invalid input
}

gs.info("Flow Script - startDateTime (display value): " + startDateTime.getDisplayValue());
gs.info("Flow Script - startDateTime (numeric value): " + startDateTime.getNumericValue());

var nowDateTime = new GlideDateTime(); // Get current server time (UTC internally)
gs.info("Flow Script - nowDateTime (display value): " + nowDateTime.getDisplayValue());
gs.info("Flow Script - nowDateTime (numeric value): " + nowDateTime.getNumericValue());

// Calculate the difference in milliseconds
var diffMillis = startDateTime.getNumericValue() - nowDateTime.getNumericValue();
gs.info("Flow Script - Difference in milliseconds (start - now): " + diffMillis);

// Define the one-hour threshold in milliseconds (1 hour = 3600 seconds * 1000 ms/second)
var oneHourInMillis = 3600 * 1000;
gs.info("Flow Script - One hour in milliseconds: " + oneHourInMillis);

if (diffMillis > 0 && diffMillis <= oneHourInMillis) {
    gs.info("Flow Script - Condition MET: Current time is within one hour before start time.");
   // return true;
} else {
    gs.info("Flow Script - Condition NOT MET.");
   // return false;
}

 

Check above script and see if it is working.

If my response was helpful, please mark it as correct and helpful.
Thank you.

Hello @Prithvi Ramesh1 ,

Did my answer resolve your question ?

 

If my response was helpful, please mark it as correct and helpful.
Thank you.