如何使用JavaScript在Dynamics 365 CRM内部部署中填写表单上的父查找?

如何使用JavaScript在Dynamics 365 CRM内部部署中填写表单上的父查找?,javascript,dynamics-crm,dynamics-365,dynamics-crm-2016,Javascript,Dynamics Crm,Dynamics 365,Dynamics Crm 2016,假设我有3个相关实体(PhoneCall帐户联系人)。关于详细信息,我必须在电话呼叫表单中说,我有一个与account相关的自定义查找字段,另一个与contact相关,最后一个与account相关,用于父帐户。现在,我想要一个解决方案,当我填写帐户字段后,用正确的数据填写父帐户查找,或者如果我首先填写联系人查找,用正确的数据填写帐户,然后用正确的数据填写父帐户字段时,可以帮到我。我搜索了很多方法,但我找不到任何方法来找到正确的家长帐户并填充我的查找,即使我使用业务规则,但它对我没有帮助 现在,我

假设我有3个相关实体(PhoneCall帐户联系人)。关于详细信息,我必须在电话呼叫表单中说,我有一个与account相关的自定义查找字段,另一个与contact相关,最后一个与account相关,用于父帐户。现在,我想要一个解决方案,当我填写帐户字段后,用正确的数据填写父帐户查找,或者如果我首先填写联系人查找,用正确的数据填写帐户,然后用正确的数据填写父帐户字段时,可以帮到我。我搜索了很多方法,但我找不到任何方法来找到正确的家长帐户并填充我的查找,即使我使用业务规则,但它对我没有帮助


现在,我在许多网站上都看到有人建议使用CRM REST BUILDER。我曾经使用过它,但它无法解决我的问题。

您需要的是一个Javascript函数,用于在更改第一次查找时触发,以从父记录中查询必要的字段,并将其填充到当前子记录表单中


确保更改字段名称和自定义设置的准确性。

感谢@ArunVinoth提供您的解决方案。这非常有帮助,为了更好地实现,我使用crmrestbuilder来消除任何错误。
function fillParentAccount() {

var lookup= Xrm.Page.getAttribute("accountfieldname").getValue();  //you will get the id with exxtra double quotes or square brackets by doing get value hence you to make it readable by CRM , you must slice it. i have use the below method:
var newid = lookup[0].id.slice(1, -1);  // you will get perfect id like "EDCJDKDJDKJDJDKJDJKD" here.
var req = new XMLHttpRequest(); //once you have the id , you have frame to make a webapi GET call by proving the newid we got.

req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.1/accounts(" + newid + ")?$select=_parentaccountfieldname_value", true);
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=\"*\"");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var result = JSON.parse(this.response); // you will get the retrieved value in object we stored in result var.
var retrivedvalue= result._parentaccountfieldname_value; //get the id of the field
var retrivedformatedvalue= result["_parentaccountfieldname_value@OData.Community.Display.V1.FormattedValue"]; //get the formatted name of the field
if (retrivedvalue!= null) {
var value = new Array();
value[0] = new Object();
value[0].id = retrivedvalue;
value[0].name = retrivedformatedvalue;
value[0].entityType = "account";
Xrm.Page.getAttribute("parentaccountfield").setValue(value); //set the lookup value finally
}
else
alert("some textt!!!!!!") // optional
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();