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

Deepak,



Thanks for the recommendation. This works great now!! I now understand your comment about using the gsftSubmit method if the user confirms the cancellation. Here is what I have for the UI action fields and the Script.  



Here is a summary of what I modified on the UI action:



1. Included Action Name: cancel_change


2. Added invoke moveToCancel function on the first line of the script.



The cancellation is successful when the user confirms cancellation. I had to invoke the moveToCancel function outside of the onClick function. Considering best practices for UI actions, do you see any issues with where I invoke moveToCancel?



Just looking into other scenarios. By adding the moveToCancel function on the first line, Is is possible that this will Cancel the change request if the user does not provide any input after 2 attempts?



find_real_file.png


find_real_file.png



moveToCancel();


//confirmCancel();



      function confirmCancel(){


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


              if (!answer)


                      {


                        alert('This change was not cancelled');


                      return false;        


                      }


              else


                  {


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



             


                  }


      }


       


      function moveToCancel(){


              action.setRedirectURL(current);


       


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


                      current.update();


      }  












find_real_file.png


Actually you dont require moveToCancel() function call at all.


You can remove this from line 1



gsftSubmit() calls this function and we are calling gsftSubmit only when user clicks on OK for cancellation.



So you don't require it.


Deepak,



Went through some more testing without the moveToCancel() function call on the first line. The script no longer cancels the Change Request based on this function. The user is returned to the previous screen and the Change Request state does not update. At this point, it appears that there must be some call to moveToCancel() function within the script.



I went back and referenced the out of the box Cancel Change script for UI actions(shown below). This includes a moveToCancel function call even though this is the only function in the script.



Unless there is a better way to call this function, I will have to leave this on the first line,



Thanks again for all of your help.


Regards,


Jahanzeb



moveToCancel();



function moveToCancel(){


      action.setRedirectURL(current);



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


              current.update();


}





Hi Deepak,

i tried same code but when i click on cancel it won't gives popup message and also don't cancel my request.. below is my code for your reference.

let me now if you can help me on this?

cycleMove();

function Cancelconfirm(){

var answer=confirm('are you sure you want to cancel?');

if(answer==true){

gsft.g_form.getSubmit(null,g_form.Element(),'cancel');//my ui action name

}

else{

return false;

}

function cyclemove(){

current.phase='cancelled';

current.active=false;

current.update();

action.SetRedirectURL(current);

}

 

 

 

 

Hi Deepak,

i tried same code but when i click on cancel it won't gives popup message and also don't cancel my request.. below is my code for your reference.

let me now if you can help me on this?

cycleMove();

function Cancelconfirm(){

var answer=confirm('are you sure you want to cancel?');

if(answer==true){

gsft.g_form.getSubmit(null,g_form.Element(),'cancel');//my ui action name

}

else{

return false;

}

function cyclemove(){

current.phase='cancelled';

current.active=false;

current.update();

action.SetRedirectURL(current);

}