How to get Journal Entry ID on creation?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2017 01:32 PM
Hi everyone,
I am building a REST integration that includes pushing Case activity history into ServiceNow as Work Notes. The problem is that the source system sends the full activity history every.single.time it updates the parent case record. Therefore, I need to identify only new Work Notes before saving to ServiceNow.
In my Istanbul instance, I've tried using gr[work_notes'].setJournalEntry('your note here'). Unfortunately, while that creates the Work Notes entry, it returns 'undefined' instead of a sys_id of the resulting journal field record. I can use an indirect approach to query the last x journal field records to make a best guess of which record I just inserted, for example, but indirect approaches are an accident waiting to happen.
If anyone can point me to a direct method of returning the journal field record's sys_id upon insert, that will help me tremendously!
Thanks in advance!
- Labels:
-
Integrations
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2017 04:01 PM
Hi,
You can use getJournay Entry to get journal entry records. http://wiki.servicenow.com/index.php?title=GlideElement#gsc.tab=0
var notes = current.work_notes.getJournalEntry(-1);
GlideElement - ServiceNow Wiki
Regards,
Sachin

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2017 05:04 AM
Hi sachin.namjoshi,
Thanks for your response. I am trying to get the sys_id of the most recent entry upon insert.
With what you're suggesting, I would perform the insert (setJournalEntry) and then call getJournalEntry(1) [-1 returns all; 1 returns the last entry]. I would then have to indirectly find the sys_journal_field sys_id by querying that table for a match maybe based on the timestamp, as getJournalEntry does not return the sys_id (according to the documentation). I could also store the timestamp in a text field and use as the unique ID...but that is still indirect.
If you (or anyone for that matter! ) knows a way I can get the sys_id on insert so I can use when de-duping the inbound record based on the external ID, that will be precisely what I'm looking for.
Thanks Sachin!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2017 05:15 AM
Hi Taarik,
You are right, it looks like setJournalEntry() does not return any value. Perhaps you can try using a glideRecord insert() function that adds the work notes directly into the Journal Entry table (sys_journal_field). Using the insert() function should return the sysID of the newly inserted record. However, it can get messy as it looks like you have to work with multiple tables (sys_journal_field, sys_audit, ..) I am not sure if this is a best practice in adding a work note.
Example:
var gr = new GlideRecord('sys_journal_field');
gr.initialize();
gr.name="task";
gr.element="work_notes";
gr.element_id='e8e875b0c0a80164009dc852b4d677d5';
gr.value='work notes goes here';
gs.log(gr.insert());
var gr = new GlideRecord('sys_audit');
gr.initialize();
gr.tablename="incident";
gr.fieldname="work_notes";
gr.documentkey='e8e875b0c0a80164009dc852b4d677d5';
gr.newvalue='work notes goes here';
gr.oldvalue='JOURNAL FIELD ADDITION';
gs.log(gr.insert());
I did some testing, it seems to return the sysID of the newly inserted sys_journal_field ok. However, the work notes does not show up in the list of work notes. There may be something I'm missing.
Thanks,
Jenny

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2017 05:25 AM
Hi jennyhu,
Thanks for the response! Lol!! I actually tried the insertion before posting to the forum and had the exact same experience as you did! I'm definitely open to the manual method if I knew what the missing piece was to get the Work Notes to show in the Incident!