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

Raju Koyagura
Tera Guru

I don't think we will get the value of queried GlideRecord until we processed using next() or hasNext() methods


var commentText = sys_journal_field.value; //this will always gives undifined



   


     


Thanks Raju.   sys_journal_field.next() is being used in an if statement in the code I posted.


Prateek Gupta3
Mega Guru

Hi Taarik



Instead of gs.print() give gs.log()



Thanks


Prateek Gupta


Thanks Prateek.   I used gs.log in the Business Rule and gs.print in the background script.