2
votes

I'm trying to add versioning functionality to a custom entity, MFAs, and I'm running into a very odd problem. I have a javascript webresource being called from two places: an onSave event on the form, and as the action of a custom ribbon button. Specifically, the onSave event calls captureSave, while the ribbon button calls makeARevision.

When called from the save button/event, everything works as expected; all information, including the new changes, are pulled to a new record and saved there, while the original record is closed without the changes being saved, and without a prompt to save. However, when called via the custom ribbon button, any unsaved changes do not get brought over to the new record, and the old record prompts for saving. Furthermore, even if the user chooses to save the changes to the old record, the changes are not saved, and the form doesn't automatically close.

The following code is the webresource in question. company_MFASaveOrRevise is just an html page that asks the user whether they want to save the record or create a new revision. Any ideas on what's causing the differences or how to resolve them is appreciated.

function captureSave(executionContext) {
    if (Xrm.Page.ui.getFormType() != 1 && Xrm.Page.data.entity.getIsDirty()) {
        var retVal = showModalDialog(Xrm.Page.context.getServerUrl() + '/Webresources/company_MFASaveOrRevise', null, 'dialogWidth: 300px; dialogHeight: 100px');
        if (retVal == "Revise") {
            executionContext.getEventArgs().preventDefault();
            makeARevision();
        }
        else if (retVal == "Save") {
        }
    }
}

function createLookupValue(oldLookup) {
    var lookupVal = new Object();
    lookupVal.Id = oldLookup.id;
    lookupVal.LogicalName = oldLookup.entityName;
    lookupVal.Name = oldLookup.Name;
    return lookupVal;
}

function makeARevision() {
    var revisedMFA = {};
    revisedMFA['company_mfaname'] = Xrm.Page.data.entity.attributes.get('company_mfaname').getValue();
    revisedMFA['company_mfadate'] = Xrm.Page.data.entity.attributes.get('company_mfadate').getValue();
    revisedMFA['company_estimatedliqdate'] = Xrm.Page.data.entity.attributes.get('company_estimatedliqdate').getValue();
    revisedMFA['company_actualliqdate'] = Xrm.Page.data.entity.attributes.get('company_actualliqdate').getValue();
    revisedMFA['company_mfanumber'] = Xrm.Page.data.entity.attributes.get('company_mfanumber').getValue();
    revisedMFA['company_revisionno'] = Xrm.Page.data.entity.attributes.get('company_revisionno') == null ? 0 : Xrm.Page.data.entity.attributes.get('company_revisionno').getValue() + 1;
    revisedMFA['company_requester'] = createLookupValue(Xrm.Page.data.entity.attributes.get('company_requester').getValue()[0]);
    revisedMFA['company_mfapreviousrev'] = Xrm.Page.data.entity.attributes.get('company_totalmfatodate').getValue();
    revisedMFA['company_contract'] = createLookupValue(Xrm.Page.data.entity.attributes.get('company_contract').getValue()[0]);

    $.ajax({
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        datatype: 'json',
        url: getODataUrl() + '/' + 'company_mfaSet',
        data: JSON.stringify(revisedMFA),
        beforeSend: function (XMLHttpRequest) {
            //Specifying this header ensures that the results will be returned as JSON.
            XMLHttpRequest.setRequestHeader('Accept', 'application/json');
        },
        success: function (data, textStatus, request) {
            Xrm.Utility.openEntityForm("company_mfa", data.d.company_mfaId.toUpperCase());
            var attributes = Xrm.Page.data.entity.attributes.get();
            for (var i in attributes) {
                attributes[i].setSubmitMode('never');
            }
            Xrm.Page.ui.close();
        },
        error: function (request, textStatus, errorThrown) {
            alert(errorThrown);
            //alert("There was an error creating the revision");
        }
    });
}

Edit: I had debugger; inserted in various places and was using VS2012 debugger, and found that the attributes were being properly set not to submit, but apparently that didn't stop the confirmation dialog from popping up (even though it works when the webresource is called through the save button). Additionally, Xrm.Page.data.entity.attributes.get(attributeName) returns the post-changes values when called during onSave event, but pre-change values when called from the ribbon. I still don't know why or how to fix it though. Is there something else I should look for?

1
can you add alert(Xrm.Page.data.entity.getDataXml() as the first line of makeARevision() and see if the XML has the new or old values?Nicknow
@Nicknow when run from the onSave event, getDataXml() returns the changes made since the last save. When run from the ribbon button, it contains nothing, regardless of whether changes have been made.BThompson

1 Answers

0
votes

Use F12 to debug your code when being called from the ribbon (just remember since it is in the ribbon, your javascript code will be in a dynamic script / script block).