Need help with "sc req item events" Business Rule

alhicks
Tera Guru

We need the "sc_req_item.inserted" event to also fire if the "Request.Requested_For", changes. So, we need it to fire when the Item is requested, like it does now but we also need it to fire if the request.requested_for changes to someone else, so they can also receive the notification. I tried changing the below Business Rule but ended up receiving duplicate notifications when the Item was opened.



if (current.operation() != 'insert' && current.comments.changes()) {
gs.eventQueue("sc_req_item.commented", current, gs.getUserID(), gs.getUserName());
}

if (current.operation() == 'insert') {
gs.eventQueue("sc_req_item.inserted", current, gs.getUserID(), gs.getUserName());
}

if (current.operation() == 'update') {
gs.eventQueue("sc_req_item.updated", current, gs.getUserID(), gs.getUserName());
}

if (current.operation() == 'update' && current.stage == "delivery") {
gs.eventQueue("sc_req_item.delivery", current, gs.getUserID(), gs.getUserName());
}

if (!current.assigned_to.nil() && current.assigned_to.changes()) {
gs.eventQueue("sc_req_item.assigned", current, current.assigned_to.getDisplayValue() , previous.assigned_to.getDisplayValue());
}

if (current.stage.changes()) {
gs.eventQueue("sc_req_item.change.stage", current, current.stage, previous.stage);
}

7 REPLIES 7

Brad Tilton
ServiceNow Employee
ServiceNow Employee

You could change:



if (current.operation() == 'insert') {
gs.eventQueue("sc_req_item.inserted", current, gs.getUserID(), gs.getUserName());
}


To:


if (current.operation() == 'insert' || current.requested_for.changes()) {
gs.eventQueue("sc_req_item.inserted", current, gs.getUserID(), gs.getUserName());
}


Or just add another few lines to the end of the rule:


if (current.operation() != 'insert' && current.requested_for.changes()) {
gs.eventQueue("sc_req_item.inserted", current, gs.getUserID(), gs.getUserName());
}


john_roberts
Mega Guru

It would be more reliable to trigger the requested by change event from a request table rule since that's really where the change is taking place. If you have the derived field (request.requested_by) on the requested item form and you only change the value in the derived field, the requested item record has not been changed so none of the rules will run.
You can enable the Debug Business Rule module to view the business rules running.

This model also covers the case where you have multiple items for a single request.
You can still call the same event, just make sure you pass the requested item glide records instead of current since current would be the request.

Would look something like:



//request rule when requested_by changes
var items = new GlideRecord("sc_req_item");
items.addQuery("request", current.sys_id);
items.query();
while (items.next()) {
gs.eventQueue("sc_req_item.inserted", items, gs.getUserID(), gs.getUserName());
}


Ok. Thank you for all your help.