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.

Script Help to exclude weekends

Jyoti Tripathi
Giga Guru

I have a custom True/False field, i wanted to make the field set as true when the Assignment Group for the case is empty for more than 1 business days..
Someone please help how to achieve this

13 REPLIES 13

Baala T
Mega Guru

Hi Jyoti,

 

Please try below code in scheduled job, value of iDay 6 and 7 are referring 

  • 6 = Saturday
  • 7 = Sunday
var grCase = new GlideRecord('<TABLE_NAME>');
grCase.addEncodedQuery('active=true^assignment_group=NULL');
grCase.query();

while(grCase.next()){
	var gdtCreated = new GlideDateTime(grCase.getValue('sys_created_on'));

	var bIsWorkingDay = false;

	do {
		var iDay = gdtCreated.getDayOfWeekLocalTime();

		gdtCreated.addDaysLocalTime(1);

		if((iDay!=6) && (iDay!=7)){
			bIsWorkingDay = true;
		}
	} while (!bIsWorkingDay);

	var gdtCurrent = new GlideDateTime();

	//gs.print(gdtCreated);
	//gs.print(gdtCurrent);

	if(gdtCreated < gdtCurrent) {
		grCase.setValue('CUSTOM_FIELD_NAME', true);
		grCase.update();
	}
}

 

Regards,
Bala

 

Hi @Baala T : Thanks for sharing the script, what if the case got created on weekend and group was empty (example on Saturday).. so on Tuesday(More than 1 business days) it will make the field set as true right?

Modified the script little bit. PFB

 

var gdtCreated = new GlideDateTime('2023-06-02 09:27:56');
gs.print('Actual Created Date & Time : ' + gdtCreated);

var bIsWorkingDay = false;
var bCheckCreatedInNonBusinessDays = false;

do {
    var iCreatedDay = gdtCreated.getDayOfWeekLocalTime();

    if(!bCheckCreatedInNonBusinessDays){
        //Is it created on Saturday
        if (iCreatedDay==6){
            gdtCreated.addDaysLocalTime(1);		
        }

        //Check if it is falls on Sunday
        iCreatedDay = gdtCreated.getDayOfWeekLocalTime();
        if (iCreatedDay==7){
            gdtCreated.addDaysLocalTime(1);		
        }

        bCheckCreatedInNonBusinessDays = true;
        gs.print('Business Start Date & Time : ' + gdtCreated);
    }

    //Add a Business Day
    gdtCreated.addDaysLocalTime(1);
    
    var iBusinessDay = gdtCreated.getDayOfWeekLocalTime();

    gs.print(iBusinessDay + '<>' + gdtCreated)

    if((iBusinessDay!=6) && (iBusinessDay!=7)) {
        bIsWorkingDay = true;
    }
} while (!bIsWorkingDay);

var gdtCurrent = new GlideDateTime();

gs.print('Business End Date & Time : ' + gdtCreated);
gs.print('Current Date & Time : ' + gdtCurrent);

 

 

@Baala T :
it is not make the Custom field to set it as true when Assignment group is empty for more than 1 business days, please help

Hope you have created as a scheduled job, What is the error that you are getting?

Let me know the Table Name and Field Name.