RE: Show loading image on save of Entity form
Hi Muhammad,
Please refer to the below javascript code which is working fine for me in Dynamics 365 Online V9.2. As per your explanation, the only place you're facing problem is identifying if there is any error window prompted while loading image is displayed. In below script, I have added timeout function for keep checking for any error pop-up window opened for every half second. If the function find any error window, it closes the loading image. But identifying the error window is using DOM, which is unsupported. You may need to find out a supported way in your CRM version, if any.
Also I have user Alert JS function to open saving dialog (refer: AlertJS if you're interested on this).
Register onsave() on form save and modifiedonChanged() on modifiedon field change.
Note: This is not a perfect solution. Because, loading image will remain open if the user closes the error pop-up window within half a second (before calling next timeout function checkErrorWindows). So, keeping minimum value for errorCheckInterval shall reduce this gap.
var onSaveCalled = false, errorOccurred = false, errorCheckInterval = 500;
function onsave() {
showSavingPopup();
}
function modifiedonChanged() {
if (onSaveCalled) {
errorOccurred = false;
// Close the 'Saving..' dialog window when Modified On field changed
new Dialog().hide();
}
onSaveCalled = false;
}
function showSavingPopup() {
onSaveCalled = true;
errorOccurred = false;
if (Xrm.Page.data.entity.getIsDirty()) {
// Open a dialog window with 'Saving..' title
var options = { title: "Saving..", preventCancel: false };
new Dialog(options).showLoading();
setTimeout(checkErrorWindows, errorCheckInterval);
}
}
function checkErrorWindows() {
// Check if there is any error pop-up window opened.
var maxPopupErrorWindows = 10;
for (var i = 0; i < maxPopupErrorWindows; i ) {
if (!errorOccurred) {
// This is unsupported. You may need to find a supported way to identify error window in your version of CRM.
var errorWindow = parent.window.document.getElementById("DialogContainer__" i "_popupContainer");
errorOccurred = errorWindow != null;
}
}
// If no error pop-up windows opened, keep checking for every errorCheckInterval
if (!errorOccurred && onSaveCalled)
setTimeout(checkError, errorCheckInterval);
// If any error pop-up windows found, close the 'Saving..' dialog window
else if (errorOccurred)
new Dialog().hide();
}