worknote not updated via fix script for few RITms

Ravivarman Saee
Tera Contributor

hi team,

i am running below fix script to cancel flow contexts and update the RITM worknote. but i can see not all ritms are updated with the worknote. Any suggestions??

 

 

var ritmGR = new GlideRecord('sc_req_item');
ritmGR.addEncodedQuery("cat_item=cdc93b401b6321904278dbd1f54bcb34^flow_context.flow=e4226b4edba36dd45c82defad3961971^flow_context.stateINWAITING,IN_PROGRESS,QUEUED,CONTINUE_SYNC,PRESUMED_INTERRUPTED,PAUSED"); //Other in Salesforce, cancelling only the old flow related ones
ritmGR.query();
// any old ritm that is in Work in progress state, even if it is closed after the flow change, its flow context will remain waiting. Hence have to cancel all ritm flow contexts in waiting states

var failed_ritms = [];
var flow_cancellation_reason = 'Cancelling incomplete flow contexts';

while (ritmGR.next()) {
    // Cancel the Flow
    var flowContextGR = new GlideRecord("sys_flow_context");
    flowContextGR.addQuery('source_record', ritmGR.getValue('sys_id'));
    flowContextGR.query();
    while (flowContextGR.next()) {
        try {
            sn_fd.FlowAPI.cancel(flowContextGR.getUniqueValue(), flow_cancellation_reason);
        } catch (e) {
            failed_ritms.push(ritmGR.getValue('number'));
        }
    }

    // Cancel the Approvers 
    var approvalGR = new GlideRecord('sysapproval_approver');
    approvalGR.addEncodedQuery('document_id=' + ritmGR.getValue('sys_id') + '^stateNOT INapproved,rejected,cancelled,not_required');
    approvalGR.query();
    while (approvalGR.next()) {
        approvalGR.setValue('state', 'cancelled');
        approvalGR.setWorkflow(false);
        approvalGR.autoSysFields(false);
        approvalGR.update();
    }

    // Cancel the SCTasks
    var taskGR = new GlideRecord('sc_task');
    taskGR.addEncodedQuery('request_item=' + ritmGR.getValue('sys_id') + '^active=true^ORstateNOT IN3,4,7');
    taskGR.query();
    while (taskGR.next()) {
        taskGR.setValue('state', '7'); // closed skipped
        taskGR.setValue('active', false);
        taskGR.setWorkflow(false);
        taskGR.autoSysFields(false);
        taskGR.update();
    }

    //update worknote in RITM
    ritmGR.work_notes = 'Cancelling Flow from Fix Script';
    // at.setWorkflow(false);// dont set this to false since it will only update the sys_journal table but not the RITM activity journal.
    ritmGR.autoSysFields(false);
    ritmGR.update();
}
if (failed_ritms.length != 0) {
    gs.log("Failed to cancel the flows of following RITMS:\n" + failed_ritms);
}

 

 

4 REPLIES 4

Bhavya11
Kilo Patron
Kilo Patron

 Hi @Ravivarman Saee 

 

In above case when the script is executed, setWorkflow(false) could only stop copying case work notes.

i.e setWorkflow(false); will disable firing of BR on table.

 

reference:
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0867584 

 

How to Update Worknote even the script is executing setWokflow(false)  

 

Please mark helpful & correct answer if it's really worthy for you

 

Thanks,

BK

but my script updates worknotes in dev for all ritms and only in test and prod instances the issue exists where few ritms not updated with worknote, while some are updated.

Dnyaneshwaree
Mega Sage

 

Hello @Ravivarman Saee ,

Check-
1. below encoded query logic returns all required number of records or not?

ritmGR.addEncodedQuery("cat_item=cdc93b401b6321904278dbd1f54bcb34^flow_context.flow=e4226b4edba36dd45c82defad3961971^flow_context.stateINWAITING,IN_PROGRESS,QUEUED,CONTINUE_SYNC,PRESUMED_INTERRUPTED,PAUSED"); //Other in Salesforce, cancelling only the old flow related ones

2. The "autoSysFields(false)" and "setWorkflow(false)" methods are not affecting to update work notes.


Please accept my solution if it works for you and thumps up to mark it as helpful.
Thank you!!

Dnyaneshwaree Satpute
Tera Guru

yes the query returns the records correctly.
if i set workflow false - then it will not update worknotes in ritm