Have you ever got into this trap?
I'd like to make a confirmation with user that he/she are certain about submit/save/update action using a client script. This is the case. I can use a dumb confirm dialog which isn't cute at all. I can also
tryto use a GlideModal overlay with rich UI Page content. Why not?!
Well, because of
- the fact that submit action has been already initiated from onSubmit function of Client Script perspective
- and
- because of javascript nature
it is not so straight-forward :)
Simple answer is that system doesn't wait for the GlideModel to render(). And I didn't find any proper way to force it.
Moreover, it is a bad design step. I think of a validation phase before a submit event is triggered. It's not facilitated by ServiceNow though. It's another story.
Still, to achieve the result I do the following - gain control by interuption and trigger event again if it is confirmed:
- check if confirmation has been already gained
- Render confirmation dialog.
- Stop submit by returning false
- trigger initial action in onPromptComplete handler script using gsftSubmit()
- set an attribute in g_scratchpad e.g. confirmed_actionXZY to indicate the confirmation has already been gained
Example follows.
if(g_scratchpad._action_confirmed) {
return true;
}
var dialog = new GlideModal('glide_modal_confirm', false, 300);
dialog.setTitle(new GwtMessage().getMessage('Confirmation'));
dialog.setPreference('body', new GwtMessage().format("Are you sure to save?"));
dialog.setPreference('focusTrap', true);
dialog.setPreference('onPromptComplete', doComplete);
dialog.setPreference('onPromptCancel', doCancel);
dialog.render();
return false;
function doComplete() {
g_scratchpad._action_confirmed = true;
gsftSubmit(null, g_form.getFormElement(), g_form.getActionName());
}
function doCancel() {
}
Additionally, consider a low order of client script like this one to avoid double load.