Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to copy both work notes and additional comments in a field

sk59
Tera Expert

Hi All,

We are using OOB Time Worked functionality on incident form So I would like to copy the latest Additional Comments /Work notes entered on Incident to comments field in Time worked table.

I wrote a before insert/update BR

var workNotes = current.task.work_notes.getJournalEntry(1);
var additionalComments = current.task.comments.getJournalEntry(1);
var dateRE = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.*\n/;

if(additionalComments){

 

current.comments = additionalComments.replace(dateRE, '');}
if(workNotes) {

current.comments = workNotes.replace(dateRE, '');
}

 

But it is updating either work notes or comments 

If I first enter comments it is updating comments later if I enter worknotes it is still updating the last comments which i entered previously instead of the lates worknotes.

 

Please suggest where I am going wrong

 

17 REPLIES 17

Hi Udhay,

I entered as below order

1. Additional Comments : comments1

2. Work Notes: notes1

3. Work Notes: notes2

4. Additional COmments : comments2

But the comments got updated as below 

1. comments1

2. notes1

3. notes2

4. notes2 //again the last updated worknotes i.e notes2 instead of the latest additional comment i.e comment2

 

 

find_real_file.png

find_real_file.png

// Try this sample code

var additionalComments = '';
var workNotes = '';
var dateRE = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.*\n/;

if(current.comments.changes){
additionalComments = current.task.comments.getJournalEntry(1);

}else if(current.work_notes.changes){
workNotes = current.task.work_notes.getJournalEntry(1);

}else{
//Do nothing

}

if(additionalComments != ''){
current.comments = additionalComments.replace(dateRE, '');
}

if(workNotes != ''){
current.comments = workNotes.replace(dateRE, '');
}

In this case worknotes is not getting copied only additional comments is getting copied

In which table you have written your business rule?

table :task_time_worked

when :before insert/update

Script:

var additionalComments = '';
var workNotes = '';
var dateRE = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.*\n/;

if(current.task.comments.changes()){
additionalComments = current.task.comments.getJournalEntry(1);

}else if(current.task.work_notes.changes()){
workNotes = current.task.work_notes.getJournalEntry(1);

}else{
//Do nothing

}

if(additionalComments != ''){
current.comments = additionalComments.replace(dateRE, '');
}

if(workNotes != ''){
current.comments = workNotes.replace(dateRE, '');
}