Question about "Work notes"

Community Alums
Not applicable

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:

 

// 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)) {
var workNotes = taskGr.work_notes.getJournalEntry(-1);
if (!gs.nil(workNotes)) {
if (!gs.nil(ContactComments)) {
ContactComments += "\n\n";
}
ContactComments += "Work notes: " + workNotes;
}
}
1 ACCEPTED SOLUTION

Gangadhar Ravi
Giga Sage
Giga Sage

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;
}

View solution in original post

2 REPLIES 2

Gangadhar Ravi
Giga Sage
Giga Sage

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;
}

Community Alums
Not applicable

That worked perfectly thanks!!