Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

cancel button on change task

harry24
Tera Contributor

Hello 

I want to create cancel button on change task which will cancel the change task .

It should make worknote field mandatory and add pop up whenever user tries to cancel the change task 

6 REPLIES 6

Jaspal Singh
Mega Patron
Mega Patron

Hi Harry,

 

Check for comments from Danish from thread.

Sohail Khilji
Kilo Patron

Hi @harry24 ,

 

 

  1. Create a new UI Action:
  • Go to the Form Layout for the record you want to modify (such as the Incident or Request form)
  • Click on the UI Actions related link
  • Click on New to create a new UI Action
  1. Configure the UI Action:
  • Set the Name of the UI Action, such as "Cancel with Reason"
  • Set the Table to the record type you want to modify (such as Incident or Request)
  • Set the Action Name to "Cancel"
  • In the Script field, enter the following code:

 

if (confirm('Are you sure you want to cancel this record?')) {
  var reason = prompt('Please enter a cancellation reason:');
  if (reason) {
    g_form.setValue('work_notes', reason);
    gsftSubmit(null, g_form.getFormElement(), 'cancel');
  } else {
    alert('A cancellation reason is required.');
  }
}

 

This code will display a confirmation dialog when the Cancel button is clicked, and prompt the user to enter a cancellation reason in a pop-up dialog. If the user enters a reason, the reason will be set as the value of the work_b otes field (which is the work notes field for some record types), and the record will be cancelled. If the user does not enter a reason, an alert message will be displayed.

  1. Save the UI Action and test it on the form.

☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

can you help me with below code 

function cancelChangeTask() {
if (confirm("You need to add a worknote with a cancellation reason while cancelling change task.")) {

// Set work notes field as mandatory
g_form.setMandatory('work_notes', true);
g_form.setValue('state', 7);
// Save the record
gsftSubmit(null, g_form.getFormElement(), 'cancel');

}
}

this is not working as expected after user enters worknote value it does not save form automatically also on clicking the cancel button again it does nothing

function cancelChangeTask() {
    if (confirm("You need to add a worknote with a cancellation reason while cancelling change task.")) {
        g_form.setMandatory('work_notes', true);
        if (g_form.getValue('work_notes')) {
            g_form.setValue('state', 3); //if Cancelled' state is by 3
            gsftSubmit(null, g_form.getFormElement(), 'cancel');
        } else {
            alert("Please add a worknote.");
        }
    }
}

☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect