weird Error TypeError: Cannot read property 'setValue' of null at onSuccessMapUnitFields [closed]

不问归期 提交于 2021-01-28 05:43:51

问题


This code was running fine until I decided to add 4 more lines of code, so I removed them. But I got this error, funny thing is I added my lines on the Form_onload function. I need help with this.

function Form_OnLoad() {
    debugger;
        if (Xrm.Page.ui.getFormType() == 1) {
            Xrm.Page.getAttribute("quantity").setValue(1);
            Xrm.Page.getAttribute("quantity").setSubmitMode("always");
            Xrm.Page.getAttribute("isproductoverridden").setValue(true);

  Xrm.Page.getAttribute("isproductoverridden").setSubmitMode("always");
        }
        else {
            ActOnFields("disable", "ir_unit");
            ActOnFields("show", "salesorderid");
        }
    }

    function Unit_OnChange() {
        var Unit = Xrm.Page.getAttribute("ir_unit").getValue();
        if (Unit != null && Unit.length > 0) {
            var lookupid = Unit[0].id;
            var oDataSetName = "ir_unitSet";
            var columns = "ir_AmountRate,ir_Expenses,ir_PublishedRate";
            var filter = "ir_unitId eq (guid'" + lookupid + "')";
            retrieveMultiple(oDataSetName, columns, filter, "", onSuccessMapUnitFields);
            Xrm.Page.getAttribute("productdescription").setValue(Unit[0].name);
            Xrm.Page.getAttribute("productdescription").setSubmitMode("always");
        }
        else {
            Xrm.Page.getAttribute("priceperunit").setValue(null);
            Xrm.Page.getAttribute("priceperunit").setSubmitMode("always");
            Xrm.Page.getAttribute("baseamount").setValue(null);
            Xrm.Page.getAttribute("baseamount").setSubmitMode("always");
        }
    }
    function onSuccessMapUnitFields(data, textStatus, XmlHttpRequest) {
        if (data && data.length > 0) {
            var price = 0;
            //if (data[0].ir_AmountRate != null && data[0].ir_AmountRate.Value != null) {
            //price += parseFloat(eval(data[0].ir_AmountRate.Value));
            //}
            if (data[0].ir_PublishedRate != null) {
                price += parseFloat(eval(data[0].ir_PublishedRate));
            }
            if (data[0].ir_Expenses != null && data[0].ir_Expenses.Value != null) {
                price += parseFloat(eval(data[0].ir_Expenses.Value));
            }
            Xrm.Page.getAttribute("priceperunit").setValue(price);
            Xrm.Page.getAttribute("priceperunit").setSubmitMode("always");
            Xrm.Page.getAttribute("baseamount").setValue(price);
            Xrm.Page.getAttribute("baseamount").setSubmitMode("always");
            Xrm.Page.getAttribute("ir_publishedrate").setValue(price);
            Xrm.Page.getAttribute("ir_publishedrate").setSubmitMode("always");
        }
    }

回答1:


Add debugger; inside Unit_OnChange function & the success callback function onSuccessMapUnitFields, then while debugging you can figure out that which one of the 3 fields among priceperunit, baseamount, ir_publishedrate is throwing this error.

Also verify if you are calling ActOnFields method to hide those 3 fields anywhere else before calling this setValue(price). Because if the control/attribute is not visible in the form - Xrm.Page.getAttribute returns null, then when you are trying to setValue it fails with this error.

Failsafe approach:

if(Xrm.Page.getAttribute("baseamount") != null){
       Xrm.Page.getAttribute("baseamount").setValue(price);
       Xrm.Page.getAttribute("baseamount").setSubmitMode("always");
}


来源:https://stackoverflow.com/questions/50519357/weird-error-typeerror-cannot-read-property-setvalue-of-null-at-onsuccessmapun

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!