Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to get journal content in scoped application?

noahstahl
Giga Contributor

I have a scoped application in Fuji with a custom table. This table has a journal field that I would like to read the contents of in script. I've found the getJournalEntry method, but this is denied when run:

"Function getJournalEntry is not allowed in scope my_app"

How do I read the journal field on my table? Thanks,

Noah

3 REPLIES 3

Mike Allen
Mega Sage

Scripting in Scoped Applications - ServiceNow Wiki



Might find something interesting here, particularly the 3.2.3 Calling a Global Script from Another Application Scope piece.


Thanks Mike. I didn't find an equivalent method, but did develop a solution that works by querying the Journal Entry table directly. Each journal entry appears to be there with a reference to the parent record sysid and the journal field name. This function gets the most recent entry:



getLastJournalEntry: function(parentSysId, fieldName){


  // Get most recent record from Journal List table


  var journalRecord = new GlideRecord('sys_journal_field');


  journalRecord.addQuery('element_id', parentSysId);


  journalRecord.addQuery('element', fieldName);


  journalRecord.orderByDesc('sys_created_on');


  journalRecord.query();



  if(journalRecord.next()) {


  return journalRecord.value;


  }


  }



Then, to use:



var lastEntry = this.getLastJournalEntry(parentRecord.sys_id, "log");


This was super-helpful, noahstahl



I adapted it a little bit to behave more like the (forbidden) baseline getJournalEntry() method:


function getJournalEntry (recordId, fieldName, count) {  


  if (gs.nil(count)) { count = 1; }



  var output = [];



  var je = new GlideRecord('sys_journal_field');    


  je.addQuery('element_id', recordId);  


  je.addQuery('element', fieldName);  


  je.orderByDesc('sys_created_on');  


  je.query();



  var index = 0;


  while (je.next() && (count==-1 || index < count) ) {  


  output.push(je.value+'');


  index++;


  }  



  return output;


  }