delay SLA
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2025 08:20 PM
hi All,,
i trust this question finds you well.
here is my requirement.
create an SLA for group approvals that triggers when if the start_date (planned start date) in the change request is within 7 days of "today".
Solution
i created an sla definition and works great in the sense that only attaches the sla to the group approval if the start_date of the change is within 7 days of today.
issue
for those records outside the 7 days, i am not attaching the sla to the group approval. So i created a scheduled job to run everyday and check for changes where the conditions are the same as the SLA definition. If not met, i skip it, but if met, i want to attach the SLA. The start date must be the date the script runs with the time of the start_date. The issue is that the script in the scheduled job does not take in consideration the work schedule in the sla definition and the breach time is not populated.
how could i solve this OR which other way i do have to meet my requirements.
var today = new GlideDateTime();
today.setValue(today.getDate() + ' 23:59:59'); // Set time to 23:59:59 (end of the day)
var sevenDaysLater = new GlideDateTime(today);
sevenDaysLater.addDays(7);
// search for normal changes in assess or scheduled or authorized stage
var chgGr = new GlideRecord('change_request');
chgGr.addEncodedQuery("stateIN-4,-3,-2^type=normal^start_date>=javascript:gs.beginningOfToday()");
chgGr.addQuery('start_date', '<=', sevenDaysLater); // Ensure exact match for 7 days
chgGr.query();
// read each approval group for the change request
while (chgGr.next()) {
var groupApproval = new GlideRecord('sysapproval_group');
groupApproval.addQuery('parent',chgGr.sys_id);
// exclude CAB and E2E approval groups from latest round of approval
groupApproval.addEncodedQuery("assignment_group!=fe1fd6f1db8f3740f0195d87f4961972^assignment_group!=d5950787db8f7300f0195d87f4961999^u_latest_approval=true");
groupApproval.query();
while(groupApproval.next()){
var grapp = groupApproval.sys_id;
// check for existing sla link to group approval. if it does not exist i create it
var slaTask = new GlideRecord('task_sla');
slaTask.addQuery('task',grapp);
slaTask.addQuery('active',true);
slaTask.addQuery('sla', '3f23054fdbad3850d605322af49619b7');
slaTask.addQuery('task.parent',chgGr.sys_id);
slaTask.query();
if(!slaTask.next()){
var slaDefinitionSysId = '3f23054fdbad3850d605322af49619b7';
var slaStartDate = new GlideDateTime(chgGr.start_date);
slaStartDate.addDays(-7); // Move exactly 7 days back (preserves time component)
slaTask.initialize();
slaTask.task = grapp;
slaTask.sla = slaDefinitionSysId;
// Initialize a new GlideSLA object
var sla = new GlideSLA();
sla.setSLA(slaDefinitionSysId); // Set the SLA definition
slaTask.start_time = slaStartDate;
// Set the breach time (2 business days = 16 hours)
// Manually adding 2 business days considering the SLA's work schedule
var breachTime = new GlideDateTime(slaStartDate);
// Add 2 business days considering the work schedule of the SLA
var workSchedule = sla.getWorkSchedule();
breachTime = workSchedule.getEndDate(slaStartDate, 2); // 2 business days from the SLA start date
// Set the planned end time (breach time) on the SLA task
slaTask.planned_end_time = breachTime;
// Insert the SLA task into the database
slaTask.insert();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2025 08:29 PM
Hello @El Cuchi
I have made below modifications in the script.
Could you please try that -
// Set the SLA definition
sla.setSLA(slaDefinitionSysId);
// Set the SLA start time
slaTask.start_time = slaStartDate;
// Get the work schedule from the SLA
var workSchedule = sla.getWorkSchedule();
if (!workSchedule) {
gs.error("Work schedule not found for the SLA definition.");
} else {
// Calculate breach time: 2 business days from SLA start date
var breachTime = workSchedule.getEndDate(new GlideDateTime(slaStartDate), 2);
if (breachTime) {
// Set the planned end time (breach time) on the SLA task
slaTask.planned_end_time = breachTime;
gs.info("Breach Time Calculated: " + breachTime.getDisplayValue());
} else {
gs.error("Failed to calculate breach time.");
}
}
// Insert the SLA task into the database
slaTask.insert();
Please mark my answer as helpful or accept as solution if it helped you in anyway.
Regards,
Shivalika
LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194
YouTube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=cwYLjvjCI-EdquTd
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2025 09:32 PM
thank you so much for your quick response
here is the latest code including your modifications. unfortunately, the breach time is not being populated.
var today = new GlideDateTime();
today.setValue(today.getDate() + ' 23:59:59'); // Set time to 23:59:59 (end of the day)
var sevenDaysLater = new GlideDateTime(today);
sevenDaysLater.addDays(7);
// search for normal changes in assess or scheduled or authorized stage
var chgGr = new GlideRecord('change_request');
chgGr.addEncodedQuery("stateIN-4,-3,-2^type=normal^start_date>=javascript:gs.beginningOfToday()");
chgGr.addQuery('start_date', '<=', sevenDaysLater); // Ensure exact match for 7 days
chgGr.query();
// read each approval group for the change request
while (chgGr.next()) {
var groupApproval = new GlideRecord('sysapproval_group');
groupApproval.addQuery('parent',chgGr.sys_id);
// exclude CAB and E2E approval groups from latest round of approval
groupApproval.addEncodedQuery("assignment_group!=fe1fd6f1db8f3740f0195d87f4961972^assignment_group!=d5950787db8f7300f0195d87f4961999^u_latest_approval=true");
groupApproval.query();
while(groupApproval.next()){
var grapp = groupApproval.sys_id;
// check for existing sla link to group approval. if it does not exist i create it
var slaTask = new GlideRecord('task_sla');
slaTask.addQuery('task',grapp);
slaTask.addQuery('active',true);
slaTask.addQuery('sla', '3f23054fdbad3850d605322af49619b7');
slaTask.addQuery('task.parent',chgGr.sys_id);
slaTask.query();
if(!slaTask.next()){
var slaDefinitionSysId = '3f23054fdbad3850d605322af49619b7';
var slaStartDate = new GlideDateTime(chgGr.start_date);
slaStartDate.addDays(-7); // Move exactly 7 days back (preserves time component)
slaTask.initialize();
slaTask.task = grapp;
slaTask.sla = slaDefinitionSysId;
slaTask.start_time = slaStartDate;
// Get the work schedule from the SLA
var sla = new GlideRecord('contract_sla');
sla.addquery('sys_id',slaDefinitionSysId);
sla.query();
if(sla.next()){
var workSchedule = sla.getWorkSchedule();
if (!workSchedule) {
gs.error("Work schedule not found for the SLA definition.");
} else {
// Calculate breach time: 2 business days from SLA start date
var breachTime = workSchedule.getEndDate(new GlideDateTime(slaStartDate), 2);
if (breachTime) {
// Set the planned end time (breach time) on the SLA task
slaTask.planned_end_time = breachTime;
gs.info("Breach Time Calculated: " + breachTime.getDisplayValue());
} else {
gs.error("Failed to calculate breach time.");
}
}
// Insert SLA Task
slaTask.insert();
}
}
}
}