- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-29-2012 11:25 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-06-2019 01:54 AM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-29-2012 02:55 PM
I don't think there is a canned function so your best bet is to do something like:
var je = current.journalField.getJournalEntry(-1);
var firstEntry = je.getJournalEntry(je.length);
You might need to tweak your je.length to be something like je.length +/- 1.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-29-2012 03:15 PM
What I ended up doing, with some help form your Navigis cohort Brad T., was somewhat similar but different (huh?).
Anyway, here's the code I'm using to get the first journal entry from the work_notes field:
var gr = <get the record>;
var workNotes = gr.work_notes.getJournalEntry(-1); //gets all journal entries as a single string
var wn = workNotes.split("(Work notes)\n"); //split the entries on the string "(Work notes)\n"
var firstNote = wn[wn.length-1];
This Wiki article suggests splitting on the string "\n\n" as that is what separates each entry in the single string. But, what that fails to handle is any content in the journal entry that itself has a double line feed.
Michael

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-29-2012 04:00 PM
Looks like Brad steered you right. As you said, similar but different. I'm glad you got it working!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-26-2013 01:21 AM
Hi Mike, have you managed to get the journal entry?
I tried the following excerpt client script under onchange script but it doesn't work. I'm trying to get the whole entry. I don't use Script Business Rules because the Incident information needs to be populated first on the Problem page and user should be able to add/edit some other information before the submit it.
Any help or guide is highly appreciated!
-----------------------------------------------------------------------------------------
function onChange(control, oldValue, newValue, isLoading, isTemplate)
{
if (!isLoading)
{
if (newValue)
{
if ((newValue != oldValue))
{
//Type appropriate comment here, and begin script below
var IncVal = g_form.getReference('u_incident', doReturn);
dSys_id = g_form.getValue('u_incident');
var retData = new GlideRecord('task');
retData.addQuery('sys_id', dSys_id);
retData.query();
var notes = retData.work_notes.getJournalEntry(-1); //gets journal entries
var na = notes.split("\n\n"); //stores each entry into an array of strings
//var ta = na[length. - 2];
g_form.setValue('description',na);