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

//GlideRecord.<fieldName>.getJournalEntry(<numberOfEntries2Fetch>)

var gr = new GlideRecord('incident');
gr.get('<sys_id');

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');
dml.get('31a60f4bdb2a70109c9268d35b96196e');

var incident = new GlideRecord('incident');
incident.get('57af7aec73d423002728660c4cf6a71c');

Enter fullscreen mode Exit fullscreen mode

The test cases

gs.debug(JSON.stringify(JournalEntry(dml)
    .field('notes')
    .toJSON(1)));

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

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

gs.debug(JournalEntry(incident)
    .field('work_notes')
    .toHTML(-1));

/* 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>"
*/

gs.debug(JournalEntry(incident)
    .field('comments')
    .toArray(2));

/* 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",""]
*/

gs.debug(JournalEntry(dml)
    .field('notes')
    .toString(-1));

/* 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)
            .split(ootbEntryDelimiter);

    }

    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(data.gr)), {});

    }

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

        return this;
    }



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

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

        return data.gr[data.fieldName]
            .getJournalEntry(x);

    }


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

        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)