- 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
‎09-25-2019 08:41 AM
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]);
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-06-2019 01:53 AM
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.
- 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
‎03-25-2020 08:30 AM
I like this method much better.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-11-2024 02:32 PM
Awesome!