DEV Community

Cover image for Working with JournalEntry Fields
Javier Arroyo
Javier Arroyo

Posted on

Working with JournalEntry Fields

Sometimes there is a need to work with JournalEntry fields in script. OOTB the platform offers


var gr = new GlideRecord('incident');

gr.comments.getJournalEntry(-1) //get all comments

Enter fullscreen mode Exit fullscreen mode

The OOTB method can be used to extend functionality when necessary. Say a need arose to get an array of entries, or in json format, or some sort of html. By encapsulating the OOTB method call then, exposing an api, one can:

The Test GlideRecords

var dml = new GlideRecord('dsl');

var incident = new GlideRecord('incident');

Enter fullscreen mode Exit fullscreen mode

The test cases


/* get the latest entry in JSON format from journal field 'notes' in record GlideRecord<dsl>

-> {"2021-09-16 21:18:43 - System Administrator":"\nnoted"} 


/* get all of the entries in "HTML" format from journal field 'work_notes' in record incident

-> "<div>2021-09-16 21:13:03 - System Administrator (Work notes)<br />worked notes<br /><br />2018-12-12 23:30:24 - System Administrator (Work notes)<br />Changed the priority of the Incident<br /><br /></div>"


/* get latest 2 entries as an array from journal field 'comments' in record incident

-> ["2021-09-16 19:18:52 - System Administrator (Additional comments)\nthis is another comment\r\n\r\n\r\nand another","2021-09-16 12:24:57 - System Administrator (Additional comments)\nBecause we need them",""]


/* get all entries as a string from journal field 'notes' from GlidRecord<dsl>. Same as OOTB

-> "2021-09-16 21:18:43 - System Administrator (Notes)\nnoted\n\n"
Enter fullscreen mode Exit fullscreen mode

The formatting for each output may be adjusted in the utility that looks like this:

var JournalEntry = function JournalEntry (gr) {
    var data = {
        "gr": gr,
        "fieldName": null,
        "count": -1

    return {
        "field": setFieldName,
        "toHTML": toHtml,
        "toJSON": fromArrayToJSON,
        "toString": toString,
        "toArray": toArray

    /*************************** public members ***********************/

    function toString (count) {
        return _journalFieldEntries(data, count);


    function toArray (count) {
        var ootbEntryDelimiter = '\n\n';

        return _journalFieldEntries(data, count)


    function toHtml (count) {
        var anyReturnOrNewLine = /\n|\r/g;
        var brTag = '<br />';

        var entries = _journalFieldEntries(data, count)
            .replace(anyReturnOrNewLine, brTag);

        return _betweenDivTag(entries);


    function fromArrayToJSON (count) {

        return toArray(count)
            .reduce(_toKeyPair(_keyRegex(, {});


    function setFieldName (x) {
        data.fieldName = x;

        return this;

    /*************************** private members ***********************/

    function _journalFieldEntries (data, count) {
        var x = count || data.count;



    function _keyRegex (gr) {
        var fieldLabel = gr.getElement(data.fieldName)

        return new RegExp('\\(' + fieldLabel + '\\)', 'i');


    function _toKeyPair (splitPattern) {
        return function applyToKeyPair (reducer, element) {
            var pair = element.split(splitPattern);
            var key = pair[0].trim();

            reducer[key] = pair[1];

            return reducer;

    function _betweenDivTag (value) {
        return "<div>" + value + "</div>";

Enter fullscreen mode Exit fullscreen mode

Method "field" specifies from which field in the GR to get entry values.

Methods "toHTML", "toJSON", "toArray" and "toString" are the terminal methods that return journal field entries given an integer value that specifies how many entries to retrieve. -1 being all, and any positive integer to get specific number of entries in date descending order.

Happy SNowing...

Top comments (0)