How Does One Prevent Duplicate Comments?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2022 08:22 PM
We have Table Square (SQ) and Table Star (ST). Let's say we have two records SQ0001 and ST0023; they both reference to each other. When a user submits a comment in SQ0001, it should then add that comment to ST0023. A comment that is submitted from ST0023 should also apply to SQ0001.
I've created two Business Rules. One on Table Square and the other on Table Star.
// Business rule in Table Square
(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord('star');
gr.addQuery('square', current.sys_id);
gr.query();
if (gr.next()) {
var text = current.comments.getJournalEntry(1).split('comment)'));
gr.comments = text[1].trim();
gr.update();
}
})(current, previous);
// Business rule in Table Star
(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord('square');
gr.addQuery('star', current.sys_id);
gr.query();
if (gr.next()) {
var text = current.comments.getJournalEntry(1).split('comment)'));
gr.comments = text[1].trim();
gr.update();
}
})(current, previous);
I've been able to get a version similar to this working as a "before" business rule with no duplicated comments. However, my understanding of business rules compels me that an "after" business rule should be used instead. After all, isn't it better practice and performance to update another table's comments using an "after" business rule. Unfortunately doing an "after" business rule creates a duplicate comments and I haven't found the right solution yet.
Is it better to do an "after" business rule and if so, how can it be done in the situation described? I do understand the inherit problem that a comment is coming in both directions and I'm looking for a solution to handle this.
Note that .setworkflow(false) does not resolve the problem.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-05-2022 03:54 AM
We have 2 Business Rules running after Update with the Filter Condition Additional comments changes on the sc_task and sc_req_item tables to copy comments from Catalog Tasks to its RITM and vice-versa. The key to not duplicating comments and preventing recursion is searching for the existing comment. Here's are scripts that are working to do that. There's a bit more to it to make sure it also works when there aren't yet any comments on the target record, and we add the record number as a prefix to prevent confusion on how that comment appeared.
This is the script on the sc_task table:
(function executeRule(current, previous /*null when async*/) {
//to copy the Additional Comments from SCTASK to RITM
var compare = '';
var task_comment2 = '';
var task_comment = '';
var ritm_comment2 = '';
var ritm_comment = '';
task_comment = current.comments.getJournalEntry(1);
//Remove timestamp and name from additional comment
var regex= new RegExp('\n');
var i = task_comment.search(regex);
if (i>0) {
task_comment2 = task_comment.substring(i+1, task_comment.length);
}
var ritm_gr = new GlideRecord('sc_req_item');
ritm_gr.addQuery('sys_id', current.request_item);
ritm_gr.query();
if(ritm_gr.next()) {
ritm_comment =ritm_gr.comments.getJournalEntry(1);
//Remove timestamp and name from additional comment
var i1 = ritm_comment.search(regex);
if(i1 > 0) {
ritm_comment2 = ritm_comment.substring(i1+1, ritm_comment.length);
var numberstring = current.number + ' - ';
var regex2= new RegExp(numberstring);
var i2 = ritm_comment2.search(regex2);
if(i2>=0) {
ritm_comment2 = ritm_comment2.substring(i2+numberstring.length, ritm_comment2.length);
}
} else {
ritm_comment2 = 'empty';
}
compare = task_comment2.indexOf(ritm_comment2);
if(compare == -1) { // If No match found
ritm_gr.comments = current.number + ' - ' + task_comment2.trim();
ritm_gr.update();
}
}
})(current, previous);
And the one on the sc_req_item table:
(function executeRule(current, previous /*null when async*/) {
//To Copy the Additional Comments from RITM to SCTASKs
var compare = '';
var ritm_comment2 = '';
var ritm_comment = '';
var task_comment2 = '';
var task_comment = '';
ritm_comment = current.comments.getJournalEntry(1);
//Remove timestamp and name from additional comment
var regex= new RegExp('\n');
var i = ritm_comment.search(regex);
if (i>0) {
ritm_comment2 = ritm_comment.substring(i+1, ritm_comment.length);
}
var task_gr = new GlideRecord('sc_task');
task_gr.addQuery('request_item', current.sys_id);
task_gr.addQuery('active', 'true');
task_gr.query();
while (task_gr.next()) {
task_comment = task_gr.comments.getJournalEntry(1);
//Remove timestamp and name from additional comment
var i1 = task_comment.search(regex);
if(i1 > 0) {
task_comment2 = task_comment.substring(i1+1, task_comment.length);
var numberstring = current.number + ' - ';
var regex2= new RegExp(numberstring);
var i2 = task_comment2.search(regex2);
if(i2>=0) {
task_comment2 = task_comment2.substring(i2+numberstring.length, task_comment2.length);
}
} else {
task_comment2 = 'empty';
}
compare = ritm_comment2.indexOf(task_comment2);
if (compare == -1) {// if no match found
task_gr.comments = current.number + ' - ' + ritm_comment2.trim();
task_gr.update();
}
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-05-2022 08:58 AM
Hi Brad,
Really appreciate the super helpful reply!
Would your code be able to handle a rare situation that the client would comment the exact same comment twice? For example, the client comments "Need 10 items". 1 week later, the client notices no one replied and then added another comment "Need 10 items".
We would expect the associated record to contain the first "Need 10 items" comment and the second "Need 10 items" comment coming in from the original record.
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-08-2022 10:47 AM
We were having some sporadic duplication before stripping out the date/time stamp inherent in the comment, so with the code as is, the same comment entered twice on the source record would only be copied once to the target record - whether this is done seconds or days... apart - provided there were no comments in-between since it's only checking the most recent journal entry for a possible duplicate.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-20-2025 10:26 AM
Works like a charm.