- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2024 10:23 PM
Hello Team,
Requesting logic for the task below.
When a user comments in the child incident, it should get copied to its parent incident -
I wrote a AFTER+UPDATE BR script, But its copying the whole comment section of the child to parent. Even if I add Query to Journal it will take all the elements for the current sys_id. That logic will not work as well.
What am I missing?
My script below -
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2024 11:15 PM
Hi @Mayank Mandhana ,
I can see few issues in the script.
1. You have not specified what is the latest comment that should flow, currently its putting the entire data.
You should use the below script to get only the latest comment-
current.comments.getJournalEntry(1) fetches the most recent journal entry (the new comment).
2. You don't need query the incident number instead try using the sys id of the parent that is stored in child record as its a reference field.
Now to summarize the above-
can you please try the below script-
(function executeRule(current, previous /*null when async*/) {
// Check if comments were added or updated
if (current.comments.changes()) {
var newComment = current.comments.getJournalEntry(1); // Get the latest comment
var parentInc = current.parent_incident; // Get the parent incident
if (parentInc) {
var gr = new GlideRecord('incident');
if (gr.get(parentInc)) { // Retrieve the parent incident record using the sys_id of the parent_incident field
gr.comments = gr.comments + '\n' + newComment; // Append the new comment from the child incident
gr.update(); // Save the parent incident with the updated comments
}
}
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2024 11:15 PM
Hi @Mayank Mandhana ,
I can see few issues in the script.
1. You have not specified what is the latest comment that should flow, currently its putting the entire data.
You should use the below script to get only the latest comment-
current.comments.getJournalEntry(1) fetches the most recent journal entry (the new comment).
2. You don't need query the incident number instead try using the sys id of the parent that is stored in child record as its a reference field.
Now to summarize the above-
can you please try the below script-
(function executeRule(current, previous /*null when async*/) {
// Check if comments were added or updated
if (current.comments.changes()) {
var newComment = current.comments.getJournalEntry(1); // Get the latest comment
var parentInc = current.parent_incident; // Get the parent incident
if (parentInc) {
var gr = new GlideRecord('incident');
if (gr.get(parentInc)) { // Retrieve the parent incident record using the sys_id of the parent_incident field
gr.comments = gr.comments + '\n' + newComment; // Append the new comment from the child incident
gr.update(); // Save the parent incident with the updated comments
}
}
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2024 12:17 AM
Hello @Mayank Mandhana ,
You can use the below script to copy the work notes from child to parent incident.
(function executeRule(current, previous /*null when async*/) {
var parentIncident = current.parent_incident;
if(current.comments.changes()){
var inc = new GlideRecord("incident");
inc.addQuery("parent_incident",current.sys_id);
inc.query();
if(inc.next()){
var Comments = current.comments.getJournal(1); // To get the latest entry.
var parnetInc = new GlideRecord("incident");
parentInc.addQuery("sys_id",parentIncident);
parentInc.query();
if(parentInc.next()){
parentInc.comments=Comments;
}
}
}
})(current, previous);
Please accept my solution and give thumbs up if it helps you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2024 12:19 AM
Hello @Mayank Mandhana ,
You can use the below code to achieve your requirement.
(function executeRule(current, previous /*null when async*/) {
var parentIncident = current.parent_incident;
if(current.comments.changes()){
var inc = new GlideRecord("incident");
inc.addQuery("parent_incident",current.sys_id);
inc.query();
if(inc.next()){
var Comments = current.comments.getJournal(1); // To get the latest entry.
var parnetInc = new GlideRecord("incident");
parentInc.addQuery("sys_id",parentIncident);
parentInc.query();
if(parentInc.next()){
parentInc.comments=Comments;
}
parentInc.update();
}
}
})(current, previous);
Please mark my answer as accepted solution and give thumbs up, if it helps you.