current.work_notes.getJournalEntry(-1); returns an empty string

gsmallridge
Kilo Contributor

i tried the code from the documentation:

 

notes = current.work_notes.getJournalEntry(-1);
      var na = notes.split("\n\n");                                     //stores each entry into an array of strings

       

      for (var i = 0; i < na.length; i++)                      

          gs.print(na[i]);

7 REPLIES 7

Gary, I took your code and added in the time, user name, and field name which makes it closer to the getJournalEntry function:



/**


* Gets all journal entries for this incident.


*


* @param incidentSysId


* @returns {String} - each journal entry is separated by a linefeed pair '\n\n'.


*/


function getActivity( incidentSysId )


{


      var journalString = "";


      var actObject = new GlideRecord('sys_journal_field');


      actObject.addQuery('element_id', incidentSysId);


      gs.info("incidentSysId: " + incidentSysId);


   


      actObject.query();


      journalString = '';


   


      while( actObject.next() ) {


           


              journalString += actObject.sys_created_on + ' - ' +


                      actObject.sys_created_by + ' (' + actObject.element + ')\n' +


                      actObject.value + '\n\n';


           


      }


   


      return journalString;


}


seeni
Giga Contributor

Hi,

 

getJournalEntry(-1) -- For the all journal entries,, returns a string that contains the field label, timestamp, and user display name of the journal entries ever entered as a single string with each entry delimited by "\n\n".

I want to display only timestamp and journalfield text. 

I don't want to display user display name in the entries.

Can any one please reply for the above case. Thanks.

 

 

Since what you are wanting to do is change the contents of the string for the different journal fields, we can use an extra function that will set the journal field to your liking.

I have typically set the journal field to change the new lines to <br/> tags, but this can be leveraged for your purposes as well.

var notes = current.work_notes.getJournalEntry(-1);
var na = notes.split("\n\n");             //stores each entry into an array of strings
for (var i = 0; i < na.length; i++) {
    gs.print(na[i]);                      //This shows what the array slice looks like before
    var newNote = setJournalString(na[i]);
    gs.print(newNote);                    //This shows what the converted string looks like after
}

/**
 * Strips the first line of a journal entry to only include the date/time
 * @param      {string}  jString  The original journal entry
 * @return     {string}  The converted journal entry
 */
function setJournalString(jString) {
        var regex = /^\d+-\d+-\d+ \d+:\d+:\d+( .+)/gm;
        var match = regex.exec(jString);
        if (match != null) {
            var result = jString.replace(match[1], '');
            return result;
        }
}

What the regex variable does is look for a date and time string from the start of the line.

What is in parenthesis (.+) is what is after the date and time string. We capture that so we can use it in our replace method.

I hope this is helpful.