
- 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 03:16 PM
Use this condition instead:
current.comments.changes() && current.comments.getJournalEntry(1).indexOf("Comments added from:") == -1; //Does not have string in comment
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2017 02:42 PM
I second Michael's solution here.
I feel like .setWorkflow() is a bit risky here, and it is better to just trigger based on the user.
Alternatively, you could prefix each comment with 'Comment from TASK' then check for 'Comment DOES NOT CONTAIN "Comment from TASK" to stop the copy from firing the rule again.
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2022 08:38 AM
This worked...Thanks, Michael Fry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2017 02:32 PM
Hi Wesley,
Try using gr.setWorkflow(false) as shown below in both the scripts:
gr.comments = "Comment added from: " + current.number + "\r\n" + "> " + current.comments;
gr.setWorkflow(false)
gr.update();
Regards,
Sagar
PS: Please mark as helpful or correct based on impact.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2017 02:50 PM
Hi Sagar,
It works sort of. It stops the duplication but then it seems to not copy to the 'Activity' stream. Test #1 was with your command and Test #2 without. As you can see Test #1 failed to be copied to the 'Activity' stream but it shows up above which is an odd behavior I trying to figure out. It doesn't occur at the Request or Request Item levels only Task.
-Wesley