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

Hello @vivek11 ,

 

Just wanted to check in to see if my earlier response helped you out or if you're still facing any issues. If your query is resolved, it would be great if you could mark my reply as helpful and accept it as the solution — that way it can also benefit others who come across the thread later.😊

 

Also, if you found any other responses helpful, feel free to mark those as helpful too. You can even accept multiple solutions if they contributed to resolving your issue.

 

Let me know if you need anything else!

 

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