Getting the first record of a Journal Entry

Michael Domke
Tera Guru

I understand that getJournalEntry(1) returns the last entry from a journal field. But, I'd was wondering if there's something similar to get the first entry from a journal field.

Michael

1 ACCEPTED SOLUTION

lasse3
Mega Guru

I came across this post while searching for how to easily find the first entry in a journal field. The solutions provided in this thread is not very good as they make assumptions on the content of the journal entries. In real life user will put all kinds of stuff into the journal fields, so it would only be a matter of time before the solutions suggested would fail.

Instead of getting all journal entries as a string and then split this string I would recommend to query the sys_journal_field table which is the table actually containing all the posts to get the first entry.

Here is an example where you would like to get the first entry in the comments field for a given record where "current" is the record you would like the first comment on:

var je = new GlideRecord('sys_journal_field');
je.addQuery('element','comments');
je.addQuery('element_id',current.sys_id.toString());
je.orderBy('sys_created_on','ASC');
je.setLimit(1)
je.query();
if(je.next()){
 gs.info('First comment: ' + je.value.toString());
}

The above solution is more stable and safe because it does not make any assumptions regarding the content of the comment. It is also performance wise a better solution as it will only get the exact record needed from the database rather than reading out all records and then have javascript remove the not needed data.

To get the first work note simply replace "comments" with "work_notes" etc.

I hope this helps anybody else who might be searching for a solution to a similar issue.

View solution in original post

9 REPLIES 9

Pritha1
Mega Expert

Tested the below code for you. Please mark as answer correct if this is what you were looking for:

 

    var sysId = "";//put the sysid of the catalog task

    var tsk = new GlideRecord('sc_task'); //assuming this is catalog task
    tsk.addEncodedQuery('request_item=' + sysId);
    tsk.query();

    if (tsk.next()) {
        var notes = tsk.work_notes.getJournalEntry(-1);

        var na = notes.split("\n\n");
        var last = na.length - 2; //or -1 test with both
        gs.log(last);
        for (var i = 0; i < na.length; i++) {
            if (i == last) {
                gs.log(na[i]);
            }
        }
    }

Community Alums
Not applicable

I came across this post while searching for how to easily find the first entry in journal field. The solutions provided in this thread is not very good as they make assumptions on the content of the journal entries. In real life user will put all kinds of stuff into the journal fields, so it would only be a matter of time before the solutions suggested would fail.

Instead of getting all journal entries as a string and then split this string I would recommend to query the sys_journal_field table which is the table actually containing all the posts to get the first entry.

Here is an example where you would like to get the first entry in the comments field for a given record where "current" is the record you would like the first comment on:

var je = new GlideRecord('sys_journal_field');
je.addQuery('element','comments');
je.addQuery('element_id',current.sys_id.toString());
je.orderBy('sys_created_on','ASC');
je.setLimit(1)
je.query();
if(je.next()){
 gs.info('First comment: ' + je.value.toString());
}

The above solution is more stable and safe because it does not make any assumptions regarding the content of the comment. It is also performance wise a better solution as it will only get the exact record needed from the database rather than reading out all records and then have javascript remove the not needed data.

To get the first work note simply replace "comments" with "work_notes" etc.

I hope this helps anybody else who might be searching for a solution to a similar issue.

lasse3
Mega Guru

I came across this post while searching for how to easily find the first entry in a journal field. The solutions provided in this thread is not very good as they make assumptions on the content of the journal entries. In real life user will put all kinds of stuff into the journal fields, so it would only be a matter of time before the solutions suggested would fail.

Instead of getting all journal entries as a string and then split this string I would recommend to query the sys_journal_field table which is the table actually containing all the posts to get the first entry.

Here is an example where you would like to get the first entry in the comments field for a given record where "current" is the record you would like the first comment on:

var je = new GlideRecord('sys_journal_field');
je.addQuery('element','comments');
je.addQuery('element_id',current.sys_id.toString());
je.orderBy('sys_created_on','ASC');
je.setLimit(1)
je.query();
if(je.next()){
 gs.info('First comment: ' + je.value.toString());
}

The above solution is more stable and safe because it does not make any assumptions regarding the content of the comment. It is also performance wise a better solution as it will only get the exact record needed from the database rather than reading out all records and then have javascript remove the not needed data.

To get the first work note simply replace "comments" with "work_notes" etc.

I hope this helps anybody else who might be searching for a solution to a similar issue.

I like this method much better.

Awesome!