Cancel change with user Confirm Alert UI Action

JahanzebB
Mega Guru

SN Team,

In the process of modifying an existing Cancel Change UI Action. The goal is for the new UI Action to provide an confirmation window before proceeding with the Cancel Change request.

I have reviewed the following articles and here is what I came up.

Reference 1:

https://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/

Reference 2: Re: Cancel confirm window after clicking cancel button

Main issue: Confirmation window opens fine and allows user to Cancel. Having trouble getting the Cancel Change request to execute when OK is selected.

Looking for some help figuring out how to get the moveToCancel(); function to execute.

//moveToCancel();

//confirmCancel();

function confirmCancel(){

      var answer=confirm("Are you sure you want to cancel this record?");

      if (answer==false)

              {

              // gsftSubmit(null, g_form.getFormElement(), 'cancel_change');         //MUST call the 'Action name' set in this UI Action

                alert('This change was not cancelled');

              return false;      

              }

     

     

      else

          {

              moveToCancel();

              return true;

      }

}

function moveToCancel(){

      action.setRedirectURL(current);

      if (new ChangeRequestStateHandler(current).moveTo("canceled"))

              current.update();

}

      }

Thanks,

Jahanzeb

1 ACCEPTED SOLUTION

Deepak Ingale1
Mega Sage
  1. function confirmCancel(){  
  2.       var answer=confirm("Are you sure you want to cancel this record?");  
  3.       if (!answer)  
  4.               {  
  5.                 alert('This change was not cancelled');  
  6.               return false;          
  7.               }  
  8.       else  
  9.           {  
  10.               gsftSubmit(null, g_form.getFormElement(), 'cancel_change');         //MUST call the 'Action name' set in this UI Action  
  11.       }  
  12. }  
  13.  
  14. function moveToCancel(){  
  15.       action.setRedirectURL(current);  
  16.  
  17.       if (new ChangeRequestStateHandler(current).moveTo("canceled"))  
  18.               current.update();  
  19. }  

View solution in original post

16 REPLIES 16

Madhu27
Tera Expert

Jahanzab,   Try the following code



  1. function confirmCancel(){  
  2.       var answer=confirm("Are you sure you want to cancel this record?");  
  3.       if (answer==true)  
  4.               {  
  5.                   moveToCancel();

                                                              }


                                                else


                                                            {


  1.               // gsftSubmit(null, g_form.getFormElement(), 'cancel_change');         //MUST call the 'Action name' set in this UI Action  
  2.                 alert('This change was not cancelled');  
  3.               return false;          
  4.               }  
  5.     }  
  6.  

Madhsusudana,



Thank you for the response. The option to exit without canceling change request works find and displays alert "This change was not cancelled". Stil not executing the change request cancel when selecting OK. After this failed, I added an alert to the moveToCancel function, but the alert is not displayed. Here is the final code.



Here is the final code.


Let me know if I am   still missing something here with the script?



Thanks again,


Jahanzeb



  function confirmCancel(){


              var answer=confirm("Are you sure you want to cancel this record?");


              if (answer==true)


                      {


                          moveToCancel();



                      }



                  else


                        {


    // gsftSubmit(null, g_form.getFormElement(), 'cancel_change');         //MUST call the 'Action name' set in this UI Action


                        alert('This change was not cancelled');


                      return false;        


                      }


            }  



function moveToCancel(){


      action.setRedirectURL(current);



      if (new ChangeRequestStateHandler(current).moveTo("canceled"))


              current.update();


        alert('This change has been cancelled');


      }







lled');


      }



Madhusudana Golla wrote:



Jahanzab, Try the following code



  1. function confirmCancel(){
  2. var answer=confirm("Are you sure you want to cancel this record?");
  3. if (answer==true)
  4. {
  5. moveToCancel();

}


else


{


  1. // gsftSubmit(null, g_form.getFormElement(), 'cancel_change'); //MUST call the 'Action name' set in this UI Action
  2. alert('This change was not cancelled');
  3. return false;
  4. }
  5. }

Deepak Ingale1
Mega Sage
  1. function confirmCancel(){  
  2.       var answer=confirm("Are you sure you want to cancel this record?");  
  3.       if (!answer)  
  4.               {  
  5.                 alert('This change was not cancelled');  
  6.               return false;          
  7.               }  
  8.       else  
  9.           {  
  10.               gsftSubmit(null, g_form.getFormElement(), 'cancel_change');         //MUST call the 'Action name' set in this UI Action  
  11.       }  
  12. }  
  13.  
  14. function moveToCancel(){  
  15.       action.setRedirectURL(current);  
  16.  
  17.       if (new ChangeRequestStateHandler(current).moveTo("canceled"))  
  18.               current.update();  
  19. }  

This code will work for you.


You want to invoke the moveToCancel() function via gsftSubmit method if user confirms for cancellation.



gsftSubmit will ignore or skill the onClick function, in your case confirmCancel() and execute the server side function.