Need help with creating business rule to update task work notes when outage record updates

keygw
Tera Guru

We are starting to use Outage records to keep track of our outages. A lot of these are automated based on reporting from SolarWinds. Currently, these are generated off of incidents but we may at some point do the same from a problem. I would like to create a business rule that updates the work notes of a task (incident/problem) to reflect changes to any of the fields on the outage record associated with that current task. The reasoning is due to notifications that are being received by the system/service owners and them wanting to be aware. Can any of you help me with this? I am still new to scripting and having a tough time with getting it to work. Thanks!

1 ACCEPTED SOLUTION

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Since you want changes to the Outage record to be reflected on the task (Incident, Problem, etc), the business rule needs to be on the Outage table and NOT incident.   This way we can detect the change there and post it to the related task.   The Task Outage plugin has several tables involved and the table with all the outage details is cmdb_ci_outage.   The "task_outage" table is just a many to many table and honestly I am not sure why its there since there is a Task reference field on the cmdb_ci_outage table itself.



I created a business rule in my test instance and this works, but feel free to edit to your liking like the verbiage that gets posted into the work notes.


1. Create a business rule for the cmdb_ci_outage table by going to System Definition\Business Rules and clicking New on the list.


2. Enter the following information:


      Name: Task Update


      Table: Outage [cmdb_ci_outage]


      Advanced: Checked


      When: After


      Insert: Checked


      Update: Checked


      Filter Conditions: Task Number is not empty


find_real_file.png


3. On the Advanced tab, enter the following script where is says "add your code here" in between the curly brackets {}.   There are differences here between the Fuji and Geneva releases so again make sure you enter it between the curly brackets:


//Create a variable with related task record


var taskRec = current.task_number.getRefRecord();



//Create an array that updates can be posted to


var taskUpdateMsg = [];


taskUpdateMsg.push("Related Outage Record Updated:");



//Check for changes to CI, Type, Message, Begin, End, and Duration and post to taskUpdateMsg if changed


if (current.cmdb_ci.changes()){


      taskUpdateMsg.push("CI was:" + previous.cmdb_ci.getDisplayValue() + " ==== Now: " + current.cmdb_ci.getDisplayValue());


}


if (current.type.changes()){


      taskUpdateMsg.push("Type was:" + previous.type + " ==== Now: " + current.type);


}


if (current.message.changes()){


      taskUpdateMsg.push("Message was:" + previous.message + " ==== Now: " + current.message);


}


if (current.begin.changes()){


      taskUpdateMsg.push("Begin was:" + previous.begin + " ==== Now: " + current.begin);


}


if (current.end.changes()){


      taskUpdateMsg.push("End was:" + previous.end + " ==== Now: " + current.end);


}


if (current.duration.changes()){


      taskUpdateMsg.push("Duration was:" + previous.duration.getDisplayValue() + " ==== Now: " + current.duration.getDisplayValue());


}



//Join the updates in the taskUpdateMsg array into a string separated by a carriage return and post to the task Work Notes field.


taskRec.work_notes = taskUpdateMsg.join("\n");


taskRec.update();



If you are on the Geneva release, your script should look like the following:


find_real_file.png



4. Click Submit and test!


View solution in original post

6 REPLIES 6

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Since you want changes to the Outage record to be reflected on the task (Incident, Problem, etc), the business rule needs to be on the Outage table and NOT incident.   This way we can detect the change there and post it to the related task.   The Task Outage plugin has several tables involved and the table with all the outage details is cmdb_ci_outage.   The "task_outage" table is just a many to many table and honestly I am not sure why its there since there is a Task reference field on the cmdb_ci_outage table itself.



I created a business rule in my test instance and this works, but feel free to edit to your liking like the verbiage that gets posted into the work notes.


1. Create a business rule for the cmdb_ci_outage table by going to System Definition\Business Rules and clicking New on the list.


2. Enter the following information:


      Name: Task Update


      Table: Outage [cmdb_ci_outage]


      Advanced: Checked


      When: After


      Insert: Checked


      Update: Checked


      Filter Conditions: Task Number is not empty


find_real_file.png


3. On the Advanced tab, enter the following script where is says "add your code here" in between the curly brackets {}.   There are differences here between the Fuji and Geneva releases so again make sure you enter it between the curly brackets:


//Create a variable with related task record


var taskRec = current.task_number.getRefRecord();



//Create an array that updates can be posted to


var taskUpdateMsg = [];


taskUpdateMsg.push("Related Outage Record Updated:");



//Check for changes to CI, Type, Message, Begin, End, and Duration and post to taskUpdateMsg if changed


if (current.cmdb_ci.changes()){


      taskUpdateMsg.push("CI was:" + previous.cmdb_ci.getDisplayValue() + " ==== Now: " + current.cmdb_ci.getDisplayValue());


}


if (current.type.changes()){


      taskUpdateMsg.push("Type was:" + previous.type + " ==== Now: " + current.type);


}


if (current.message.changes()){


      taskUpdateMsg.push("Message was:" + previous.message + " ==== Now: " + current.message);


}


if (current.begin.changes()){


      taskUpdateMsg.push("Begin was:" + previous.begin + " ==== Now: " + current.begin);


}


if (current.end.changes()){


      taskUpdateMsg.push("End was:" + previous.end + " ==== Now: " + current.end);


}


if (current.duration.changes()){


      taskUpdateMsg.push("Duration was:" + previous.duration.getDisplayValue() + " ==== Now: " + current.duration.getDisplayValue());


}



//Join the updates in the taskUpdateMsg array into a string separated by a carriage return and post to the task Work Notes field.


taskRec.work_notes = taskUpdateMsg.join("\n");


taskRec.update();



If you are on the Geneva release, your script should look like the following:


find_real_file.png



4. Click Submit and test!


This worked perfect! Thanks!!