How To get Latest Comment On incident.

Rehan7
Kilo Contributor

i want get latest comment on incident form. when incident state is In Progress and additional comment Changes.

Using Business rule GlideRecord.

1. Create Business Rule - On before update

state - In Progress.

And 

Additional Comment Change

Script -:

var gr = new GlideRecord('sys_journal_field');
gr.addEncodedQuery('element=comments');
gr.addQuery('element_id', current.sys_id);
gr.addQuery('sys_created_by', '!=', current.caller_id);

gr.query();
if (gr.next()) {
current.setValue('description', gr.value);
}

 

 

7 REPLIES 7

Viraj Hudlikar
Giga Sage

Hi @Rehan 

In your BR just add below line to get latest comments or worknotes as per your requirement

current.description=current.comments.getJournalEntry(1);

current.description=current.work_notes.getJournalEntry(1);

 

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

update as this and it should work fine to get latest comments

var fieldName = 'comments';

var rec = new GlideRecord('sys_journal_field');
rec.orderByDesc('sys_created_on');
rec.addQuery('name', current.getTableName());
rec.addQuery('element', fieldName);

gr.addQuery('sys_created_by', '!=', current.caller_id.user_name);
rec.addQuery('element_id', current.sys_id);
rec.setLimit(1);
rec.query();
if(rec.next()){

current.setValue('description', rec.value);

}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Rehan 

Hope you are doing good.

Did my reply answer your question?

If so, please mark appropriate response as correct & helpful so that the question will appear as resolved for others who may have a similar question in the future.

Thanks!
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Rehan 

Thank you for marking my response as helpful.

If my response helped you please mark it correct to close the question so that it benefits future readers as well.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader