The CreatorCon Call for Content is officially open! Get started here.

Fix Script

Rooma1
Tera Contributor

Hi All,

I have a requirement to clean up the approval records which are older than 12 months and cancel all the related RITMs and workflow.

I have written the code as below: I have one doubt when i am using gr.update() only once at the end to update the RITM record it is not updating the RITM's work note but cancelling the stage and state of it.

But when i use gr.update() after worknotes it is updating the RITM, can someone tell why it is happening?

 

var gr = new GlideRecord('sysapproval_approver');

gr.addEncodedQuery('state=requested^sysapproval.sys_updated_on<=javascript&colon;gs.beginningOfLast12Months()');

gr.query();

if(gr.next()) {

 

    var RITMgr = new GlideRecord('sc_req_item');

    RITMgr.addQuery('sys_id', gr.document_id);

    RITMgr.addQuery('state', '!=', '5');

    RITMgr.query();

    while (RITMgr.next()) {

        RITMgr.work_notes = 'Cancelled by Scheduled Job - Housekeeping Job for RI/RQ';

        RITMgr.update();

       

        RITMgr.stage='Request Cancelled';

        RITMgr.state='5';

        RITMgr.setWorkflow(false);

        RITMgr.update();

 

        var workflow = new global.Workflow();

        workflow.cancel(RITMgr);

    }

    gr.state='cancelled';

    gr.update();

}

 

Thanks,

Rooma

2 REPLIES 2

shyamkumar VK
Kilo Patron

@Rooma1  , Update the Script as below 

 

var gr = new GlideRecord('sysapproval_approver');

gr.addEncodedQuery('state=requested^sysapproval.sys_updated_on<=javascript&colon;gs.beginningOfLast12Months()');
gr.orderBy('approver');

gr.query();

while(gr.next()) {

 

    var RITMgr = new GlideRecord('sc_req_item');

    if(RITMgr.get(gr.sysapproval.sys_id)) {

       var workflow = new Workflow();
            workflow.cancel(RITMgr);
            new UIActionUtils().approvalsNoLongerRequired(RITMgr.sys_id);
           RITMgr.state = 5; // update correct state value
            RITMgr.work_notes = "Cancelled by Scheduled Job - Housekeeping Job for RI/RQ";
            RITMgr.update();

        }
}

 

Regards,

Shyamkumar 

 

Please mark this as helpful and accept as a solution if this resolves your Ask.
Regards,

Shyamkumar

Amit Pandey
Kilo Sage

Hi @Rooma1 

 

Please use the following script once-

var gr = new GlideRecord('sysapproval_approver');
gr.addEncodedQuery('state=requested^sysapproval.sys_updated_on<=javascript&colon;gs.beginningOfLast12Months()');
gr.query();

while (gr.next()) {
    var RITMgr = new GlideRecord('sc_req_item');
    RITMgr.addQuery('sys_id', gr.document_id);
    RITMgr.addQuery('state', '!=', '5');
    RITMgr.query();

    while (RITMgr.next()) {
        // Update RITM work notes
        RITMgr.work_notes = 'Cancelled by Scheduled Job - Housekeeping Job for RI/RQ';
        RITMgr.update();

        // Cancel RITM
        RITMgr.stage = 'Request Cancelled';
        RITMgr.state = '5';
        var wf = new global.Workflow();
        wf.cancel(RITMgr);
    }

    // Update approval record
    gr.state = 'cancelled';
    gr.update();
}

Please let me if it works.