- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-12-2017 12:52 PM
I have two Business Rules set up.
One business rule is on the sc_req_item table and copies comments on the RITM record as work Notes on the Task record.
Table: sc_req_item
Runs on: After
Condition: current.comments.changes()
The second business rule is on the sc_task table and copies comments on the Task record to comments on the RITM record.
Table: sc_task
Runs on: After
Condition: current.comments.changes()
This is working fine for the most part. If a user comments on the RITM, the first business rule runs and copies that comment as a work note to the Task record. When a fulfiller eventually types a comment on this Task record, that comment gets sent over to the RITM record as a RITM comment. This is great and the functionality I want.
My problem is that when this second business rule runs and updates the RITM record, my first business rule gets triggered because its condition is waiting for that field to change. This leads to duplicate text on my Task record. I've been trying to fix this but am getting nowhere. Anyone have an idea of what I can do about this?
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-12-2017 11:10 PM
Hi Shant,
You can place a global variable in your business rule to indicate where the update originated (i.e., from which task). This variable will persist only while that particular update is being processed. For example...
For your Business Rule on [sc_task]:
var updateInitiatedBy = updateInitiatedBy || current.sys_id.toString();
(function executeRule(current, previous /*null when async*/) {
if(updateInitiatedBy == current.sys_id.toString()){
var gr = new GlideRecord('sc_req_item');
gr.addQuery('sys_id',current.request_item.sys_id.toString());
gr.query();
while(gr.next()){
gr.comments = current.comments;
gr.update();
}
}
})(current, previous);
For your Business Rule on [sc_req_item]:
var updateInitiatedBy = updateInitiatedBy || current.sys_id.toString();
(function executeRule(current, previous /*null when async*/) {
if(updateInitiatedBy == current.sys_id.toString()){
var gr = new GlideRecord('sc_task');
gr.addQuery('request_item',current.sys_id);
gr.query();
while(gr.next()){
gr.comments = current.comments;
gr.update();
}
}
})(current, previous);
Only the rule which fires initially and sets the variable 'updateInitiatedBy' will initiate a GlideRecord and update the other RITM or TASK record associated with its 'current' record. Also, it is important to make these run as 'Before' business rules (I believe) because you are dealing with Journal Entry fields which will no longer contain a value after the update has been committed to the database.
Give that a try and let me know if you have any questions.
Thanks,
-Brian
Edit: I changed the global variable's name to 'updateInitiatedBy' for the sake of code clarity.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-13-2017 09:33 AM
Thanks for the reply Brian. Unfortunately This doesn't run for me. I don't think its possible to define variables outside of the main business rule function?
The system logs are telling me that the variable is not defined 😕

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-13-2017 12:02 PM
Hi Shant,
I had tested this successfully on my PDI before posting, so I know it works. Please copy/paste your code here so we can take a look.
Thanks,
-Brian
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-13-2017 02:55 PM
Appreciate the help! Here's my code. I commented out 3 lines that implement what you recommended. I just ran some further tests and I noticed that the 1st business rule won't run at all if I put in those 3 lines but the 2nd one will run with or without them.
If I leave both business rules as is below, I get the behavior I mentioned earlier:
1st business rule sends the RITM comment to task worknote, 2nd business rule waits for any Task comment and sends it to RITM comment, and then the 1st business rule gets triggered sending that new comment back to the Task as a work note.
Any ideas would be very appreciated. I've been stuck on this for way too long
Business Rule 1:
Table: sc_req_item
When: Before
Condition: current.comments.changes()
Description: Sends RITM comments to Task work notes
//var updateSelfInitiated = updateSelfInitiated || current.sys_id.toString();
(function executeRule(current, previous /*null when async*/) {
updateWorkNotes();
function updateWorkNotes() {
//if (updateSelfInitated == current.sys_id.toString()){
var task = new GlideRecord('sc_task');
task.addQuery('request_item.number', current.number);
task.addQuery('active', true);
task.query();
while (task.next()) {
var comments = current.comments.getJournalEntry(1);
var taskWorkNotes = task.work_notes.getJournalEntry(1);
if (taskWorkNotes.indexOf(comments)<0) {
task.work_notes = comments;
task.update();
}
}
//}
}
})(current, previous);
Business Rule 2:
Table: sc_task
When: Before
Condition: current.comments.changes()
Description: Sends Task comments to Task comments
/var updateSelfInitiated = updateSelfInitiated || current.sys_id.toString();
(function executeRule(current, previous /*null when async*/) {
updateComments();
function updateComments() {
//if (updateSelfInitiated == current.sys_id.toString()) {
var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('sys_id', current.getValue('request_item'));
ritm.addQuery('active', true);
ritm.query();
if (ritm.next()) {
var comments = current.comments.getJournalEntry(1);
var ritmComments = ritm.comments.getJournalEntry(1);
if (ritmComments.indexOf(comments)<0) {
ritm.comments = comments;
ritm.update();
}
}
//}
}
})(current, previous);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-13-2017 03:13 PM
Hi Shant,
You have a typo, the variable name in your 'if' condition is misspelled ("Initiated" is missing the third 'i') and does not match your var declaration.
...
//if (updateSelfInitated == current.sys_id.toString()){
var task = new GlideRecord('sc_task');
...
Correct this and it should work.
Thanks,
-Brian
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-13-2017 05:32 PM
Wow, beating myself up over that one haha. Thanks a ton Brian! This works perfectly