How to copy work notes and comments except time stamp

AnilM99
Tera Expert

Hi Team,

I have a business rule - Before insert and update to copy all incident updates to other custom table record.

All fields are copying except work notes and comments.

My requirement is 

1. Copy all the work notes and comments except time and user stamp to multi line text field in other custom table.

2. Business rule is triggered on every update so i would like to add if condition for latest work notes and comments.

 

Thanks,

Anil!

1 ACCEPTED SOLUTION

AnilM99
Tera Expert

Hi all thanks for the reply 

I got the solution:

var comment;

var workNotes;

 

if(current.comments != previous.comments){
    comment = current.comments.getJournalEntry(1).match(/\n.*/gm).join("\n");
}
if(current.work_notes != previous.work_notes){
    workNotes = current.work_notes.getJournalEntry(1).match(/\n.*/gm).join("\n");
}

var gr = new GlideRecord('custom_table');
gr.addQuery('number', number);
gr.query();
if(gr._next()){
gr.comments = comment;
gr.work_notes = workNotes;
}
gr.update();
 
Thanks,
Anil!

 

View solution in original post

3 REPLIES 3

Rajesh Chopade1
Mega Sage

hi @AnilM99 

1) Copy all the work notes and comments except time and user stamp:

In the script section of the business rule, add the logic to extract the work notes and comments, excluding timestamps and user information. Here’s an example script:

var customRecord = new GlideRecord('your_custom_table');
customRecord.initialize();

// Copy work notes (excluding timestamps and user information)
var workNotes = current.work_notes.getJournalEntry(1); // Get the latest work note
if (workNotes) {
    customRecord.work_notes_field = workNotes.replace(/^[^:]*:\s*/, ''); // Remove user info
}

// Copy comments (excluding timestamps and user information)
var comments = current.comments.getJournalEntry(1); // Get the latest comment
if (comments) {
    customRecord.comments_field = comments.replace(/^[^:]*:\s*/, ''); // Remove user info
}

// Insert the custom record
customRecord.insert();

 

2) Add condition for latest update:

In the Conditions section, you can use a script to check if there are new updates. To capture only the latest work notes and comments, you can do something like this:

// Check if the current record has changes
if (current.changes() && (current.work_notes.changes() || current.comments.changes())) {
    // Logic to handle the copying of notes
}

 

I hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.

thank you

Rajesh

 

GlideFather
Tera Patron

hi Anil,
Can you share the BR, what does it do exactly and what type it is?
Is it either triggered on [sys_journal_field] table or using that in the script field in case of triggering on another table?

Also, regarding the point 2. when a comment or a work note is added and saved it will still be triggered I guess, there is no latest comment, the added comment will be always the latest one... or what am I missing? 

———
/* If my response wasn’t a total disaster ā†™ļø ⭐ drop a Kudos or Accept as Solution āœ… ā†˜ļø Cheers! */


AnilM99
Tera Expert

Hi all thanks for the reply 

I got the solution:

var comment;

var workNotes;

 

if(current.comments != previous.comments){
    comment = current.comments.getJournalEntry(1).match(/\n.*/gm).join("\n");
}
if(current.work_notes != previous.work_notes){
    workNotes = current.work_notes.getJournalEntry(1).match(/\n.*/gm).join("\n");
}

var gr = new GlideRecord('custom_table');
gr.addQuery('number', number);
gr.query();
if(gr._next()){
gr.comments = comment;
gr.work_notes = workNotes;
}
gr.update();
 
Thanks,
Anil!