Confirm box (Yes/No) in a UI Action

hongsok
Tera Contributor

Hello all,

I have created the following UI action and it works fine. Now I have a requirement to add the Confirm box (Yes/No) to that UI Action button.

createGSTicket1();

function createGSTicket1() {
current.transf_inc = 'true';
current.update();
action.setRedirectURL(current);
}

_____________________________________________________________________

Now I add the following code in and the Confirm box is not working.

createGSTicket1();

function createGSTicket1() {
var usrResponse = confirm('Are you sure you want to transfer this request to incident?');
if (usrResponse == true) {
current.transf_inc = 'true';
current.update();
action.setRedirectURL(current);
} else {
return false;
}}

I would appreciate if someone can help me on this issue.

Regards,

Hong

8 REPLIES 8

Harsh Vardhan
Giga Patron

confirm() works at client side. you have to use confirm in client side function and update part will be at server side. 

Please have a look on below blog for further details about UI Action

 

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

Harsh Vardhan
Giga Patron

Updated Sample code:

 

//Client-side 'onclick' function
function test(){
	var usrResponse = confirm('Are you sure you want to transfer this request to incident?');
   if(usrResponse == 'false'){
      return false;  //Abort submission
   }
   //Call the UI Action and skip the 'onclick' function
   gsftSubmit(null, g_form.getFormElement(), 'test'); //MUST call the 'Action name' set in this UI Action
}

//Code that runs without 'onclick'
//Ensure call to server-side function with no browser errors
if(typeof window == 'undefined')
   runBusRuleCode();

//Server-side function
function runBusRuleCode(){
   current.transf_inc  = true;
   current.update();
   gs.addInfoMessage('You did it!');
   action.setRedirectURL(current);
}

Hi Harshvardhan,

 

When I click it does not show the Yes/No confirm box. It's aborted submission. Any idea?

Hi Harshvardhan,

The confirm box is working and the problem is if I click on either OK or Cancel, the script set the current.transf_inc = true.

Any idea?

____________________________________

function confirmAndTransferFromForm() {

var usrResponse = confirm('Are you sure you want to transfer this request to incident?');

  if (usrResponse == 'false') {
     return false; //Abort submission

} else
gsftSubmit(null, g_form.getFormElement(), 'transfer_incident'); //Call the Action name set in this UI Action

}

if (typeof window == 'undefined')
runBusRuleCode();

//Server-side function
function runBusRuleCode() {
current.transf_inc = true;
current.update();
gs.addInfoMessage('You did it!');
action.setRedirectURL(current);
}