- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-14-2018 05:01 AM
Hi All,
Is it possible to fetch the last recent tagged SLA from each ticket. Each ticket will have few SLA's attached to it
but we require only last recent SLA attached to the tickets for reporting purpose.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-14-2018 07:00 AM
You will need to add another field to capture an additional data point whenever you have a duplicate 'task_sla' record entered. Then you can easily filter what you want on this entry. Here's how you can accomplish this.
1) Create a new 'True/False' field on the 'task_sla' table named 'Last added (u_last_added)'. Give it a default value of 'true'. This field will only be updated by the system so you can also mark it as 'Read only' in the dictionary. This is the field you'll be able to filter on to only show the latest SLA per type. You'll be able to filter and show 'task_sla' records where this field is 'true'.
2) Create a new business rule on the 'task_sla' table with the following settings...
Name: Update 'Last added' SLA
When: After
Insert: True
Advanced: True
Condition: !current.task.nil() && !current.sla.nil()
Script:
(function executeRule(current, previous /*null when async*/) {
// Find the 'Last added' SLA for this task/sla combination and mark it
var tsla = new GlideRecord('task_sla');
tsla.addQuery('task', current.task);
tsla.addQuery('sla', current.sla);
tsla.addQuery('u_last_added', true);
tsla.addQuery('sys_id', '!=', current.sys_id);
tsla.query();
if (tsla.next()) {
tsla.u_last_added = false; // Change the 'u_last_added' field to 'false'
tsla.autoSysFields(false); // Don't update time stamps
tsla.setWorkflow(false); // Don't run any business rules
tsla.update();
}
})(current, previous);
3) The above will solve the problem for SLAs added in the future, but you will also need to run a one-time background script to update any existing data. You can run the following script from the 'Scripts -> Background' module. You'll be updating every 'task_sla' record in your system (no business rules or time stamps will be applied) so proceed with caution and test thoroughly!
var lastTask = '';
var lastsla = '';
// Query all 'task_sla' records
var allsla = new GlideRecord('task_sla');
// Order by task and sla, then by 'sys_created_on' to group correctly
allsla.orderBy('task');
allsla.orderBy('sla');
allsla.orderByDesc('sys_created_on');
allsla.query();
while (allsla.next()) {
allsla.u_last_added = false;
// Find the 'Last added' SLA for this task/sla combination and mark it
if ((allsla.task != lastTask) || (allsla.sla != lastsla)) {
lastTask = allsla.task.toString();
lastsla = allsla.sla.toString();
allsla.u_last_added = true; // Change the 'u_last_added' field to 'true'
}
allsla.autoSysFields(false); // Don't update time stamps
allsla.setWorkflow(false); // Don't run any business rules
allsla.update();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-14-2018 06:44 AM
Hi Kun,
Please try this in background script. This script can get the latest task sla record for the single incident.
******************* START *******************
var holdRecs = [];
var latestRec = new GlideRecord('task_sla');
latestRec.addEncodedQuery('task=' + pass the incident sys_id which is having multiple SLAs atatched to it);
// Example for addEncodedQuery -> latestRec.addEncodedQuery('task=' + '1c741bd70b2322007518478d83673af3');
latestRec.orderByDesc('sys_created_on'); //descending order
latestRec.query();
while(latestRec.next()){
holdRecs.push(latestRec.sys_id.toString());
}
gs.print('Resulted Records: ' + holdRecs);
var getLatestRec = holdRecs[0];
gs.print('Latest Tas SLA: ' + getLatestRec);
******************* END *******************
Regards,
Dharani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-14-2018 06:50 AM
This is the business rule? on before or after i have to run this?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-14-2018 07:00 AM
You will need to add another field to capture an additional data point whenever you have a duplicate 'task_sla' record entered. Then you can easily filter what you want on this entry. Here's how you can accomplish this.
1) Create a new 'True/False' field on the 'task_sla' table named 'Last added (u_last_added)'. Give it a default value of 'true'. This field will only be updated by the system so you can also mark it as 'Read only' in the dictionary. This is the field you'll be able to filter on to only show the latest SLA per type. You'll be able to filter and show 'task_sla' records where this field is 'true'.
2) Create a new business rule on the 'task_sla' table with the following settings...
Name: Update 'Last added' SLA
When: After
Insert: True
Advanced: True
Condition: !current.task.nil() && !current.sla.nil()
Script:
(function executeRule(current, previous /*null when async*/) {
// Find the 'Last added' SLA for this task/sla combination and mark it
var tsla = new GlideRecord('task_sla');
tsla.addQuery('task', current.task);
tsla.addQuery('sla', current.sla);
tsla.addQuery('u_last_added', true);
tsla.addQuery('sys_id', '!=', current.sys_id);
tsla.query();
if (tsla.next()) {
tsla.u_last_added = false; // Change the 'u_last_added' field to 'false'
tsla.autoSysFields(false); // Don't update time stamps
tsla.setWorkflow(false); // Don't run any business rules
tsla.update();
}
})(current, previous);
3) The above will solve the problem for SLAs added in the future, but you will also need to run a one-time background script to update any existing data. You can run the following script from the 'Scripts -> Background' module. You'll be updating every 'task_sla' record in your system (no business rules or time stamps will be applied) so proceed with caution and test thoroughly!
var lastTask = '';
var lastsla = '';
// Query all 'task_sla' records
var allsla = new GlideRecord('task_sla');
// Order by task and sla, then by 'sys_created_on' to group correctly
allsla.orderBy('task');
allsla.orderBy('sla');
allsla.orderByDesc('sys_created_on');
allsla.query();
while (allsla.next()) {
allsla.u_last_added = false;
// Find the 'Last added' SLA for this task/sla combination and mark it
if ((allsla.task != lastTask) || (allsla.sla != lastsla)) {
lastTask = allsla.task.toString();
lastsla = allsla.sla.toString();
allsla.u_last_added = true; // Change the 'u_last_added' field to 'true'
}
allsla.autoSysFields(false); // Don't update time stamps
allsla.setWorkflow(false); // Don't run any business rules
allsla.update();
}