OnSave event on a custom entity form in CRM 2015 enters in a recursive trigger

2.3k views Asked by At

I have an event registered onSave on form of a custom entity on a crm 2015 on premise organization, which seems to enter in a recursive call. After i hit save the event keeps call it self, until i kill IE through task manager or the IE crashes with memory stack full. I have checked and it doesn't seem to be related to what i have in the function, meaning i am not triggering in any way the on save event again.

Here is the function code:

function contract_OnSave(context) {

    var eventArgs = context.getEventArgs();
    if (eventArgs.getSaveMode() == 1) {

        if (Xrm.Page.getAttribute("new_field").getValue() != null) {
            var mort = Xrm.Page.getAttribute("new_field").getValue();

            if (mort == true) {


                Xrm.Utility.confirmDialog("Would you like to open a new window for a Case?",
                    saveFormOnOpenCase,
                    callback)
            }
        }
    }
}
function saveFormOnOpenCase()
{
    Xrm.Page.data.save();
    var parameters = {};
    parameters["new_subjectnew"] = 100000002;
    // Open the window.
    var windowOptions = {
        openInNewWindow: true
    };
    Xrm.Utility.openEntityForm("incident", null, parameters, windowOptions);
}
function callback() {

}

It seemed to me like it is a bug in CRM 2015, after which i checked the version of the CRM, and noticed it is the RTM version. I suggested a CRM upgrade to Update 1 and it seemed as the most logical step to take. But even after the upgrade, the issue is still there.

I tried registering the same function on OnChange event on the field, and it was working fine, except the openInNewWindow option, of the openEntityForm function, which does not open a new window, but that is a different issue.

I am using IE 11.

Can someone please confirm if this is happening in another CRM 2015 environment as well, or if i am missing something?

2

There are 2 answers

0
Bojan Borisovski On BEST ANSWER

The actual problem which makes the OnSave function go into a recursive call is in the following:

var windowOptions = {
    openInNewWindow: true
};    
Xrm.Utility.openEntityForm("incident", null, parameters, windowOptions);

As mentioned in the link

https://msdn.microsoft.com/en-us/library/jj602956.aspx.

The windowOptions parameter in the openEntityForm should open the new form in a new window but for some reason it doesn't, although the CRM is updated with update 1 for CRM 2015. Because the form doesn't open in a new window, when leaving the current form, an auto-save event is fired which causes the form to enter the recursion and initiate the OnSave event.

I fixed the issue by using pure a javascript function (not using the CRM SDK) to open the form in a new window, which doesn't initiate the OnSave event.

2
Henk van Boeijen On

In the OnSave event you ask the user for confirmation:

Xrm.Utility.confirmDialog("Would you like to open a new window for a Case?",
                saveFormOnOpenCase,
                callback)

When the user selects OK, this dialog executes function saveFormOnOpenCase. The first instruction this function executes is:

Xrm.Page.data.save();

... triggering the save event again.