问题描述
例如,在表单上有两个字段 new_paymenttypeid 和 new_revenueid (这些是它们的 ID/字段名称) 我正在为他们创建一个委托,因为每次他们更改时,我希望他们将它们加在一起并将其存储在一个局部变量中。
Total = Attribute["new_paymenttypeid"].getValue() + Attribute["new_revenueid"].getValue()
Xrm.Page.getAttribute("new_paymenttypeid").addOnChange(PaymentTypeDependency);
Xrm.Page.getAttribute("new_revenueid").addOnChange(RevenueTypeDependency);
我使用 FetchXml 从下面的方法中读取了 TotalRevenue 的值 在父案例下可能有许多包含 TotalRevenue 的记录。 我想从下面的 FetchXml 做一个 RertriveAllREcords。然后从 SUM(new_paymenttypeid + new_revenueid) 中减去 SUM(TotalRevenue) 并将其存储在另一个表单的字段中。该过程将从另一种形式作为 javascript 在更改/保存/加载时运行。但它不起作用。 我们使用的是 Dynamics 365 8.2 版。由于它,我被困在方法CalculateSurplusdeficit中。 我打算使用 XMLHttpRequest API。如果您可以通过向我展示如何使用下面的查询 (fetchXml) 检索多个记录并创建处理程序,以便在我将数据输入到这些文本框中时它们在更改时被触发,以便它们可以在那些盒子。并在保存时从 Totapplicationtotalrevenue 中减去相加的数字,并在此实体表单上使用此值设置一个字段。
function CalculateSurplusDeficit()
{
var caseId = Xrm.Page.data.entity.getId(); // case guid
//var grantYearLookup = Xrm.Page.getAttribute("new_grantyear").getValue();
//Retrieve Account Names whose Account Name Starts with word "M" using WEB API
//Step-1 : create fetch xml in adv find and replace all double quotes with single quote inside properties to shape it as a string
var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'> " +
"<entity name='new_applicationrevenue'> " +
" <attribute name='new_applicationtotalrevenue' /> " +
" <attribute name='new_grantyear' /> " +
" <order attribute='title' descending='false' /> " +
" <filter type='and'> " +
" <condition attribute='new_parentcase' operator='eq' uitype='incident' value='{'" + caseId + "}' /> " +
" </filter> " +
" </entity> " +
"</fetch> " ;
//Step-2 : encode URI : var encodedFetchXML = encodeURI(fetchxml)
var encodedFetchXML = encodeURI(fetchXml);
var data = {
"EntityId": caseId
};
//Step-3 : create a query path with query and odata partial uurl : var query = "/api/data/v8.0/accounts?fetchXml="+encodedFetchXML ;
var query = "/api/data/v8.2/Revenue?fetchXml=" + encodedFetchXML;
//Step-4 : create complete path : var path = Xrm.Page.context.getClientUrl() + query ;
var finalpathwithquery = Xrm.Page.context.getClientUrl() + query;
//Step-5 : create a XmlHttpRequest to retrieve data
var data = null;
var isAsync = false;
var req = new XMLHttpRequest();
req.open("POST",finalpathwithquery);
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);
data = result;
}
else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
//Step-6 show return result values
var acclist = null;
for(var i=0;i< data.value.length;i++){
Totapplicationtotalrevenue= Totapplicationtotalrevenue + data.value[i].new_applicationtotalrevenue;
}
}
解决方法
下面的一些修改应该可以使它工作。
-
实体名称应为复数名称。验证这一点:
new_applicationrevenues
var query = "/api/data/v8.2/new_applicationrevenues?fetchXml=" + encodedFetchXML;
-
将请求方法更改为
GET
而不是POST
。调用可以是异步的,因此我将其保留为true
req.open("GET",finalpathwithquery,true);
-
将代码移到成功块内
if (this.status === 200) { var result = JSON.parse(this.response); data = result; for(var i=0;i< data.value.length;i++){ Totapplicationtotalrevenue= Totapplicationtotalrevenue + data.value[i].new_applicationtotalrevenue; } }