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 Radhika,

 

Thank you for response.

 

I used the above script.

 

SCTASK state is now close incomplete

RITM stage is request cancelled.

RITM state is close incomplete

but REQ state is close complete I need REQ state as close incomplete as well.

 

Please advice

Sid_Takali
Kilo Patron
Kilo Patron

Hi @Ankita Gupte Try below code 

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;
}

function updateTaskAndRITM() {
    // Update current sc_task record (assuming this is the current task being cancelled)
    current.state = 4; // '4' represents Cancelled state
    current.work_notes = 'Request Cancelled';
    current.comments = 'Request Cancelled';
    current.update();

    // Update related sc_req_item (RITM)
    var reqItem = new GlideRecord('sc_req_item');
    reqItem.addQuery('sys_id', current.request_item);
    reqItem.query();
    while (reqItem.next()) {
        reqItem.stage = 'request_cancelled'; // Update to the appropriate stage name
        reqItem.state = 4; // '4' represents Completed state
        reqItem.active = false;
        reqItem.update();
    }
    // Cancel workflows associated with the RITM (if needed)
    new Workflow().cancel(reqItem);
    // Update associated sc_request (REQ)
    var req = new GlideRecord('sc_request');
    req.addQuery('request_item', current.request_item);
    req.query();
    while (req.next()) {
        req.state = 4; // '4' Cancelled state
        req.work_notes = 'Request Cancelled';
        req.update();
    }

    var catalogTask = new GlideRecord('sc_task');
    catalogTask.addQuery('request_item', current.request_item);
    catalogTask.query();
    while (catalogTask.next()) {
        catalogTask.state = 4; // '4' Cancelled state
        catalogTask.work_notes = 'Request Cancelled';
        catalogTask.update();
    }
}

This is not working.

 

The RITM stage not getting updated and REQ state as well.

Arya123
Tera Expert

Hi Ankita,

thanks for the response 

Again I checked the same code, I am getting the output REQ state is Close Incomplete. can you check the backend value for the REQ state ( for me the  the REQ state field value is " closed_incomplete").

Hi Radhika,

 

For me also REQ state field value is " closed_incomplete" but not sure why it is not working for me.

 

the req state is still close complete.