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

Hi Ankita,

Oh really I am also confused why  it  notworking  for you. It showing fine for me" close_incomplete".

my suggestion is check one more time the field name and the backend value for the REQ form state field.

Thanks

Radhika

Hi Radhika,

 

Request state value is close incomplete only, but still not working for me

AnkitaGupte_0-1720000932230.png

 

Krushna R Birla
Kilo Sage

Hi @Ankita Gupte 

 

Can you try below code,

 

function cancelTask() {
    g_form.setValue('state', 4);
    gsftSubmit(null, g_form.getFormElement(), 'cancel_sc_task');
}

if (typeof window == 'undefined')
    updateTask();

function updateTask() {
    current.state = 4;
    current.work_notes = 'Task Cancelled';
    current.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();
    }

    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(current.request_item);

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

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

 

This will definitely helps you to resolved your issue. Let me know in case you need to understand the flow or you can DM on LinkedIn.

 

If this solution resolves your query, kindly mark it as the accepted solution and give it a thumbs up.

 

Best Regards,
Krushna Birla