- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2024 06:16 PM
Hi all,
I’m currently seeking a method to append only the most recently added work note to another field. The code I have at the moment appends all work notes, including old ones, to the ContactComments field. I’m interested in modifying this script to append just the latest work note. Any way to achieve this? Thanks
Here is my Business Rule (BR) code:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2024 06:27 PM
you need to query sys_journal_field table to get that.
// Get 'work_notes' field value from the 'wm_task' table and append to Contact comments
var taskGr = new GlideRecord('wm_task');
if (taskGr.get(current.u_work_order_task)) {
// Retrieve the latest work note entry
var latestWorkNote = getLatestWorkNote(taskGr.sys_id);
if (!gs.nil(latestWorkNote)) {
if (!gs.nil(ContactComments)) {
ContactComments += "\n\n";
}
ContactComments += "Work notes: " + latestWorkNote;
}
}
// Function to get the latest work note entry
function getLatestWorkNote(taskSysId) {
var journalEntryGr = new GlideRecord('sys_journal_field');
journalEntryGr.addQuery('element_id', taskSysId);
journalEntryGr.addQuery('element', 'work_notes');
journalEntryGr.orderByDesc('sys_created_on');
journalEntryGr.setLimit(1);
journalEntryGr.query();
if (journalEntryGr.next()) {
return journalEntryGr.value.toString();
}
return null;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2024 06:27 PM
you need to query sys_journal_field table to get that.
// Get 'work_notes' field value from the 'wm_task' table and append to Contact comments
var taskGr = new GlideRecord('wm_task');
if (taskGr.get(current.u_work_order_task)) {
// Retrieve the latest work note entry
var latestWorkNote = getLatestWorkNote(taskGr.sys_id);
if (!gs.nil(latestWorkNote)) {
if (!gs.nil(ContactComments)) {
ContactComments += "\n\n";
}
ContactComments += "Work notes: " + latestWorkNote;
}
}
// Function to get the latest work note entry
function getLatestWorkNote(taskSysId) {
var journalEntryGr = new GlideRecord('sys_journal_field');
journalEntryGr.addQuery('element_id', taskSysId);
journalEntryGr.addQuery('element', 'work_notes');
journalEntryGr.orderByDesc('sys_created_on');
journalEntryGr.setLimit(1);
journalEntryGr.query();
if (journalEntryGr.next()) {
return journalEntryGr.value.toString();
}
return null;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-24-2024 04:35 PM
That worked perfectly thanks!!