Want to create an Incident on first Working Day of the Month using flow designer with flow variable.

mishrarakes
Tera Contributor

I got a task to create an incident on the first working day of the month using flow designer with flow variable , I already have done it using schedule jobs, can some one help me, I am adding script which I have included in the flow variable toggle script section post that creating an incident and trigerring it on Monthly 1 st day at 8 : am`.

```

( function execute(){
var now = new GlideDateTime();


var firstDay = new GlideDateTime();
firstDay.setDayOfMonthLocalTime(1);

// 6 == saturday, 7 == sunday  ,1 == monday
while(firstDay.getDayOfWeek() == 6 || firstDay.getDayOfWeek() == 7){
   
    firstDay.addDaysUTC(1);
}
firstWorkingDayy = firstDay.getValue();
console.log(firstWorkingDayy);
return firstWorkingDayy;
})();
```
firstWorkinDayy flow variable with type Date/Time 
 
problem is incident is being created on any day whenever i test the flow.
2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@mishrarakes 

you can run flow on Daily at 8am then check if it's today is first Working day or not

1) create a flow variable of type string

2) use script to get check if today is 1st working day of current month (Monday to Friday), I considered Monday as Start of week and Saturday and Sunday as holidays

 var gdt = new GlideDateTime(); // Get the current date and time
    var dayOfMonth = gdt.getDayOfMonth(); // Retrieve the day of the month (1-31)
    var dayOfWeek = gdt.getDayOfWeek(); // Retrieve the day of the week (1=Sunday, 7=Saturday)

    // Check if today is a working day (Monday to Friday)
    if (dayOfWeek >= 2 && dayOfWeek <= 6) { // Monday (2) to Friday (6)
        // If it's within the first 7 days of the month
        if (dayOfMonth == 1) {
            gs.info("Today is the first working day of the month.");
            return 'true';
        } else {
            // Check if all previous days are non-working days
            var previousDay = new GlideDateTime(gdt);
            for (var i = dayOfMonth - 1; i > 0; i--) {
                previousDay.addDaysUTC(-1); // Go back one day
                var prevDayOfWeek = previousDay.getDayOfWeek();
                if (prevDayOfWeek >= 2 && prevDayOfWeek <= 6) {
                    gs.info("Today is NOT the first working day of the month.");
                    return 'false';
                }
            }
            gs.info("Today is the first working day of the month.");
            return 'true';
        }
    } else {
        gs.info("Today is NOT a working day.");
        return 'false';
    }

3) then use IF logic to see if that variable is true

4) if yes then create INC

Something like this please enhance further.
I believe I have answered your question

1st working day of month is today.gif

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

@mishrarakes 

are you creating a flow action to check today is 1st working day of month?

Not required.

I informed to use flow and flow variable and also shared the flow logic

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

6 REPLIES 6

Community Alums
Not applicable

Hi @mishrarakes ,

If you're using a flow, then don't need any Scheduled job or script ideally to achieve your requirement, you can simply do the following:

Use the trigger condition as scheduled monthly on 1st day.

SandeepDutta_0-1744015820281.png

 


Use OOB actions to create the incident record and fill in the required fields for the incident.

SandeepDutta_1-1744015820229.png

 

 

 

mishrarakes
Tera Contributor

It should create on first working day of the month i.e Mon - Friday not sat sun

Ankur Bawiskar
Tera Patron
Tera Patron

@mishrarakes 

you can run flow on Daily at 8am then check if it's today is first Working day or not

1) create a flow variable of type string

2) use script to get check if today is 1st working day of current month (Monday to Friday), I considered Monday as Start of week and Saturday and Sunday as holidays

 var gdt = new GlideDateTime(); // Get the current date and time
    var dayOfMonth = gdt.getDayOfMonth(); // Retrieve the day of the month (1-31)
    var dayOfWeek = gdt.getDayOfWeek(); // Retrieve the day of the week (1=Sunday, 7=Saturday)

    // Check if today is a working day (Monday to Friday)
    if (dayOfWeek >= 2 && dayOfWeek <= 6) { // Monday (2) to Friday (6)
        // If it's within the first 7 days of the month
        if (dayOfMonth == 1) {
            gs.info("Today is the first working day of the month.");
            return 'true';
        } else {
            // Check if all previous days are non-working days
            var previousDay = new GlideDateTime(gdt);
            for (var i = dayOfMonth - 1; i > 0; i--) {
                previousDay.addDaysUTC(-1); // Go back one day
                var prevDayOfWeek = previousDay.getDayOfWeek();
                if (prevDayOfWeek >= 2 && prevDayOfWeek <= 6) {
                    gs.info("Today is NOT the first working day of the month.");
                    return 'false';
                }
            }
            gs.info("Today is the first working day of the month.");
            return 'true';
        }
    } else {
        gs.info("Today is NOT a working day.");
        return 'false';
    }

3) then use IF logic to see if that variable is true

4) if yes then create INC

Something like this please enhance further.
I believe I have answered your question

1st working day of month is today.gif

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

mishrarakes
Tera Contributor

Hello ankur, thanks for your guidance but I am not getting script option inside add action i tried many times before also , can i add script inside set flow variable script section