How to query from most recent worknotes activities

Andre6
Mega Expert

Hi,

I'm doing a query from an IF statement workflow to get information from RTIM notes activities.

The script script is working but I would like to be able to query only the last activity (most recent).

The reason is in some cases, the workflow will need to loop back and will need to redo a query in the note activities and it will require to  see the lastest activity recorded.

Thanks in advance for your help

 

-----

answer = ifScript();

function ifScript() {

var notes = current.work_notes.getJournalEntry(-1);

if (notes.toLowerCase().indexOf("please extend expiry date") > -1){
return 'yes';
}
if (notes.toLowerCase().indexOf("please deactivate/withdraw") > -1){
return 'no';
}
}

-----

2 ACCEPTED SOLUTIONS

Matthew Smith
Kilo Sage

getJournalEntry(Number mostRecent)

Returns either the most recent journal entry or all journal entries.
Parameters
Name Type Description
mostRecentNumberIf 1, returns the most recent entry. If -1, returns all journal entries.

 

 current.work_notes.getJournalEntry(-1); 

 current.work_notes.getJournalEntry(1);

View solution in original post

RaghavSh
Kilo Patron

You need to replace -1 with 1

var notes = current.work_notes.getJournalEntry(1);

 

-1 fetches all entries whereas 1 fetches the latest one.


Please mark the answer correct/helpful accordingly.


Raghav
MVP 2023

View solution in original post

7 REPLIES 7

Matthew Smith
Kilo Sage

getJournalEntry(Number mostRecent)

Returns either the most recent journal entry or all journal entries.
Parameters
Name Type Description
mostRecentNumberIf 1, returns the most recent entry. If -1, returns all journal entries.

 

 current.work_notes.getJournalEntry(-1); 

 current.work_notes.getJournalEntry(1);

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Andre6 ,

Can you give a try on below code :-

answer = ifScript();

function ifScript() {
    var je = new GlideRecord('sys_journal_field');
    je.addEncodedQuery('element=work_notes');
    je.addQuery('element_id', current.sys_id.toString());
    je.orderByDesc('sys_created_on');
    je.setLimit(1);
    je.query();
    if (je.next()) {
        if (je.value.toString().toLowerCase().indexOf("please extend expiry date") > -1) {
            return 'yes';
        } else if (je.value.toString().toLowerCase().indexOf("please deactivate/withdraw") > -1) {
            return 'no';
        }
    }
}

Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

for a reason, the IF statement is always selecting No

@Andre6 

I would use the .getJournalEntry(1) command rather than gliding the whole journal table every time.

You will put unnecessary load on your system.


If I helped you with your case, please click the Thumb Icon and mark as Correct.