- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2023 10:58 AM - edited 09-06-2023 11:01 AM
Hi,
I'm trying to solve issue with copying comments from HR Case to HR Task and vice versa. My requirement is to copy comments (not work notes).
I'm using business rules on after (update) but when I add comment in HR Case
The problem arises when a comment is added to the HR Case, which is then copied to the HR Task as intended. However, this action of copying the comment to the HR Task triggers the Business Rule again I think and causing the same comment to be copied back to the HR Case. This results in the comment being duplicated in the HR Case. The same is when I add comment in HR Task.
I have checked this community article but I haven't found solution. They provided solution but it was related to copying work notes and commenting OOB script (I don't want to modify anything that is OOB).
Any suggestions what should be changed in scripts?
Business rule: Copy comment from Case to Task
var hrTask = new GlideRecord('sn_hr_core_task');
hrTask.addQuery('parent', current.sys_id);
hrTask.query();
if (hrTask.next()) {
var comment = current.comments.getJournalEntry(1);
hrTask.comments = comment;
hrTask.update();
}
Business rule: Copy comments from Task to Case
var hrCase = new GlideRecord('sn_hr_core_case');
hrCase.addQuery('sys_id', current.parent);
hrCase.query();
if (hrCase.next()) {
var comment = current.comments.getJournalEntry(1);
hrCase.comments = comment;
hrCase.update();
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2023 11:51 AM
We've been doing this for years, with a couple iterations on approaches tweaked from examples found in the Community, so it's tried and true by this point accounting for the target record not having any existing comments, and checking the most recent entry to precent recursion. This example uses the Requested Item (sc_req_item) and Catalog Task (sc_task) tables, but will operate exactly like the sn_hr_core_case and the sn_hr_core_task tables. On each rule you'll want to add a Filter Condition, so that the rule only runs when the record is updated with a comment:
On the 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';
}
var numberstring2 = ritm_gr.number + ' - ';
var regex3= new RegExp(numberstring2);
var i3 = task_comment2.search(regex3);
if(i3>=0) {
task_comment2 = task_comment2.substring(i3+numberstring2.length, task_comment2.length);
}
compare = task_comment2.indexOf(ritm_comment2);
if(compare != 0) { // If no exact entire match found
ritm_gr.comments = current.number + ' - ' + task_comment2.trim();
ritm_gr.update();
}
}
})(current, previous);
On the case 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';
}
var numberstring2 = task_gr.number + ' - ';
var regex3= new RegExp(numberstring2);
var i3 = ritm_comment2.search(regex3);
if(i3>=0) {
ritm_comment2 = ritm_comment2.substring(i3+numberstring2.length, ritm_comment2.length);
}
compare = ritm_comment2.indexOf(task_comment2);
if (compare != 0) {// 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
09-06-2023 11:51 AM
We've been doing this for years, with a couple iterations on approaches tweaked from examples found in the Community, so it's tried and true by this point accounting for the target record not having any existing comments, and checking the most recent entry to precent recursion. This example uses the Requested Item (sc_req_item) and Catalog Task (sc_task) tables, but will operate exactly like the sn_hr_core_case and the sn_hr_core_task tables. On each rule you'll want to add a Filter Condition, so that the rule only runs when the record is updated with a comment:
On the 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';
}
var numberstring2 = ritm_gr.number + ' - ';
var regex3= new RegExp(numberstring2);
var i3 = task_comment2.search(regex3);
if(i3>=0) {
task_comment2 = task_comment2.substring(i3+numberstring2.length, task_comment2.length);
}
compare = task_comment2.indexOf(ritm_comment2);
if(compare != 0) { // If no exact entire match found
ritm_gr.comments = current.number + ' - ' + task_comment2.trim();
ritm_gr.update();
}
}
})(current, previous);
On the case 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';
}
var numberstring2 = task_gr.number + ' - ';
var regex3= new RegExp(numberstring2);
var i3 = ritm_comment2.search(regex3);
if(i3>=0) {
ritm_comment2 = ritm_comment2.substring(i3+numberstring2.length, ritm_comment2.length);
}
compare = ritm_comment2.indexOf(task_comment2);
if (compare != 0) {// 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
09-07-2023 01:55 AM
Hi @Brad Bowman
your solution works like a charm, many thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2023 05:06 AM
You are welcome!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2025 11:42 AM
just use \n will give error
NOTE:\\n: In JavaScript, the backslash \ is an escape character, so you need to escape the backslash itself by using \\. This ensures that the regex interprets it correctly as a newline character.