Create a new form without reloading after saving
works well in crm 2011 but not in crm 2013 online
Whenever possible, Entity create New entry form i show only:
1. Some attributes (all other fields / section / tabs in hidden form)
2. and button Html , e.g.
function OnFormLoad(){
if(Xrm.Page.ui.getFormType() == 1 ) {
SetRequiredLevelToNone();
createHtmlButton("Save");
HideTabsAndSections()
}
else {
}
}
When the Html button is clicked , the following function starts.
function showWholeForm() {
Xrm.Page.data.entity.save();
}
I want to show all the form fields after saving, so I need to reload the whole form.
How it works in crm 2011
source to share
The method save
in CRM 2013 no longer refreshes the page. The new method should be used in this case as follows: refresh
Xrm.Page.data.refresh(save).then(successCallback, errorCallback);
- save : a boolean value that should be passed as true if the data should be saved after updating it.
- successCallback : function to call on success
- errorCallbak : a function to call when the operation fails, which will be passed an object with 2 properties - an error code (number) and a localized error message (string)
You can find the method described here: http://msdn.microsoft.com/en-us/library/dn481607(v=crm.6).aspx
source to share
I think you must first disable the auto-save, if you want consistency with CRM 2011 .
function OnSaveDisableAutoSave(eventArgs) {
var saveType = eventArgs.getEventArgs().getSaveMode();
if (saveType == 70 ||saveType == 2)
{ //Disable AutoSave
eventArgs.preventDefault();
}
}
and then
function showWholeForm() {
Xrm.Page.data.refresh(true).then(successCallback, errorCallback);
}
source to share
To fix this, you can do:
var saved = false;
function onLoad(){
checkIfFormShouldBeReadOnly();
attachEvent();
}
function attachEvent() {
Xrm.Page.data.entity.addOnSave(executeOnSave);
}
function executeOnSave(saveExecution) {
if (!saved) {
saved = true;
Xrm.Page.data.save().then(
function () {
checkIfFormShouldBeReadOnly();
},
function (errorCode, message) {
Xrm.Page.data.refresh();
}
);
}
}
source to share