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.

Create Catalog Task on Selecting Variable

Dileep2
Tera Contributor

Hi All,

 

I have to create a Catalog task for the Catalog item if the variable "Temporary" is selected, I have created Flow designer for the Catalog item and the Catalog task should create before or less than 14 days (<=14 days) on basis of Temporary date variable selection please suggest what to write script in the Flow Designer. Please check the screenshot attached.

 

Thank you.

2 REPLIES 2

G Ponsekar
Tera Guru

Hi @Dileep2 ,

 

  1. Add a 'Get Catalog Variables' action at the beginning of your flow to retrieve the variable values.
  2. Add an 'If' flow logic block with a condition that checks both requirements. You can do this with a single condition using the Formula builder. 
 
The formula for the 'If' condition
Here is the formula you will use in the 'If' block. This formula performs two checks:
  • Check 1: Is the "Temporary" variable selected? (Assuming it's a checkbox or a choice list that can be set to "true" or a similar value).
  • Check 2: Is the date difference between the "Temporary" date and today less than or equal to 14 days?
Assuming your "Temporary" variable is a Boolean (True/False) or a choice list and the date variable is named temporary_date:
 
javascript
// Check if "Temporary" variable is true AND the temporary date is within 14 days
var temporarySelected = fd_data.trigger.current.variables.temporary == 'true';
var temporaryDate = new GlideDateTime(fd_data.trigger.current.variables.temporary_date);
var today = new GlideDateTime();var diff = GlideDateTime.subtract(today, temporaryDate); // returns GlideDuration var daysDiff = diff.getNumericValue() / (1000 * 60 * 60 * 24); // Convert ms to days // Perform the check for temporary selection AND date <= 14 days
(temporarySelected && daysDiff <= 14)
Where:
  • fd_data.trigger.current.variables.temporary: The data pill for your Temporary variable.
  • fd_data.trigger.current.variables.temporary_date: The data pill for your Temporary Date variable.
  • Explanation: The script first checks if the Temporary variable is true. If so, it calculates the date difference in milliseconds between the temporary_date and today's date using GlideDateTime.subtract(). It then converts the difference to days and checks if it's less than or equal to 14.

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

Thanks, GP

 

 

 

Ajay_Chavan
Kilo Sage

@G Ponsekar  please follow below steps, i have tested it in my PDI its working

 

Flow :

Screenshot 2025-09-08 at 21.47.57.png

 

Create a custom action: "Calculate the date difference"

 

Screenshot 2025-09-08 at 21.48.36.png

 

Action script:

 

(function execute(inputs, outputs) {
var tempDate = inputs.temporary_date;
if (tempDate) {
    var temporaryDateTime = new GlideDateTime(tempDate);
    var currentDateTime = new GlideDateTime();
    
    // Calculate difference in days
    var diff = GlideDateTime.subtract(temporaryDateTime, currentDateTime);
    var daysDiff = Math.abs(diff.getNumericValue() / (1000 * 60 * 60 * 24));
    
    outputs.days_difference = daysDiff;
    outputs.within_14_days = daysDiff <= 14;
} else {
    outputs.within_14_days = false;
}
})(inputs, outputs);

 

Make sure you create input variables as - temporary_date and output variables as - days_difference and within_14_days

 

 

Glad I could help! If this solved your issue, please mark it as Helpful and Accept as Solution so others can benefit too.*****Chavan A.P. | Technical Architect | Certified Professional*****