Cancel UI action on Catalog task

Shruti Chelimel
Tera Expert

I need a cancel button on catalog task form. When Cancel UI action is clicked, the sc_task state should be changes to 'Close cancelled' and all its approval along with SLA's should also be cancelled and made inactive.

Please suggest an possible way to achieve this requirement.

8 REPLIES 8

Runjay Patel
Giga Sage

Hi @Shruti Chelimel ,

 

You can use below script in UI Action.

Create New UI action called "Cancel".

 

 

current.state = '4'; // replace with Closed Cancelled value
    current.update();


    // Cancel associated approvals
    var approvalGR = new GlideRecord('sysapproval_approver');
    approvalGR.addEncodedQuery('document_id='+current.sys_id+'^stateNOT INapproved,rejected,cancelled');
    
    approvalGR.query();
    while (approvalGR.next()) {
        approvalGR.state = 'cancelled'; // Change to "Cancelled"
        approvalGR.update();
    }


    // Cancel SLAs
    var slaGR = new GlideRecord('task_sla');
    slaGR.addQuery('task', current.sys_id);
    slaGR.addQuery('stage', '!=', 'cancelled');
    slaGR.query();
    while (slaGR.next()) {
        slaGR.stage = 'cancelled'; // Mark SLA as cancelled
        slaGR.active = false;     // Set SLA to inactive
        slaGR.update();
    }

    // Redirect the user back to the list view or refresh the form
    action.setRedirectURL(current.getTableName() + "_list.do"); 

 

 

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

Hi @Runjay Patel ,

 

I wrote the code in same way, but the state is changing to Closed Complete instead of Closed Cancelled. Some other script is changing the state to Closed Complete. I am unable to figure out exactly how the state is changing to Closed Complete instead of Closed Cancelled. 

 

Hi @Shruti Chelimel ,

 

Do you have close cancelled state as it wont come with OOB. Make sure you are putting the right value.

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

@Shruti Chelimel 

there would be some other script which is moving the state again

Did you debug business rules and see?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader