Copy comments and worknotes from incident task to Incident
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2025 04:54 AM - edited 04-01-2025 04:55 AM
Hi All,
I want copy comments and work notes from incident task to incident. I have written business rules script is working fine but my issue is incident have multiple task, if user add comments in one task its has to copy in incident, but its coping in all incident tasks and incident. Please help me if user add comments or worknotes is should copy in incident not for the other child tasks.
Business rule Script :
When to Run : After, Update
Conditions : Additional comments changes || Worknotes changes
Script :
(function executeRule(current, previous /*null when async*/ ) {
updateIncident();
function updateIncident() {
var compare, task_comment2, task_comment, incident_comment2, incident_comment;
task_comment = current.comments.getJournalEntry(1); // Additional Comments
var task_worknote = current.work_notes.getJournalEntry(1); // Work Notes
// Remove timestamp and name from Additional Comments
task_comment2 = removeTimestamp(task_comment);
var task_worknote2 = removeTimestamp(task_worknote);
var incident_gr = new GlideRecord('incident');
incident_gr.addQuery('sys_id', current.incident);
incident_gr.addQuery('active', true);
incident_gr.query();
if (incident_gr.next()) {
incident_comment = incident_gr.comments.getJournalEntry(1);
var incident_worknote = incident_gr.work_notes.getJournalEntry(1);
// Remove timestamp from existing comments and work notes
incident_comment2 = removeTimestamp(incident_comment);
var incident_worknote2 = removeTimestamp(incident_worknote);
// Sync Additional Comments
if (task_comment2 && (!incident_comment2 || incident_comment2.indexOf(task_comment2) == -1)) {
incident_gr.comments = task_comment2.trim();
}
// Sync Work Notes
if (task_worknote2 && (!incident_worknote2 || incident_worknote2.indexOf(task_worknote2) == -1)) {
incident_gr.work_notes = task_worknote2.trim();
}
incident_gr.update();
}
}
function removeTimestamp(comment) {
if (!comment) return "";
var regex = new RegExp('\n');
var i = comment.search(regex);
return i > 0 ? comment.substring(i + 1, comment.length) : comment;
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2025 05:37 AM
Hello @suresh40
Use below 👇 script -
(function executeRule(current, previous /*null when async*/) {
syncCommentsToIncident();
function syncCommentsToIncident() {
if (!current.incident) {
return; // Exit if no linked Incident
}
var incidentGR = new GlideRecord('incident');
if (!incidentGR.get(current.incident.toString()) || !incidentGR.active) {
return; // Exit if the linked Incident is not found or inactive
}
// Get the latest Additional Comment & Work Note from Incident Task
var taskComment = removeTimestamp(current.comments.getJournalEntry(0));
var taskWorknote = removeTimestamp(current.work_notes.getJournalEntry(0));
// Get the latest Additional Comment & Work Note from Incident
var incidentComment = removeTimestamp(incidentGR.comments.getJournalEntry(0));
var incidentWorknote = removeTimestamp(incidentGR.work_notes.getJournalEntry(0));
var updated = false;
// Sync Additional Comments
if (taskComment && (!incidentComment || !incidentComment.includes(taskComment))) {
incidentGR.comments.setJournalEntry(taskComment.trim());
updated = true;
}
// Sync Work Notes
if (taskWorknote && (!incidentWorknote || !incidentWorknote.includes(taskWorknote))) {
incidentGR.work_notes.setJournalEntry(taskWorknote.trim());
updated = true;
}
if (updated) {
incidentGR.update(); // Update only if there's a new comment or work note
}
}
function removeTimestamp(comment) {
if (!comment) return "";
var regex = /\n/;
var i = comment.search(regex);
return i > 0 ? comment.substring(i + 1).tr
im() : comment;
}
})(current, previous);
Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket.
Regards,
Shivalika
My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194
My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2025 05:39 AM
Hi @suresh40 ,
Please refer to this Solution :https://www.servicenow.com/community/developer-forum/copy-comments-and-worknotes-from-task-to-incide...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2025 05:44 AM
your script is querying incident and not incident_task so your script is not the culprit.
Seems when you are updating it on INC, some other after update BR is updating it to the child tasks.
are you syncing comments and work notes between INC and INC Tasks?
if yes then it's going in recursion
check this link and response from Sravani and use the script so that it avoids the duplicate comments and work notes
Sync'ing RITM and SCTASK work notes \ additional comments
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader