Cancel request if catalog task is cancelled

Ankita Gupte
Kilo Sage

Hi Experts,

 

I have created a UI Action cancel task on catalog task form. So when "Cancel Task" UI action is clicked the associated RITM stage must be Request Cancelled, workflow must be cancelled and All associated REQ/RITM/Catalog task state must be closed incomplete. To achieve this I have written below script 

 

 

function cancelTask(){

if(confirm('Are you sure you want to cancel your Request?')) {

//Call the UI Action and skip the 'onclick' function

gsftSubmit(null, g_form.getFormElement(), 'cancel_sc_task');

}
return false;
}

//Code that runs without 'onclick'
//Ensure call to server-side function with no browser errors

if (typeof window == 'undefined')

updateTaskAndRITM();

function updateTaskAndRITM(){
current.state = 4; //value for Cancelled
current.work_notes = 'Request Cancelled';
current.comments = 'Request Cancelled';
current.update();

var reqItem = new GlideRecord('sc_req_item');
reqItem.addQuery('sys_id', current.request_item);
reqItem.query();
while(reqItem.next()) {
reqItem.setValue('stage','Request Cancelled');
reqItem.setValue('state','4');//stage is completed
reqItem.setValue('active','false');//stage is completed
reqItem.current.setWorkflow(false);//to disable all the notifications
reqItem.update();
new UIActionUtils().approvalsNoLongerRequired(current.sys_id);
}

var sctask = new GlideRecord('sc_task');
sctask.addQuery('request_item', 'current.sys_id');
sctask.query();
while(sctask.next()) {
    sctask.state = 4;
    sctask.worknotes = 'Request Cancelled';
}

var req = newGlideRecord('sc_request');
req.addQuery('request_item', 'current.sys_id');
req.query();
while(req.next()) {
    req.state = 4;
    req.worknotes = 'Request Cancelled';
}

}
AnkitaGupte_0-1719660098280.pngAnkitaGupte_1-1719660134783.png

 

AnkitaGupte_2-1719660167926.png

 

The problem here is The RITM workflow is getting completed due to which REQ is updated as close complete instead of close incomplete.
 
Please advice how can I cancel the RITM workflow and update REQ state as close incomplete.
 
 
12 REPLIES 12

OlaN
Giga Sage
Giga Sage

Hi,

Two things.

You should really start exploring Flow designer. Workflow editor is considered legacy since a while back.

 

Instead of writing a UI action that tries to cancel all the related RITM and REQ to the SCTASK, just cancel the SCTASK itself, and then have the Flow/Workflow evaluate how the SCTASK was ended, and act different if it was completed or cancelled.

We have implemented the workflow editor approach, but customer did not want it to be as part of workflow. They need it as UI action so based on future needs they can easily enable/disable the UI action. So we need it as a part of UI action only.

Okay. I cannot recommend going forward on this approach. There are a lot of other things going on in the platform to control the process of Workflows and Flows.
Creating a UI action that interrupts in this way will quite surely create some technical debt to be handled in the future.

And another thing I cannot grasp, is why it should cancel the request too. Just because a single RITM is cancelled shouldn't mean that the entire REQ should be cancelled. There could be numerous other RITMs connected to the same REQ, and they might be in the middle of processing when this cancellation is happening. Which means a lot to consider in all those cases.

 

I strongly recommend that you run this by the customer again and explain the downsides of the suggested solution.

Arya123
Tera Expert
Using below code i am able to achieve the requriment "when "Cancel Task" UI action is clicked the associated RITM stage must be Request Cancelled, workflow must be cancelled and All associated REQ/RITM/Catalog task state must be closed incomplete"
 
function cancelTask(){
    g_form.setValue('state', 4);
    //Call the UI Action and skip the 'onclick' function
    gsftSubmit(null, g_form.getFormElement(), 'cancel_sc_task');
}
//Code that runs without 'onclick'
//Ensure call to server-side function with no browser errors
if (typeof window == 'undefined')
   updateTask();

function updateTask(){
current.state = 4;
current.update();

 var ritm = new GlideRecord('sc_req_item');
    if (ritm.get(current.request_item)) {
        ritm.stage = 'Request Cancelled';
        ritm.state = 4; // 4 is the state for 'Closed Incomplete'
        ritm.update();
    }

    // Cancel the workflow associated with the RITM
    var wf = new Workflow();
    wf.cancel(ritm);

    // Close the associated REQ
    var req = new GlideRecord('sc_request');
    if (req.get(ritm.request)) {
        req.request_state = 'closed_incomplete';
        // req.state = 4; // 4 is the state for 'Closed Incomplete'
        req.update();
    }

    // Close all catalog tasks associated with the RITM
    var task = new GlideRecord('sc_task');
    task.addQuery('request_item', current.request_item);
    task.query();
    while (task.next()) {
        task.state = 4; // 4 is the state for 'Closed Incomplete'
        task.update();
    }

    // Redirect to the current record
    action.setRedirectURL(current);
};