How to get last comment from sys_journal_field?

cflloyd
Kilo Explorer

I'm trying to get the last comment of a current problem from the sys_journal_field table to avoid getting header (timedate stamp, etc.) information. I'm using the function below in a business rule.   I thought if I ordered descending and set the limit to one, I would get the most recent comment. However I'm not getting the most recent comment nor the oldest but one in between. What am I doing wrong? Thanks.

function resolveRelatedIncidents(me) {

  //Get comments from problem through sys_journal_field in order to avoid duplicate header information when copied to incident.

  var gr = new GlideRecord('sys_journal_field');

  gr.addQuery('element_id', me.sys_id);

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

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

  gr.orderbyDesc('sys_created_on');

  gr.setLimit(1);

  gr.query();

  while (gr.next()) {

  //Store last comment in variable

      var lastComment = gr.value.toString();

  }

3 REPLIES 3

coryseering
ServiceNow Employee
ServiceNow Employee

Hi Carol,



You need to capitalize the "b" in OrderByDesc:


  gr.orderByDesc('sys_created_on');



orderbyDesc is not a valid method (method namess are case-sensitive).


Thanks Cory. I was hoping it was that easy. Sometimes you just need a second set of eyes.


Kalaiarasan Pus
Giga Sage

And this can be replaced with a 'if condition'


while (gr.next()) {