- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2024 02:17 PM - edited 03-06-2024 02:21 PM
Hi, I have a requirement that checks the PRB first reported by has an existing Outage then create a new link for the PRB record.
This is my BR that works fine however, it keeps on creating a link even though a Task-Outage link has been made already. What filter do I have to add to refrain from this record to create a new entry if already established.
Table: Problem
After insert/update
First reported by is not empty
(function executeRule(current, previous /*null when async*/) {
// Add your code here
//Get the task for the First Reported by
var frb = current.first_reported_by_task;
//Check if FRb has associated Outages and return the Outage number
if (frb != '') {
var checkTask = new GlideRecord('task_outage');
checkTask.addQuery('task', frb);
checkTask.query();
while (checkTask.next()) {
var outageTask = checkTask.outage.sys_id;
}
}
//Create a new Task Outage relationship
var taskOutage = new GlideRecord('task_outage');
taskOutage.initialize();
taskOutage.outage = outageTask;
taskOutage.begin = current.opened_at;
taskOutage.task = current.sys_id;
taskOutage.insert();
})(current, previous);
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2024 02:46 PM
Before creating a new outage relationship, check if one exists
(function executeRule(current, previous /*null when async*/) {
// Add your code here
//Get the task for the First Reported by
var frb = current.first_reported_by_task;
//Check if FRb has associated Outages and return the Outage number
if (frb != '') {
var checkTask = new GlideRecord('task_outage');
checkTask.addQuery('task', frb);
checkTask.query();
while (checkTask.next()) {
var outageTask = checkTask.outage.sys_id;
}
}
//Create a new Task Outage relationship only when a relationship doesnt exists.
var taskOutage = new GlideRecord('task_outage');
taskOutage.addQuery('outage',outageTask);
taskOutage.addQuery('task',current.sys_id);
taskOutage.query();
if (!taskOutage.next())
{
taskOutage.newRecord();
taskOutage.outage = outageTask;
taskOutage.begin = current.opened_at;
taskOutage.task = current.sys_id;
taskOutage.insert();
}
})(current, previous);
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2024 02:52 PM
Hi @olufsen ,
The BR code will create a new record in table [ task_outage ] every time the PRB either a new insert or update because the code has written for insert.
I believe, if you keep updating the PRB .. you will see more duplicate outage record in the task_outage table.
try with this updated code, added the code to check if outage already exist/mapped with same PRB.
//Create a new Task Outage relationship
var taskOutage = new GlideRecord('task_outage');
taskOutage.addQuery("task", current.sys_id); //check is outage record exist
taskOutage.addQuery("outage", outageTask); //
taskOutage.query();
// if not outage record exist for same PRB and same outage of First reported by
if(!taskOutage.next()) {
taskOutage.initialize();
taskOutage.outage = outageTask;
taskOutage.begin = current.opened_at;
taskOutage.task = current.sys_id;
taskOutage.insert();
}
-Thanks,
AshishKM
Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2024 02:46 PM
Before creating a new outage relationship, check if one exists
(function executeRule(current, previous /*null when async*/) {
// Add your code here
//Get the task for the First Reported by
var frb = current.first_reported_by_task;
//Check if FRb has associated Outages and return the Outage number
if (frb != '') {
var checkTask = new GlideRecord('task_outage');
checkTask.addQuery('task', frb);
checkTask.query();
while (checkTask.next()) {
var outageTask = checkTask.outage.sys_id;
}
}
//Create a new Task Outage relationship only when a relationship doesnt exists.
var taskOutage = new GlideRecord('task_outage');
taskOutage.addQuery('outage',outageTask);
taskOutage.addQuery('task',current.sys_id);
taskOutage.query();
if (!taskOutage.next())
{
taskOutage.newRecord();
taskOutage.outage = outageTask;
taskOutage.begin = current.opened_at;
taskOutage.task = current.sys_id;
taskOutage.insert();
}
})(current, previous);
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2024 02:52 PM
Hi @olufsen ,
The BR code will create a new record in table [ task_outage ] every time the PRB either a new insert or update because the code has written for insert.
I believe, if you keep updating the PRB .. you will see more duplicate outage record in the task_outage table.
try with this updated code, added the code to check if outage already exist/mapped with same PRB.
//Create a new Task Outage relationship
var taskOutage = new GlideRecord('task_outage');
taskOutage.addQuery("task", current.sys_id); //check is outage record exist
taskOutage.addQuery("outage", outageTask); //
taskOutage.query();
// if not outage record exist for same PRB and same outage of First reported by
if(!taskOutage.next()) {
taskOutage.initialize();
taskOutage.outage = outageTask;
taskOutage.begin = current.opened_at;
taskOutage.task = current.sys_id;
taskOutage.insert();
}
-Thanks,
AshishKM
Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution