Check duplicate in the task_outage and refrain from creating a new entry

olufsen
Kilo Sage

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);

 

  

olufsen_2-1709763360589.png olufsen_0-1709763318228.png 

olufsen_1-1709763338164.png

 

 

2 ACCEPTED SOLUTIONS

SanjivMeher
Kilo Patron
Kilo Patron

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.

View solution in original post

AshishKM
Kilo Patron
Kilo Patron

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

View solution in original post

2 REPLIES 2

SanjivMeher
Kilo Patron
Kilo Patron

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.

AshishKM
Kilo Patron
Kilo Patron

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