SLA - that ends on the first working day of the next month at 08:30 AM?

Augustohenri01
Tera Expert

Hi guys, could someone help me, how can I create an SLA that ends on the first working day of the next month at 08:30 AM?

1 ACCEPTED SOLUTION

Augustohenri01
Tera Expert

Thanks for the replies, what did the trick was to create a Relative Duration:

 

(function() {

    var startDateMs = calculator.startDateTime;

	
    var retorno = verificaDiaPrimeiro(startDateMs);
    if (retorno) {
        //Criar o calculo para o mesmo dia
		var dateTime = new GlideDateTime(startDateMs);
		current.planned_end_time = dateTime.getDate() + " 10:00:00";
    }
    if (!retorno) {
			senaofordiautil(startDateMs);
        //utilizar a segunda função para calcular o primeiro dia util do proximo mês
    }


    function verificaDiaPrimeiro(startTime) {


        var startDate = new GlideDateTime(startTime).getLocalDate();

        var mes = startDate.getMonthNoTZ();
        var ano = startDate.getYearNoTZ();
        var dia = startDate.getDayOfMonthNoTZ();

        var diasAnterioresUteis = 0;

        if (dia != 01) { //Vai verificar se exitem dias úteis antes do dia que está na data

            for (var i = dia - 1; i > 0; i--) {
                var grData = ano + '-' + mes + '-' + i;
                var diaAtual = new GlideDateTime(grData);
                // Verifica se o dia é um dia útil (segunda a sexta-feira)
                var diaSemana = diaAtual.getDayOfWeekUTC();
                if (diaSemana !== 7 && diaSemana !== 6) {
                    diasAnterioresUteis++;
                }
            }

        }

        if (dia == 01 || diasAnterioresUteis == 0) { //vai verificar se é dia útil

            var day = startDate.getDayOfWeekUTC();

            if (day == 7 || day == 6) { //sabado ou domingo

                return false;

            } else {
               

                var time = new GlideDateTime(startTime).getLocalTime();
                var gt = time.getTime();
                var timeStr = gt.getByFormat('HH:mm:ss'); // 24 hour format
                var split = timeStr.split(":");
                
                if (split[0] < "09") {

                    return true;

                } else {
					
                    return false;
                
				}

            }

        } else {

            return false;

        }

    }

    function senaofordiautil(start_time) {

        var startDateTime = new GlideDateTime(start_time);
        var startTime = new GlideDateTime().getLocalTime().getByFormat('HH:mm:ss');
       
      
        //Cria um novo objeto
        var sentDateTime = new GlideDateTime(startDateTime);
        //Pega o número de dias no mês
        var diasInMonth = sentDateTime.getDaysInMonth();
        //Pega o dia atual
        var sentDay = sentDateTime.getDayOfMonth();
        //Cria um novo objeto
        var firstOfMonth = new GlideDateTime(sentDateTime).getLocalDate();
        //pega o dia atual e diminui menos 1 e deixa ele negativo
        firstOfMonth.addDaysLocalTime(-1 * (sentDay - 1));
        //adiciona o proximo mês na data
        firstOfMonth.addMonthsUTC(1);

        var data = firstOfMonth.getDisplayValue() + " " + "10:00:00";


        var gdt = new GlideDateTime();
        gdt.setDisplayValue(data, "dd/MM/yyyy HH:mm:ss");

		if(gdt.getDayOfWeekUTC() == 6 || gdt.getDayOfWeekUTC()==7){
        do {
            gdt.addDaysUTC(1);
        } while (gdt.getDayOfWeekUTC() == 6 || gdt.getDayOfWeekUTC() == 7);
		}
		
		var dateTimeForField = new GlideDateTime(gdt.getDate());
		
		//calculator.calcRelativeDueDate(startDateTime.getDate,dateTimeForField,"10:00:00");
    current.planned_end_time = dateTimeForField.getDate() + " 10:00:00";
	}

})();

 

View solution in original post

9 REPLIES 9

NaveenGN
Tera Expert

Hi @Augustohenri01 

Write a schedule jobs which 

and check the below code i have used background script please change according to your requirement

ar firstDay = new GlideDateTime();
var firstDayOfOctober = new GlideDateTime();
firstDayOfOctober.setMonth(10); // October
firstDayOfOctober.setDayOfMonth(1);

// Check if the first day of October is a weekend (Saturday or Sunday)
if (firstDayOfOctober.getDayOfWeek() == 1 /* Sunday */ || firstDayOfOctober.getDayOfWeek() == 7 /* Saturday */) {
  // If it's a weekend, add the appropriate number of days to make it a weekday (Monday)
  var daysToAdd = 2; // For Sunday, add 2 days to make it Monday
  if (firstDayOfOctober.getDayOfWeek() == 7) {
    daysToAdd = 1; // For Saturday, add 1 day to make it Monday
  }
  firstDayOfOctober.addDays(daysToAdd);
}
gs.info(firstDay)

if(firstDayOfOctober.compareTo(firstDay)==0){
    var gr = new GlideRecord('task_sla');
    if(gr.get('1b482f989f23320030581471367fcf3e')){
        gr.has_breached = 'true';
        gr.setValue('end_time',firstDayOfOctober)
        gr.update()
    }
}

Please mark the answer as correct if its helpful

Best Regards,

Naveen G N

Hi @NaveenGN, Thank you very much for your reply. The scrip is wrong, because the next month could be any month and not just October. 

NaveenGN
Tera Expert

Hello @Augustohenri01 ,

just remove setMonth(10) so it would change change automatically everyday.

 

Best Regards,

Naveen G N

Tony Chatfield1
Kilo Patron

Hi, I would look at creating a custom 'Relative duration'

SLA duration types (servicenow.com)

/cmn_relative_duration_list.do?

 

As per other comments, I am not sure I agree with this type of configuration\ and don't see how it adds real value to the business. But if you are tasked with delivering it, then based on your original post a custom relative duration may be the best solution. 

Augustohenri01
Tera Expert

Thanks for the replies, what did the trick was to create a Relative Duration:

 

(function() {

    var startDateMs = calculator.startDateTime;

	
    var retorno = verificaDiaPrimeiro(startDateMs);
    if (retorno) {
        //Criar o calculo para o mesmo dia
		var dateTime = new GlideDateTime(startDateMs);
		current.planned_end_time = dateTime.getDate() + " 10:00:00";
    }
    if (!retorno) {
			senaofordiautil(startDateMs);
        //utilizar a segunda função para calcular o primeiro dia util do proximo mês
    }


    function verificaDiaPrimeiro(startTime) {


        var startDate = new GlideDateTime(startTime).getLocalDate();

        var mes = startDate.getMonthNoTZ();
        var ano = startDate.getYearNoTZ();
        var dia = startDate.getDayOfMonthNoTZ();

        var diasAnterioresUteis = 0;

        if (dia != 01) { //Vai verificar se exitem dias úteis antes do dia que está na data

            for (var i = dia - 1; i > 0; i--) {
                var grData = ano + '-' + mes + '-' + i;
                var diaAtual = new GlideDateTime(grData);
                // Verifica se o dia é um dia útil (segunda a sexta-feira)
                var diaSemana = diaAtual.getDayOfWeekUTC();
                if (diaSemana !== 7 && diaSemana !== 6) {
                    diasAnterioresUteis++;
                }
            }

        }

        if (dia == 01 || diasAnterioresUteis == 0) { //vai verificar se é dia útil

            var day = startDate.getDayOfWeekUTC();

            if (day == 7 || day == 6) { //sabado ou domingo

                return false;

            } else {
               

                var time = new GlideDateTime(startTime).getLocalTime();
                var gt = time.getTime();
                var timeStr = gt.getByFormat('HH:mm:ss'); // 24 hour format
                var split = timeStr.split(":");
                
                if (split[0] < "09") {

                    return true;

                } else {
					
                    return false;
                
				}

            }

        } else {

            return false;

        }

    }

    function senaofordiautil(start_time) {

        var startDateTime = new GlideDateTime(start_time);
        var startTime = new GlideDateTime().getLocalTime().getByFormat('HH:mm:ss');
       
      
        //Cria um novo objeto
        var sentDateTime = new GlideDateTime(startDateTime);
        //Pega o número de dias no mês
        var diasInMonth = sentDateTime.getDaysInMonth();
        //Pega o dia atual
        var sentDay = sentDateTime.getDayOfMonth();
        //Cria um novo objeto
        var firstOfMonth = new GlideDateTime(sentDateTime).getLocalDate();
        //pega o dia atual e diminui menos 1 e deixa ele negativo
        firstOfMonth.addDaysLocalTime(-1 * (sentDay - 1));
        //adiciona o proximo mês na data
        firstOfMonth.addMonthsUTC(1);

        var data = firstOfMonth.getDisplayValue() + " " + "10:00:00";


        var gdt = new GlideDateTime();
        gdt.setDisplayValue(data, "dd/MM/yyyy HH:mm:ss");

		if(gdt.getDayOfWeekUTC() == 6 || gdt.getDayOfWeekUTC()==7){
        do {
            gdt.addDaysUTC(1);
        } while (gdt.getDayOfWeekUTC() == 6 || gdt.getDayOfWeekUTC() == 7);
		}
		
		var dateTimeForField = new GlideDateTime(gdt.getDate());
		
		//calculator.calcRelativeDueDate(startDateTime.getDate,dateTimeForField,"10:00:00");
    current.planned_end_time = dateTimeForField.getDate() + " 10:00:00";
	}

})();