Javascript - 在动态 CRM 中选择查找字段时自动填充字段
Posted
技术标签:
【中文标题】Javascript - 在动态 CRM 中选择查找字段时自动填充字段【英文标题】:Javascript - autofill a field when lookup field selected in Dynamic CRM 【发布时间】:2017-03-03 17:41:07 【问题描述】:我有这个检查交易实体,在那个“cse_vendor_name”(供应商主实体)和“cse_expense_tracker”上会有一个查找字段,所以每次我选择一个 cse_vendor_name 时,它都会自动填充 cse_expense_tracker 的字段。我已经尝试过这个工作代码,但它没有,
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 setParentAccountbasedonPrimaryContact()
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_vendormaster(" + 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_checkingaccounttransaction(" + guid + ")?$select=cse_vendor_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.name;
value[0].entityType = "cse_checkingaccounttransaction";
Xrm.Page.getAttribute("cse_expense_category").setValue(value);
)
.catch(function (err)
console.error('there was an error!', err.statusText);
);
)
.catch(function (err)
console.error(' there was an error!', err.statusText);
);
【问题讨论】:
你能更清楚地定义你的目标是什么吗? 你得到什么错误?你试过调试吗? @Jackstine 好的。实体名称:检查帐户字段:cse_vendor_name(查找供应商主文件)cse_expense_category(连接到 cse_vendor_name 的字段) @HenkvanBoeijen 没有错误,但它不起作用 【参考方案1】:所以这就是我选择查找字段时填写查找字段的答案。这个解决方案的唯一问题是,该领域有一个问题说 “查找控件错误:无法将 typename= 的项目添加到查找控件”所以当我单击保存时。它将再次进入查找以选择数据。
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";
Xrm.Page.getAttribute("cse_expense_category").setValue(value);
)
.catch(function (err)
console.error('there was an error!', err.statusText);
);
)
.catch(function (err)
console.error(' there was an error!', err.statusText);
);
【讨论】:
对不起,我在错误的问题中发布了答案,我应该在我的问题中发布我的答案,但没有看到我在不同的问题中发布了它以上是关于Javascript - 在动态 CRM 中选择查找字段时自动填充字段的主要内容,如果未能解决你的问题,请参考以下文章
Dynamics CRM项目实例之十:CRM 2015的捆绑销售在订单中的效果