How to get individual values of data from most recent comment on an incident ticket

JC S_
Mega Guru

We are trying to create a widget for Service Portal in which we need to evaluate the sys_created_on, created_by and value of the most recent comment on an incident. So far we've only come up with the following:

data.recentcomment = gr.comments.getJournalEntry(1);

But this outputs a single line containing all the values as a string.

We are looking into a way to use glide record on the journal entry and get the individual values for the sys_created_on, created_by and comment value itself on the Server Side Script and pass it on a variable.

1 ACCEPTED SOLUTION

JC S_
Mega Guru

Thanks everyone, upon further research I was able to extract the individual comment data values using the following code:



// Check Latest Comment


var cm = new GlideRecord('sys_journal_field');


  cm.addQuery('element_id', data.sys_id);


  cm.addQuery('name', 'task');


  cm.addQuery('element', 'comments');


  cm.orderByDesc('sys_created_on');


  cm.setLimit(1);


  cm.query();


  while (cm.next()) {


      data.cmvalue = cm.value.toString();


      data.cmdate = cm.sys_created_on.toString();


      data.cmby = cm.sys_created_by.toString();


  }



Now you can just use data.cmvalue, data.cmby and data.cmdate anywhere on your code or output it on HTML.


View solution in original post

4 REPLIES 4

antin_s
ServiceNow Employee
ServiceNow Employee

HI Jimboy,



You may use 'sys_journal_field' table which stores all the comments/journal entries.



Hope this helps. Mark the answer as correct/helpful based on impact.



Thanks


Antin


Sujata Vishwak1
Mega Expert

HI Jimboy,



You can check with the below script.



var comment = new GlideRecord("sys_journal_field");


      comment.orderBy('sys_created_on');


      comment.query();


      if(comment.next()) {


              return comment.sys_created_on;   //First comment added Timestamp


      }//end of journal Query



Instead of OrderBy you can use 'OrderByDesc" to get the latest comment



Regards,


Sujata



Mark the answer as correct/helpful based on impact.


This returned no value. We are adding this on a widget that is to be placed on the ticket page on Service Portal for reference.


JC S_
Mega Guru

Thanks everyone, upon further research I was able to extract the individual comment data values using the following code:



// Check Latest Comment


var cm = new GlideRecord('sys_journal_field');


  cm.addQuery('element_id', data.sys_id);


  cm.addQuery('name', 'task');


  cm.addQuery('element', 'comments');


  cm.orderByDesc('sys_created_on');


  cm.setLimit(1);


  cm.query();


  while (cm.next()) {


      data.cmvalue = cm.value.toString();


      data.cmdate = cm.sys_created_on.toString();


      data.cmby = cm.sys_created_by.toString();


  }



Now you can just use data.cmvalue, data.cmby and data.cmdate anywhere on your code or output it on HTML.