I want to restrict the popup message when someone exits the form without submitting it.

SAI_JAGADEESH15
Tera Contributor

Here, when I fill in the form and then change my mind and exit without submitting it, an out-of-the-box popup appears.

SAI_JAGADEESH15_0-1764793499117.png



Now I need to restrict that,

Kindly help me with the approach, Thanks in advance !

 

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

@SAI_JAGADEESH15 

why to remove that?

It's best to show that message to user as it's a warning to them.

keep that OOTB

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hello @Ankur Bawiskar 
The client want to remove it,
only for custom where page_id= pag_my_survey

 

@SAI_JAGADEESH15 

check other response where approach is shared

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Assuming you have cloned the Surveys and Assessments widget with id take_assessment and/or in your html template you have the spAssessmentModel directive (<sp-assessment-model> somewhere in there) then the same functionality is in a factory called spAsmtDirtyFormManager instead.

 

Logic is the same as in the other reply I wrote. Create the factory and add it as an angular provider to your widget. The script for this one:

function($rootScope, $window, spModal, i18n, cabrillo) {
	'use strict';
	var registeredForms = {};
	var nativeMobile;
	var isModalOpen = false;
	function registerForm(form) {
		registeredForms[form.getSysId()] = form;
	}
	function isNative(native) {
		nativeMobile = native;
	}
	function unregisterForms(sysIds) {
		sysIds.forEach(function (sysId) {
			delete registeredForms[sysId];
		});
	}
	function checkForDirtyForms() {
		var includedForms = Object.keys(registeredForms);
		var isFormDirty = false;
		for (var i in includedForms) {
			var fieldNames = registeredForms[includedForms[i]].getFieldNames();
			isFormDirty = fieldNames.some(function (fieldName) {
				var field = registeredForms[includedForms[i]].getField(fieldName);
				if ((typeof field.original_add_info !== 'undefined' && field.original_add_info !== field.add_info) || (field.type !== 'attachment' && field.originalValue !== field.value))
					return true;
			});
			if (isFormDirty)
				break;
		}
		return isFormDirty;
	}
	function clearCabrilloButtons() {
		if (cabrillo.isNative() && nativeMobile) {
			cabrillo.viewLayout.setTitle('');
			cabrillo.viewLayout.setNavigationBarButtons();
			cabrillo.viewLayout.setBottomButtons();
		}
	}
//remove/edit from here
	$rootScope.$on('$locationChangeStart', function (event, next) {
		if (Object.keys(registeredForms).length == 0)
			return;
		if (checkForDirtyForms()) {
			event.preventDefault();
			var options = {
				title: i18n.getMessage("Leave page?"),
				headerStyle: { border: 'none', 'padding-bottom': 0 },
				footerStyle: { border: 'none', 'padding-top': 0 },
				message: i18n.getMessage("Changes you made will be lost."),
				buttons: [
					{ label: i18n.getMessage("Cancel"), value: "cancel" },
					{ label: i18n.getMessage("Leave"), primary: true, value: "leave" }
				]
			};
			if (cabrillo.isNative() && nativeMobile) {
				var title = i18n.format("{0} {1}", options.title, options.message);
				if (confirm(title)) {
					clearCabrilloButtons();
					$window.location = next;
				}
			} else {
				isModalOpen = true;
				spModal.open(options).then(function (confirm) {
					if (confirm.value == "leave") {
						$window.location = next;
					}
					isModalOpen = false;
				}, function () {
					isModalOpen = false;
				});
			}
		} else {
			clearCabrilloButtons();
		}
	});
//remove/edit to here
	$window.onbeforeunload = function () {
		if (Object.keys(registeredForms).length != 0 && (checkForDirtyForms() && !isModalOpen))
			return true;
	}

	$window.onpagehide = function () {
		clearCabrilloButtons();
	}
	return {
		register: registerForm,
		unregisterForms: unregisterForms,
		isNative: isNative
	}
}

 

danishShaikh
Kilo Contributor

METHOD 1:

Disable popup completely in Service Portal

 

Add this to a Widget Client Script or a Theme JS Includes file

window.onbeforeunload = function() {

    return null;  // disables the warning popup

};

 

METHOD 2:

Disable only for a specific page

Go to:

Service Portal -- Pages -- Select your page -- JS Includes

Add:

window.onbeforeunload = null;