sys_journal_field.value - conditionally-returns null/undefined

taarik_rahaman
Tera Expert

Hi folks,

I replicated this on a customer's Istanbul instance as well as on my own Developer instance from within a Business Rule and running as a Background Script:

var current = new GlideRecord('sc_req_item');

      //current.get('number', 'RITM0011547');

      current.get('number', 'RITM0010001');

      var sys_journal_field = new GlideRecord('sys_journal_field');

    sys_journal_field.addQuery('name', 'task');
    sys_journal_field.addQuery('element_id', current.sys_id);
    sys_journal_field.addQuery('element', 'comments');
    sys_journal_field.addQuery('sys_created_by', gs.getUserName());
    sys_journal_field.orderByDesc('sys_created_on');
    sys_journal_field.query();

     

      var commentText = sys_journal_field.value;

//var commentText = sys_journal_field.getValue('value');

//var commentText = sys_journal_field.getDisplayValue();

//var commentText = sys_journal_field.value + '';

     

      if(sys_journal_field.next()) {

   
    gs.print('Comment:   ' + sys_journal_field.value, 'RITM comment BizRule');
    gs.print('Formatted Comment:   ' + commentText, 'RITM comment BizRule');

      }

The text in green shows my comment, whereas the text in red shows "undefined" or "null" depending on how I tried to set the commentText variable.

Anyone run into this before?   Any ideas?

Thank you!

1 ACCEPTED SOLUTION

Community Alums
Not applicable

I just want to make sure I understand the requirements:



Instead of the BR firing an event, which triggers a notification, you want the comment to be added to the catalog tasks beneath the RITM? Or do you want to do both?



I would probably leave the BR as is, and create a new one which will copy the comments from the RITM down to each sc_task beneath it. Something like this:



Table: sc_req_item
when: after


filter conditions: active is true and additional comments changes


script conditions: gs.getUserID() == current.opened_by || gs.getUserID() == current.request.requested_for



(function executeRule(current, previous /*null when async*/) {


// Regex to strip header info from journal entries


var headerRE = /.+\n/;


// Get the latest comment in the record; strip out the header


var sComment = "Comment added from " + current.number + "\n\n";


sComment += current.comments.getJournalEntry(1).replace(headerRE, '');


var oTask = new GlideRecord("sc_task");


//oTask.addActiveQuery();


oTask.addQuery("request_item", current.sys_id);


oTask.addActiveQuery();


oTask.query();


while(oTask.next()) {


//gs.log("TM===>Updating " + oTask.number, "Q: Cascade Comments BR");


oTask.comments = sComment;


oTask.update();


}


})(current, previous);



Any comments added to the sc_task record should appear on the activity log.



Cheers,



Tim


View solution in original post

13 REPLIES 13

Community Alums
Not applicable

I just want to make sure I understand the requirements:



Instead of the BR firing an event, which triggers a notification, you want the comment to be added to the catalog tasks beneath the RITM? Or do you want to do both?



I would probably leave the BR as is, and create a new one which will copy the comments from the RITM down to each sc_task beneath it. Something like this:



Table: sc_req_item
when: after


filter conditions: active is true and additional comments changes


script conditions: gs.getUserID() == current.opened_by || gs.getUserID() == current.request.requested_for



(function executeRule(current, previous /*null when async*/) {


// Regex to strip header info from journal entries


var headerRE = /.+\n/;


// Get the latest comment in the record; strip out the header


var sComment = "Comment added from " + current.number + "\n\n";


sComment += current.comments.getJournalEntry(1).replace(headerRE, '');


var oTask = new GlideRecord("sc_task");


//oTask.addActiveQuery();


oTask.addQuery("request_item", current.sys_id);


oTask.addActiveQuery();


oTask.query();


while(oTask.next()) {


//gs.log("TM===>Updating " + oTask.number, "Q: Cascade Comments BR");


oTask.comments = sComment;


oTask.update();


}


})(current, previous);



Any comments added to the sc_task record should appear on the activity log.



Cheers,



Tim


Thanks Tim!   I much prefer avoiding having to parse the header out, but it is what it is.



By the way, should I log a HI Incident regarding the weird behavior of the sys_journal_field.value field in the GlideRecord?


Community Alums
Not applicable

You can leave the header in if you like, but then the string text would include the date and user



If it's not working as it should, then I would for sure open a ticket, yes. Could be a bug in the version of SN you are running. I don't think getters and setters work with journal fields, though there may be another way to achieve that functionality.



Cheers,



Tim


Community Alums
Not applicable

I took another look at this, as I was curious. I made a small change, which now sets the 'commentText' variable correctly:



var sys_journal_field = new GlideRecord('sys_journal_field');


//sys_journal_field.addQuery('name', 'task');


sys_journal_field.addQuery('element_id', current.sys_id);


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


sys_journal_field.addQuery('sys_created_by', gs.getUserName());


sys_journal_field.orderByDesc('sys_created_on');


sys_journal_field.query();



//var commentText = sys_journal_field.value;


//var commentText = sys_journal_field.getValue('value');


//var commentText = sys_journal_field.getDisplayValue();


//var commentText = sys_journal_field.value + '';



if (sys_journal_field.next()) {


      var commentText = sys_journal_field.value;


      gs.print('Comment:   ' + sys_journal_field.value, 'RITM comment BizRule');


      gs.print('Formatted Comment:   ' + commentText, 'RITM comment BizRule');


}




I moved the variable declaration inside the while loop. Now both statements print the value correctly. Not sure if that helps now



Cheers,



Tim