
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2017 01:34 PM
Hello,
Need help in how address duplication between scripts. Basically what I am trying to do is establish communication between the User entering comments from Service Portal > Request Item page and the Technician working on the request items Task comments. So when 'additional comments' are added at the Task level they get copied up to the Request Items 'comments' field making the comments available to the User out in the Service Portal. Then if the user needs to respond back then his/her comments from the Request Item comments field get copied to the Task comments field. My issue is that my copy rules execute when there is a change to either comments field; task or request item. Hence, then a duplication occurs and the comments get copied back to the same record. Surprisingly the loop occurs only once where I suppose the logic could create an endless loop of copying comments back and forth. Hope this makes sense?
I am struggling how to establish a condition if the comments come from either copy script not to execute, but obviously execute if comments are added by the user. Can you assist me with how I can resolve this issue?
BR: Copy Comments to Request Item [ condition = current.comments.changes() ]
(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord('sc_req_item');
gr.addQuery('sys_id', current.request_item.sys_id);
gr.query();
while(gr.next()) {
gr.comments = "Comment added from: " + current.number + " | " + current.short_description + "\r\n" + "> " + current.comments;
gr.update();
}
})(current, previous);
BR: Copy Comments to Task(s) [ condition = current.comments.changes() ]
(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord('sc_task');
gr.addQuery('request_item', current.sys_id);
gr.query();
while(gr.next()) {
gr.comments = "Comment added from: " + current.number + "\r\n" + "> " + current.comments;
gr.update();
}
})(current, previous);
Thank you!
-Wesley
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2017 01:59 PM
While we do push comments from SCTask to RITM (so Requester gets notified), we push Comments from RITM to Worknotes on SCTask but we check if the updater is the same person assigned to the SCtask. If they are, we don't send the comments to SCtask. Here's the script we use to check:
var ritmupdater = gs.getUserID();
var gr = new GlideRecord('sc_task');
gr.addQuery('request_item',current.sys_id);
gr.query();
while(gr.next()){
//if RITM is updated by SCTASK assigned to, don't push comments to work notes
if (gr.assigned_to != ritmupdater)
gr.work_notes = current.comments;
gr.update();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2017 02:56 PM
Hi Wesley,
As said by Paul, it is risky to use it here. As it stops the other Business rules from running. May be because of that it is not working correctly.
Regards,
Sagar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2017 06:58 PM
Hi Wesley,
I find using setWorkflow(false) too extreme for something like this, you would then always have to account for it when creating/updating business rules in the future. I found that a better way to prevent business rules from creating an update loop is to add a global variable (outside of the BR execute function) that persists during that update cycle. The variable tracks which record spawned the update and can be checked in your logic accordingly.
Here's an example using your business rule Copy Comments to Request Item for the scenario sharing comments:
var updateInitiatedBy = updateInitiatedBy || current.sys_id.toString();
(function executeRule(current, previous /*null when async*/) {
if(updateInitiatedBy != current.sys_id.toString()){
return; //(i.e., if this task didn't initiate the update, don't push the changes in Comments)
}
var gr = new GlideRecord('sc_req_item');
gr.addQuery('sys_id', current.request_item.sys_id);
gr.query();
while(gr.next()) {
gr.comments = "Comment added from: " + current.number + " | " + current.short_description + "\r\n" + "> " + current.comments;
gr.update();
}
})(current, previous);
And since the operation you're performing is essentially symmetric, your other BR would look the same except for the table and field names used in your GlideRecord. Let me know if you have any questions or issues.
Thanks,
-Brian

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2017 07:55 PM
Thanks to Michael, Paul and Brian!
Three brilliant solutions. I gave credit to Michael because his solution was first, but I implemented Paul's because it was the easiest and I thought it works better with my current script. But I have to say Brian's seems to make the most sense globally and seems more like a 'best practice' method. Don't want to start a debate, but really, really, really want to say how much I appreciate you three offering such great solutions. I am still learning and these three examples will be definitely added to my library of solutions. Love this Community
Thanks again!
-Wesley
P.S. I found out why the comments were duplicating. Someone had added the "Comments and Work Notes" field above the vs. the Additional Comments>Work Notes>Activities (filtered) fields that provide integrated behavior.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2022 09:12 AM
Can you please share your updated script that you used I added the condition by paul but not copying my comments from case to change
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2022 09:12 AM
(function executeRule(current, previous /*null when async*/ ) {
var grr = new GlideRecord('change_request');
grr.addQuery('sys_id', current.change); // this is the field name where change number is getting saved in case record
grr.query();
if(grr.next()) {
current.comments.changes() && current.comments_and_work_notes.getJournalEntry(1).indexOf("Comments added from:") == -1; //Does not have string in comment
grr.update();
}
})(current, previous);