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

justin_drysdale
Mega Guru

You can create a Bussiness Rule on the Outage table.   Conditionalize it so that it runs when your selected field(s) update.   I am assuming your incident table has a outage ticket reference field that points back to the outage.   Here is an example:



//BR:


//when: after,   update


//order: 1001


//table: <your outage table>


//cond:


current.state.changesTo('In Progress') || current.field1.changes() || current.<your field>.changes();


//script:



//update inc with outage work notes. Assuming the incident table has a outage reference field that points to Outage:


var outage_work_notes_latest = current.work_notes.getJournalEntry(1); //gets the latest entry in work_notes.


var inc = new GlideRecord('incident');


          inc.addQuery('outage', current.sys_id); //the outage reference field on INC is getting matched up to the current outage sys_id.


          inc.query();


          if(inc.next()) {


              //incident found, now update the work_notes:


              inc.work_notes += outage_work_notes_latest;


              inc.update();


          }


There isn't an outage reference field on the incident table. I have a related list showing associated outage records related to Task. What would I need to do in that case?


Glen,



  Then there should be reference field for outage records on task table.You can write Glide Record query on task table instead of incident table, since incident is extended from task table.



So replace this line in the above script "var inc = new GlideRecord('incident'); " with "var inc = new GlideRecord('task');"


There is no reference from the task table to outage that I can find. What I did find was a table called "task_outage" which seems to connect the two. This is odd to me but that seems to be where the connection is made. It has a reference field to both task and outage. The issue I find there is that not all of my outages are listed. Are any of you using outage and if so, can you take a look?