TypeError:当查找字段清除时,无法读取null的属性'0' - 动态CRM

时间:2017-03-04 21:29:22

标签: javascript dynamics-crm

所以我有一个供应商名称Lookup字段,每当我为其选择一个值时,它将填充费用类别查找。所以我的问题是当我清除供应商名称查找时,我收到此错误“TypeError:无法在setExpenseCategorybasedonVendor读取null的属性'0'”。

已经尝试过清除费用类别

的价值
lookup1.reset();
value.reset();
cse_expense_category_value.reset();
cse_expense_category_value.clear();
nullcse_expense_category_value.setValue("");
cse_expense_category_value.setValue(null);
document.getElementById("cse_expense_category").reset();
document.getElementById("cse_expense_category").null();
document.getElementById("cse_expense_category").clear();
document.getElementById("cse_expense_category").setValue("");       
document.getElementById("cse_expense_category").setValue(null); 
Xrm.Page.getElementById("cse_expense_category").reset();    
Xrm.Page.getElementById("cse_expense_category").clear();    
Xrm.Page.getElementById("cse_expense_category").null(); 
Xrm.Page.getElementById("cse_expense_category").setValue("");   
Xrm.Page.getElementById("cse_expense_category").setValue(null);

我的完整代码

function makeRequest(method, url) {
return new Promise(function (resolve, reject) {
    var xhr = new XMLHttpRequest();
    xhr.open(method, url);
    xhr.setRequestHeader("OData-MaxVersion", "4.0");
    xhr.setRequestHeader("OData-Version", "4.0");
    xhr.setRequestHeader("Accept", "application/json");
    xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    xhr.onload = function () {
        if (this.status >= 200 && this.status < 300) {

            resolve(xhr.response);
        } else {
            reject({
                status: this.status,
                statusText: xhr.statusText
            });
        }
    };
    xhr.onerror = function () {
        reject({
            status: this.status,
            statusText: xhr.statusText
        });
    };
    xhr.send();
});
}

function setExpenseCategorybasedonVendor() {
var lookup1 = Xrm.Page.getAttribute("cse_vendor_name").getValue()[0].id;
var clientUrl = Xrm.Page.context.getClientUrl();
var query = clientUrl + "/api/data/v8.0/cse_vendormasters(" +           lookup1.slice(1, -1) + ")?$select=_cse_expense_category_value";
makeRequest('GET', query)
.then(function (res) {
var res2 = JSON.parse(res);
var guid = res2._cse_expense_category_value;
var query2 = clientUrl + "/api/data/v8.0/cse_expensemasters(" + guid + ")?     $select=cse_name";
makeRequest('GET', query2)
.then(function (response) {
var res3 = JSON.parse(response);
var value = new Array();
value[0] = new Object();
value[0].id = guid;
value[0].name = res3.cse_name;
value[0].entityType = "cse_expensemasters";
if (lookup1 != null){
Xrm.Page.getAttribute("cse_expense_category").setValue(value);
Xrm.Page.getControl("cse_expense_category").setFocus();
Xrm.Page.getControl("cse_amount").setFocus();
}
else{
Xrm.Page.getElementById("cse_expense_category").clear();    
}
})
.catch(function (err) {
console.error('there was an error!', err.statusText);
});
})
.catch(function (err) {
console.error(' there was an error!', err.statusText);
});
}

1 个答案:

答案 0 :(得分:0)

乍一看,您似乎没有使用正确的JavaScript API。

正如Guido在上面评论的那样,你应该只使用:Xrm.Page.getAttribute来与表单上的值进行交互。不支持直接使用getElementById,可能会导致意外行为。

        //Get reference to control and attribute
        var cse_expense_category = Xrm.Page.getAttribute('cse_expense_category');
        var cse_expense_categoryControl = Xrm.Page.getControl('cse_expense_category');

        //To get or set the form or set requirement
        cse_expense_category.setRequiredLevel('none');
        cse_expense_category.setValue(null);

        //To interactive with the control (enable, disable, hide etc.)
        cse_expense_categoryControl.setDisabled(false);

客户端sdk引用可在此处找到:https://msdn.microsoft.com/en-us/library/gg328255.aspx?f=255&MSPPError=-2147217396

我个人发现这个CRM SDK Cheat Sheet非常有用(它打印到a4页): http://crmunwrapped.blogspot.com.au/2015/03/crm-2015-client-api-cheat-sheet.html