The Zurich release has arrived! Interested in new features and functionalities? Click here for more

onSubmit script g_modal popup saving form when popup appears

Khalnayak
Tera Guru

Hello,

I have the following onSubmit client script, and it presents a modal popup for end users when conditions are met.

This is for native Ui and agent workspace.

Works fine in native, it prompts the user with modal and only submits form when user selects yes.

Whereas in workspace, when the modal popup appears the form saves at the same time.

How can I prevent the form from saving in workspace until the modal has not been actioned, i.e. yes/no selected.

here is my script for HR cases:

function onSubmit() {

    var subjectPersonValue = g_form.getValue('subject_person');
    var openedForValue = g_form.getValue('opened_for');
    var openedFor = g_form.getControl('opened_for');



    var url = top.location.href;
    if (url.indexOf('workspace') <= -1) { // if using native UI run this code

        if (openedFor.changed) {
            if (subjectPersonValue == openedForValue) {

                if (g_scratchpad._action_confirmed) {
                    return true;
                }
					// Render GlideModal window
                var dialog = new GlideModal('glide_modal_confirm', false, 400);
                dialog.setTitle(new GwtMessage().getMessage('Opened for Changed to Subject Person'));
                dialog.setPreference('body', new GwtMessage().format("You have set the Opened For to the Subject Person, which will grant them access to view the Case - are you sure?"));
                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() {

                }

            }
        }
    } else if (url.indexOf('workspace') > -1) { // if using workspace run this code

        if (g_scratchpad.openedFor != g_form.getValue('opened_for')) { // If opened for value has changed

            if (subjectPersonValue == openedForValue) {
				// Render Modal window
                var msg = "You have set the Opened For to the Subject Person, which will grant them access to view the Case - are you sure?";
                g_modal.confirm(getMessage("Opened for Changed to Subject Person"), msg, function(confirmed) {

                    if (confirmed) {
                        g_form.save();
                    }
                });
            }


        }

    }


}
18 REPLIES 18

Here it is, a bit redesigned:

function onSubmit () {
	var data = getData(g_form, g_scratchpad);

	return !confirmationIsNeeded(data) || queryConfirmation(data);

	function getData (g_form, g_scratchpad) {
		var data = {
			'actionName': g_form.getActionName(),
			'actionConfirmed': g_scratchpad.u_actionConfirmed,
			'messages': {
				'Opened for Changed to Subject Person': '',
				'You have set the Opened For to the Subject Person, which will grant them access to view the Case - are you sure?': '',
			},
			'opened_for': g_form.getValue('opened_for'),
			'opened_for_hasChanged': g_scratchpad.u_opened_for_changed,
			'subject_person': g_form.getValue('subject_person'),
			'whenTranslated': showConfirmModal,
		};

		data.opened_for_isSameAs_subject_person = data.opened_for == data.subject_person;
		data.translationCount = Object.keys(data.messages).length;

		return data;
	}

	function confirmationIsNeeded (data) {
		return data.opened_for_hasChanged && data.opened_for_isSameAs_subject_person;
	}

	function queryConfirmation (data) {
		return data.actionConfirmed || translateAndConfirm(data)
	}

	function translateAndConfirm (data) {
		return Object.keys(data.messages).reduce(translateMessage(data), false);
	}

	function translateMessage (data) {
		return function (sum, key) {
			getMessage(key, setTranslatedMessage(data, key));
			return sum;
		};
	}

	function setTranslatedMessage (data, key) {
		return function (message) {
			data.messages[key] = message;
			data.translationCount--;
			return data.translationCount > 0 || data.whenTranslated(data);
		};
	}

	function showConfirmModal (data) {
		return runningInWSEnvironment() ? openWSModalAndAbort(data) : openUI16ModalAndAbort(data);
	}

	function runningInWSEnvironment () {
		return typeof g_aw != 'undefined';
	}

	function openWSModalAndAbort (data) {
		g_modal.confirm(data.messages['Opened for Changed to Subject Person'],
			data.messages['You have set the Opened For to the Subject Person, which will grant them access to view the Case - are you sure?'],
			onWSModalResult(data));
	}

	function onWSModalResult (data) {
		return function (acceptedOrDismissed) {
			return acceptedOrDismissed ? doComplete(data)() : doCancel();
		};
	}

	function openUI16ModalAndAbort (data) {
		var dialog = new GlideModal('glide_modal_confirm', false, 400);

		dialog.setTitle(data.messages['Opened for Changed to Subject Person']);

		dialog.setPreference('body', data.messages['You have set the Opened For to the Subject Person, which will grant them access to view the Case - are you sure?']);
		dialog.setPreference('focusTrap', true);
		dialog.setPreference('onPromptComplete', doComplete(data));
		dialog.setPreference('onPromptCancel', doCancel);

		dialog.render();

		return false;
	}

	function doComplete (data) {
		return function () {
			g_scratchpad.u_actionConfirmed = true;
			return runningInWSEnvironment() ? g_form.save(data.actionName) : gsftSubmit(null, g_form.getFormElement(), data.actionName);
		};
	}

	function doCancel () {
		getMessage('The record has not been saved', addInfoMessageTo(g_form));
	}

	function addInfoMessageTo (form) {
		return function addInfoMessage (message) {
			form.addInfoMessage(message);
		};
	}
}

Almost forgot, the solution needs an onChange Client Script on field "Opened for":

function onChange (control, oldValue, newValue, isLoading, isTemplate) {
	g_scratchpad.u_opened_for_changed = oldValue != newValue;
}

Hi @janos thanks for the script.

I updated it and also created the onchange client script for Opened for field

Works fine in native UI but in Workspace the modal popup is no longer appearing??

Did you make sure to configure the Client Scripts to run in all environments? Did you set their "UI type" attribute to "All"?

Also, in case you are configuring the Client Scripts for table sn_hr_core_case, did you make sure to mark the "Inherited" box? If you don't do that and the current record is in a child table of sn_hr_core_case, not the table itself, the script will not execute.