When cancelling an RITM - REQ and SC Tasks show as complete

EricG
Kilo Sage

Hello Guru's:

 

I have a request to allow ITIL users to Cancel an RITM.

I have a Before Update BR that checks if RITM is in state of Closed Incomplete or Closed Skipped.

(function executeRule(current, previous /*null when async*/ ) {

        // Add your code here
        //closing any child records
       
        current.active = false;
        current.state = 'closed_incomplete';
        current.close_notes = "This request was cancelled by " + gs.getUserDisplayName();

		//Close any open SC Tasks
        var currChld = new GlideRecord('sc_task');
        currChld.addQuery('request_item', current.sys_id);
        currChld.addQuery('active', true);
        currChld.query();

        while (currChld.next()) {
            currChld.state = 4;
            currChld.active = false;
            currChld.close_notes = "Ticket was cancelled by " + gs.getUserDisplayName();
            currChld.update();
        }

		//Several workflows have additional task that keep opening
        var currChld2 = new GlideRecord('sc_task');
        currChld2.addQuery('request_item', current.sys_id);
        currChld2.addQuery('active', true);
        currChld2.query();

        while (currChld2.next()) {
            currChld2.state = 4;
            currChld2.active = false;
            currChld2.close_notes = "Ticket was cancelled by " + gs.getUserDisplayName();
            currChld2.update();
        }
        //Closing parent REQ only if additional RITMs are closed
        var currReq = current.request;
        gs.info('The current req is ' + currReq + ' and the number is ' + currReq.number);

        var addParntChild = new GlideRecord('sc_req_item');
        addParntChild.addQuery('request', current.request);
        addParntChild.addActiveQuery(); //addParntChild.addQuery('active',true);
        addParntChild.query();

        if (addParntChild.next()) {
            gs.info("Parent Request " + current.request + " can not be cancelled");
        } else {
            var cancelTicket = new GlideRecord('sc_request');
            cancelTicket.addQuery('sys_id', currReq);
            cancelTicket.query();

            while (cancelTicket.next()) {
                cancelTicket.request_state = 'closed_cancelled';
                cancelTicket.state = 4;
                cancelTicket.update();
            }
        }
		//Cancelling active ritm workflow
        var currWorkflow = new global.Workflow();
        currWorkflow.cancel(current.sys_id);

        current.update();
    }

)(current, previous);

 

When i Set the RITM to either state, the RITM Stage goes to "Complete" as well as the REQ's Request State and State.

 

I've been looking through BR's and such, but can't find what is settings that way, instead of Incomplete.

Any Thoughts where/how to find this.

1 REPLY 1

AshishKM
Kilo Patron
Kilo Patron

Hi @EricG,

As i understood, the  RB running over RITM table and checking all SCTASK and other RITM of same REQ and closing them as cancel. 

The Stage field is set by running workflow, if you open the any workflow and check the Stage value of End activity. Stage reflect the workflow process completion status, even though request was cancel[State] but the request life-cycle reached at end.

Refer for Stage ->    https://docs.servicenow.com/csh?context=Workflow_Stages 

AshishKMishra_0-1700246120312.png

AshishKMishra_1-1700246156407.png

-Thanks,

AshishKMishra


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution