Client Script - Discard Changes to Record

David Cole1
Tera Expert

I have a UI Page that is meant to be a re-usable recreation of the OOB "dirty page handler" Modal:

find_real_file.png

The client script of the UI Page is as follows:

addLoadEvent(function() {
	// Show message with parent record display value
	$j("#dirty_form_modal_confirmation_description").text(
		formatMessage(getMessage("Do you want to save changes to " + g_form.getDisplayValue() + " before leaving this page?"))
	);
});

function save() {
	//Redir to RITM after save
	var afterSaveURL = new GlideURL();
	afterSaveURL.setFromString('sc_req_item.do?sys_id=' + g_form.getValue('request_item'));

	addHidden(g_form.getFormElement(), "sysparm_goto_url", afterSaveURL.getURL());
	g_form.save();
}

//Leave record and discard changes
function discard() {
	//Redir to RITM after discard
	var afterSaveURL = new GlideURL();
	afterSaveURL.setFromString('sc_req_item.do?sys_id=' + g_form.getValue('request_item'));
	addHidden(g_form.getFormElement(), "sysparm_goto_url", afterSaveURL.getURL());
	
	// -- Here: run a save or submit after discarding changes made to the record --
}

function cancel() {
	GlideDialogWindow.get().destroy();
}

 

The save() and cancel() functions work as I would expect them to, however I am stuck as to how to discard changes made to the form from a client script. I have tried to find the source for the OOB Modal, but have been unsuccessful.

The UI Page is being launched as a GlideModal from a Client/Server hybrid UI Action:

//Client-side 'onclick' function
function runClientCode(){
	if(g_form.modified){
		g_form.hideAllFieldMsgs();
		var dirtyFormUiPage = "record_save_discard_cancel";
		var dialogWindow = new GlideModal(dirtyFormUiPage, false, "modal-md");
		dialogWindow.setTitle(getMessage("Save changes"));
		dialogWindow.setPreference("displayValue", g_form.getDisplayValue());
		dialogWindow.setPreference("focusTrap", true);
		dialogWindow.render();
	}
	else{
		//Call the UI Action and skip the 'onclick' function
		gsftSubmit(null, g_form.getFormElement(), 'Open RITM'); //MUST call the 'Action name' set in this UI Action
	}
}

//Server side redirect code after this...

I'd appreciate any help 🙂

1 ACCEPTED SOLUTION

After stepping through the OOB Discard button, I figured out the correct way to discard. Their code:

 

var gm = new GlideModal('dirty_form_modal_confirmation');
gm.setPreference('focusTrap', true);
gm.setPreference('autoFocus', false);
gm.setSize(200);
gm.setTitle(getMessage('Save changes'));
var content = $j(new XMLTemplate('dirty_form_modal').evaluate({displayValue: g_form.getDisplayValue()}));
content.on('click', 'button', function(evt) {
var button = evt.target;
if (button.getAttribute('data-action') == 'discard') {
g_form.modified = false;
discardCallback();
} else if (button.getAttribute('data-action') == 'save') {
button.disabled = true;
saveCallback();
}
});
window.nowapi.g_popup_manager && window.nowapi.g_popup_manager.destroypopDiv();
gm.renderWithContent(content);
}
function checkSaveURL(tableName, url) {
checkDirtyForm(function save() {
saveAndRedirect(tableName, url);
}, function discard() {
g_navigation.open(url);
});

 

Transformed into the UI Page:

 

//Leave record and discard changes
function discard() {
	// -- Here: run a save or submit after discarding changes made to the record --
	g_form.modified = false;
	g_navigation.open('sc_req_item.do?sys_id=' + g_form.getValue('request_item'), "_self");
}

 

 

View solution in original post

2 REPLIES 2

David Cole1
Tera Expert

I've made a bit of progress, however have hit a new roadblock.

Using a raw window.location will ignore changes, however I get the following:

find_real_file.png

 

According to a few sources I found, changing window.onbeforeunload to an empty function should remove this message:

 

//Leave record and discard changes
function discard() {
	// -- Here: run a save or submit after discarding changes made to the record --
	window.onbeforeunload = function() { 
		return true; 
	};
	window.location =  'sc_req_item.do?sys_id=' + g_form.getValue('request_item');
}

 

However even after doing that, the message still appears.

After stepping through the OOB Discard button, I figured out the correct way to discard. Their code:

 

var gm = new GlideModal('dirty_form_modal_confirmation');
gm.setPreference('focusTrap', true);
gm.setPreference('autoFocus', false);
gm.setSize(200);
gm.setTitle(getMessage('Save changes'));
var content = $j(new XMLTemplate('dirty_form_modal').evaluate({displayValue: g_form.getDisplayValue()}));
content.on('click', 'button', function(evt) {
var button = evt.target;
if (button.getAttribute('data-action') == 'discard') {
g_form.modified = false;
discardCallback();
} else if (button.getAttribute('data-action') == 'save') {
button.disabled = true;
saveCallback();
}
});
window.nowapi.g_popup_manager && window.nowapi.g_popup_manager.destroypopDiv();
gm.renderWithContent(content);
}
function checkSaveURL(tableName, url) {
checkDirtyForm(function save() {
saveAndRedirect(tableName, url);
}, function discard() {
g_navigation.open(url);
});

 

Transformed into the UI Page:

 

//Leave record and discard changes
function discard() {
	// -- Here: run a save or submit after discarding changes made to the record --
	g_form.modified = false;
	g_navigation.open('sc_req_item.do?sys_id=' + g_form.getValue('request_item'), "_self");
}