Send Notification 5 business days before the Contract start date

Bhavana Reddy
Mega Guru

Hello Guys,

We need to send a Notification to the Business owners 5 Business days ( Mon to friday excluding weekends) before the Contract start date , can anyone please help me with the script part please

I tried a sample script in the background script but it doesnot work

var contractCi = new GlideRecord("contract_rel_ci");
contractCi.addNotNullQuery('ci_item');
contractCi.addEncodedQuery('contract.contract_model=e863bd7c1b7f6410138cfffe034bcb43^contract.ref_u_svo.u_ramp=true^contract.active=true^contract.startsISNOTEMPTY');
contractCi.addQuery('ci_item', '019fe1fadbfab01033b3560868961996'); // example record
contractCi.query();
while (contractCi.next()) {
var gdt = new GlideDateTime(contractCi.contract.starts);
var day = gdt.getDayOfWeekLocalTime();
if (day == 1 || day == 2 || day == 3 || day == 4 || day == 5) {
gdt.addDaysUTC(-7);
} else if (day == 6) {
gdt.addDaysUTC(-5);
} else if (day == 7) {
gdt.addDaysUTC(-6);
}


var dur=GlideDateTime.subtract(gdt, new GlideDateTime(contractCi.contract.starts));
var days = dur.getRoundedDayPart();
if(days== 7{

gs.eventQueue("ramp.reminder", contractCi, contractCi.contract.u_owner );
}

}

8 REPLIES 8

Pedro Grilo1
Mega Sage

Hi,

 

I think you can take a different approach and just compare two GlideDateTime objects, today+5 days based on weekday schedule, and the contract start date.

Something like this:

var numberOfDays = 5;
var today = new GlideDateTime();
var schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828'); //check the sys_id of your weekdays schedule
var threshold = schedule.add(today, 24*60*60*1000*numberOfDays); //ms
var contractCi = new GlideRecord("contract_rel_ci");
contractCi.addNotNullQuery('ci_item');
contractCi.addEncodedQuery('contract.contract_model=e863bd7c1b7f6410138cfffe034bcb43^contract.ref_u_svo.u_ramp=true^contract.active=true^contract.startsISNOTEMPTY');
contractCi.addQuery('ci_item', '019fe1fadbfab01033b3560868961996'); // example record
contractCi.query();
while (contractCi.next()) {
	var gdt = new GlideDateTime(contractCi.contract.starts);
	if(threshold > gdt)
		gs.eventQueue("ramp.reminder", contractCi, contractCi.contract.u_owner );
}

 

Hope it helps!

Pedro

Kevin Paul
Mega Guru

Hi,

Pls check the below link,

We have a requirement to send email notification 5 business days before the selected date in catalog...

 

Please mark correct/helpful based on impact 🙂

Hi Kevin,

 

The above solution doesnt work because in our case we need to send notification exactly before 5 business days from the contract start date, The above solution doesnt work for exact condition

for ex: My contract start date is 23rd may and the notification should go only on 16th may and this exact condition the above solution doesnot meet, it is checking before 5 business days but not exact 5 business days.

I have written the schedule job everyday check this condition not written BR.

Hi @Bhavana Reddy 

I tried your script in my instance using OOTB contracts table and triggered some random event. I made some changes to the script and it worked for me. The event got triggered and the notification was sent. Let me add my script here, the table, record and event is from my instance.

gr.ends is end date which I had set to next friday 20/5/2022

 

var gr = new GlideRecord('ast_contract');
gr.get('010205f9df520100a9e78b6c3df26313');
gr.query();
if(gr.next())
{
    var gdt = new GlideDateTime(gr.ends);
    var day = gdt.getDayOfWeekUTC();
    gs.print(day);
    if (day == 1 || day == 2 || day == 3 || day == 4 || day == 5) 
        gdt.addDaysUTC(-7);
        if (day == 6) 
        gdt.addDaysUTC(-5);
        if (day == 7)
        gdt.addDaysUTC(-6);
    var duration = new GlideDateTime.subtract(gdt, new GlideDateTime(gr.ends));
    var days = duration.getRoundedDayPart();
    gs.print(days);
    if (days == 7)
    gs.eventQueue('notification.french',gr,gr.approver);

}