简体   繁体   中英

Values seems changing straight after Initial Save in Dynamics CRM Opportunity

I'm doing a Service Call to retrieve the Account details (currency, discount associated with the account) on the Selection of the Account lookup on the Opportunity form (form type == 1 // Create) using Web API in CRM 2016 On-Premise . Everything is working fine but when the opportunity is saved initially it's straight away coming up with unsaved changes next to the Save button after the initial save which is forcing me to do another save(abnormal behaviour).I'm not so sure what value is changing straightaway after initial save.

The Service Call is Synchronous and is being triggered on the change of the Account Lookup, well before the initial save. Any Help Appreciated!.

function SetOpportunityCurrencyAndDiscount(){
    var accountId = (GetValue("vm_accountid"))[0].id;
    var result = RetrieveRecord("account", null, accountId.slice(1,-1));
    var accountDiscount = result["vm_accountdiscount"];
    var transactionCurrencyId = result["_transactioncurrencyid_value"];
    var currencyName =  result["_transactioncurrencyid_value@OData.Community.Display.V1.FormattedValue"];
    SetValue("vm_discount", accountDiscount);
    Xrm.Page.getAttribute("transactioncurrencyid").setValue([{ id: transactionCurrencyId, name: currencyName, entityType: "transactioncurrency"}]); }

function RetrieveRecord(recordType, alternateKey, accountId){
    var result = null;
    var entityType = recordType;
    var query = null;
    if(alternateKey != null && agencyId == null)
        query = "/api/data/v8.0/accounts(emailaddress1='"+alternateKey+"')?$select=name,accountid,_transactioncurrencyid_value,vm_agencydiscount";
    else
        query = "/api/data/v8.0/accounts("+agencyId+")?$select=name,accountid,_transactioncurrencyid_value,vm_agencydiscount";
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + query, false);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");
    req.onreadystatechange = function () {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                result = JSON.parse(this.response);
            }
            else {
                alert(this.statusText);
            }
        }
    };
    req.send();
    return result; 
}

After you save your record and the form is dirty again, open dev tools and paste this into the console. It will show you which fields are dirty.

function showDirtyFields() { 
    var Xrm = Array.prototype.slice.call(document.querySelectorAll('iframe')).filter(function(d) {
        return d.style.visibility !== 'hidden';
    })[0].contentWindow.Xrm;
    var message='The following fields are dirty: \n';
    Xrm.Page.data.entity.attributes.forEach(function(attribute,index){
        if(attribute.getIsDirty()==true){message+="\u2219 "+attribute.getName()+"\n";}
    });
    Xrm.Utility.alertDialog(message);
}
showDirtyFields();

Another way of accomplishing the same thing is to turn on auditing for the entity. The audit log will show you which fields were submitted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM