Is it possible to create an "Are you sure" pop-up box with server-side scripts?

C_dric Chen
Tera Contributor

Greetings, people.

 

I'm trying to configure an "Are you sure you want to cancel" dialog box, but I've been experiencing quite a bit of difficulties because of other business requirements:

  • This "Are you sure you want to cancel" dialog box should appear right after I click on the "Cancel" button, and there should be a "I'm sure" button and a "Never mind" button.
  • The business analyst dictated that I have to create a glideDialogWindow window, because the bare-bone JavaScript dialog box looks "unprofessional".
  • Because one of the business requirement states that "Nobody should be able to modify the 'State' field manually. It has to be done through UI actions", and because I've created an ACL to address this requirement, I absolutely have to use server-side scripts in this UI action (Otherwise, the "State" field won't be updated at all, even when it needs to be updated). However, I haven't been able to find any examples of triggering a dialog box through server-side scripts.

 

Would anyone like to suggest what approach I should take?

 

Thank you in advance!

10 REPLIES 10

Paul Deonarine3
Tera Expert

To trigger a dialog box from server-side scripts in ServiceNow, you can use the showDialog method of the GlideDialogWindow API. Here's an example of how you can use this method to create a "Are you sure you want to cancel" dialog box in a UI action:

  1. In the UI action, add a server-side script that checks if the current record can be canceled based on your business requirements. If the record can be canceled, proceed to step 2. Otherwise, display an error message to the user and exit the script.
  2. Create a new instance of the GlideDialogWindow API and configure its properties, such as the title, message, and buttons. For example:

 

var dialog = new GlideDialogWindow('are_you_sure_dialog');
dialog.setTitle('Cancel Confirmation');
dialog.setMessage('Are you sure you want to cancel?');
dialog.addChoice('yes', 'I\'m sure', true);
dialog.addChoice('no', 'Never mind', false);

 

In this example, we create a new dialog window with the ID "are_you_sure_dialog" and set its title and message. We also add two buttons with the labels "I'm sure" and "Never mind", respectively.

 

3. Call the showDialog method of the GlideDialogWindow API to display the dialog box to the user. For example:

 

dialog.show();

4. If the user clicks the "I'm sure" button, update the state of the current record and redirect the user to the appropriate page. If the user clicks the "Never mind" button, do nothing and exit the script.

 

Here's an example of how the entire server-side script might look like:

 

if (!gs.hasRole('cancel_record_role')) {
  gs.addErrorMessage('You do not have permission to cancel this record.');
  return;
}

var dialog = new GlideDialogWindow('are_you_sure_dialog');
dialog.setTitle('Cancel Confirmation');
dialog.setMessage('Are you sure you want to cancel?');
dialog.addChoice('yes', 'I\'m sure', true);
dialog.addChoice('no', 'Never mind', false);

if (dialog.show() == 'yes') {
  current.state = 3; // Set the state to "Canceled"
  current.update();
  action.setRedirectURL(current);
}

 

In this example, we check if the current user has the "cancel_record_role" role, display an error message if they don't, and exit the script. If the user has the role, we create a new dialog window, display it to the user, and update the state of the current record if the user clicks the "I'm sure" button. We also redirect the user to the appropriate page after the update.

Note that you may need to modify this script to suit your specific business requirements and use case. Also, make sure to test your script thoroughly before deploying it to production.

Hello.

 

When I entered your scripts, I got a parsing error at the "return;". ServiceNow says "'return' outside of function".

 

Do you know what might have happened?

Hello.

 

So I punched in your codes, like this:

截圖 2023-03-14 10.21.02.png

 

And here's the outcome:

ezgif.com-video-to-gif(1).gif

No matter how furiously I click on the button, nothing happens.

In case anyone comes across and considers this response for their own use, please note that the GlideDialogWindow API is both only client side and now deprecated. This means it would only have worked if they had checked the client checkbox for their UI Action.