RITM Work Notes is not Updating

vivek11
Tera Contributor

Find all approval records for RITMs that are in a pending/requested state.

Reject those approvals.

Update the corresponding RITM to reflect the rejection by:

Setting its state to "Closed Incomplete"

Marking it as inactive

Adding a work note if one doesn't already exist

Please correct my below code for above requirement as RITM Work notes is not updating



var
processedRITMs = {};
var approvalGR = new GlideRecord('sysapproval_approver');
approvalGR.addEncodedQuery('sysapproval.numberLIKERITM^sysapproval.state=4^state=requested');
approvalGR.setLimit(200);
approvalGR.query();

while (approvalGR.next()) {
    approvalGR.setWorkflow(false);
    approvalGR.autoSysFields(false);
    approvalGR.state = 'rejected';
    approvalGR.update();
    gs.info('Approval rejected: ' + approvalGR.sys_id + ' for RITM ' + approvalGR.sysapproval);

    var ritmID = approvalGR.sysapproval.toString();

    if (!processedRITMs[ritmID]) {
        var reqItemGR = new GlideRecord('sc_req_item');
        if (reqItemGR.get(ritmID)) {
            // Check for existing work note
            var noteExists = false;
            var journalGR = new GlideRecord('sys_journal_field');
            journalGR.addQuery('element_id', reqItemGR.sys_id);
            journalGR.addQuery('element', 'work_notes');
            journalGR.addQuery('value', 'Updated by Fix Script');
            journalGR.query();

            if (journalGR.hasNext()) {
                noteExists = true;
                gs.info('Work note already exists for RITM: ' + reqItemGR.number);
            }

            reqItemGR.setWorkflow(false);
            reqItemGR.autoSysFields(false);
            reqItemGR.setValue('state', 4);
            reqItemGR.setValue('stage', 'Closed Incomplete');
            reqItemGR.setValue('approval', 'rejected');
            reqItemGR.setValue('active', false);
           

            if (!noteExists) {
               // reqItemGR.work_notes = 'Updated by Fix Script';
                gs.info('Adding work note to RITM: ' + reqItemGR.number);
                reqItemGR.work_notes = 'Updated by Fix Script';
            }

            reqItemGR.update();
            gs.info('Updated RITM: ' + reqItemGR.number + ' (' + reqItemGR.sys_id + ')');
        } else {
            gs.info('RITM not found for sys_id: ' + ritmID);
        }

        processedRITMs[ritmID] = true;
    }
}
5 REPLIES 5

Mark Manders
Mega Patron

First: what's the use case? You are closing all RITMs that are waiting on approval when you do this. That can cause a lot of confusion, especially when it's only in the work notes that you put something that doesn't say anything about the reason.


And why are you looking at the sys_journal_field record? If you run your query over the approval records and check on the still open RITMs, you don't need to check if the work note already exists, since you can update the work note at the same time as you move the RITM to 'rejected'. And any other approval record that is still open for this RITM, won't update it again. 

And maybe your check isn't even necessary. Just get all RITMs that are awaiting approval and update those to rejected. Depending on the (work)flow behind it, you will probably also handle all approval records. 


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Hi @Mark Manders 
Thank you for quick reply
If possible please provide me updated code

Ankur Bawiskar
Tera Patron
Tera Patron

@vivek11 

is that a business requirement?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Aniket Chavan
Tera Sage
Tera Sage

Hello @vivek11 ,

 

You may want to first consider the points raised by @Mark Manders — especially around whether mass-closing RITMs like this is the right long-term approach, since it can cause confusion without a clear reason logged in the record.

 

That said, if you do choose to proceed with the current logic, the reason why the work notes aren't updating is because of this line in your script:

reqItemGR.setWorkflow(false);

 

When you use setWorkflow(false), it disables all system triggers — including business rules and the internal logic that creates journal entries (like work notes or comments). That’s why even though you assign work_notes, nothing shows up on the RITM.

 

To solve this, you can use a two-step update:

var processedRITMs = {};
var approvalGR = new GlideRecord('sysapproval_approver');
approvalGR.addEncodedQuery('sysapproval.numberLIKERITM^sysapproval.state=4^state=requested');
approvalGR.setLimit(200);
approvalGR.query();

while (approvalGR.next()) {
    approvalGR.setWorkflow(false);
    approvalGR.autoSysFields(false);
    approvalGR.state = 'rejected';
    approvalGR.update();
    gs.info('Approval rejected: ' + approvalGR.sys_id + ' for RITM ' + approvalGR.sysapproval);

    var ritmID = approvalGR.sysapproval.toString();

    if (!processedRITMs[ritmID]) {
        var reqItemGR = new GlideRecord('sc_req_item');
        if (reqItemGR.get(ritmID)) {
            // Check if work note already exists
            var noteExists = false;
            var journalGR = new GlideRecord('sys_journal_field');
            journalGR.addQuery('element_id', reqItemGR.sys_id);
            journalGR.addQuery('element', 'work_notes');
            journalGR.addQuery('value', 'Updated by Fix Script');
            journalGR.query();

            if (journalGR.hasNext()) {
                noteExists = true;
                gs.info('Work note already exists for RITM: ' + reqItemGR.number);
            }

            // Step 1: update core fields without workflow
            reqItemGR.setWorkflow(false);
            reqItemGR.autoSysFields(false);
            reqItemGR.setValue('state', 4);
            reqItemGR.setValue('stage', 'Closed Incomplete');
            reqItemGR.setValue('approval', 'rejected');
            reqItemGR.setValue('active', false);
            reqItemGR.update();
            gs.info('Updated RITM fields: ' + reqItemGR.number);

            // Step 2: update work note with workflow true (only if needed)
            if (!noteExists) {
                reqItemGR.setWorkflow(true);
                reqItemGR.work_notes = 'Updated by Fix Script';
                reqItemGR.update();
                gs.info('Added work note to RITM: ' + reqItemGR.number);
            }

        } else {
            gs.info('RITM not found for sys_id: ' + ritmID);
        }

        processedRITMs[ritmID] = true;
    }
}

 

🔹 Please mark Correct if this solves your query, and 👍 Helpful if you found the response valuable.

 

Best regards,
Aniket Chavan
🏆 ServiceNow MVP 2025 | 🌟 ServiceNow Rising Star 2024