Copy comments from approval to requested item and from requested item to approval

rody-bjerke
Giga Guru

Hi,

In service portal you have a screen for requested items and one for approvals (the one the approver see).

Is there an easy way to copy the comments from approvals to requested item if someone write something in the approval, and then the other way if someone writes in the activity stream in requested item, copy it from requested item to the approval. (without getting a loop where the message goes back and forward)

Best regards,

1 ACCEPTED SOLUTION

This is the Business Rule running onBefore on sc_task when additional comments change:



(function executeRule(current, previous /*null when async*/) {



// This BR is running onBefore on intention, as we need to decide if we wanna update foreign records and cut a specific part of the string before we are saving


var comments = current.comments;


var commentsFromForeignRecord = (comments.startsWith('Customer comments:'));


if (!commentsFromForeignRecord) {


var ritm = current.request_item.getRefRecord();


ritm.comments = 'Supporter comments:\n' + comments;


ritm.update();


}



})(current, previous);



In this specific scenario the customer has the possibility to post comments on RITM level and we want to promote them to sc_task. Other way around the agent is only posting comments on Catalog Task level, which should be available to the customer on the RITM as well.



This is the onBefore BR running on sc_req_item to get the customer comments when additional comments change:



(function executeRule(current, previous /*null when async*/) {



// This BR is running onBefore on intention, as we need to decide if we wanna update foreign records and cut a specific part of the string before we are saving


var ctasks = new GlideRecord('sc_task');


ctasks.addQuery('request_item', current.sys_id + '');


ctasks.addActiveQuery();


ctasks.query();


while (ctasks.next()) {


var comments = current.comments;


var commentsAllowedToBeShared = (!comments.startsWith('Supporter comments:'));


if (commentsAllowedToBeShared) {


ctasks.comments = 'Customer comments:\n' + comments;


ctasks.update();


}


}



})(current, previous);


View solution in original post

8 REPLIES 8

Deepak Ingale1
Mega Sage

I Rody,



Its tricky as you have already figured that out since it may end up in a loop.


What could be the long solution for such issue would be to build the formatter by querrying sys_history_line table, get details against comments element from request and show those to sysapproval and vice-versa. This will require some efforts, but you can ensure no "programatic" loops in it.


Mwatkins
ServiceNow Employee
ServiceNow Employee

The way to avoid getting into loops is to use GlideRecord.setWorkflow(false). See the following:


API Doc: GlideRecord-setWorkflow(boolean)


Prevent Recursive Business Rules


Ulrich Jugl
ServiceNow Employee
ServiceNow Employee

There are times where you can't avoid setWorkflow(false) as you would need to update the record and let other business rules run (e.g. assume the situation where a customer is entering a comment on RITM and this comment has to be send to the current Catalog Task. What I did in the past is to add a prefix, so you can avoid the update if a specific prefix is not there, e.g.



Comment from the Approval: <message>


Comment from the requested item: <message>


Hi Ulrich,



Could you show some examples, as I can't manage to get it to work propely.



Best regards,