首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用FetchXml查询调用HTTPRequest检索CRMDynamic365Version8.2中的多条记录,但HTTPRequest不能工作

使用FetchXml查询调用HTTPRequest检索CRMDynamic365Version8.2中的多条记录,但HTTPRequest不能工作
EN

Stack Overflow用户
提问于 2021-01-19 10:24:11
回答 1查看 1.8K关注 0票数 0

例如,在表单上有两个字段new_paymenttypeid和new_revenueid (它们是它们的id/字段名),我为它们创建一个委托,因为每次它们更改时,我都希望它们一起添加并存储在一个局部变量中。

代码语言:javascript
复制
Total = Attribute["new_paymenttypeid"].getValue() + Attribute["new_revenueid"].getValue()

Xrm.Page.getAttribute("new_paymenttypeid").addOnChange(PaymentTypeDependency);  
Xrm.Page.getAttribute("new_revenueid").addOnChange(RevenueTypeDependency);

我从下面的方法中读取了TotalRevenue的值,使用FetchXml,在父案例下可能有很多包含TotalRevenue的记录。我正在尝试做一个RertriveAllREcords从FetchXml下面。然后将SUM(TotalRevenue)从SUM(new_paymenttypeid + new_revenueid)中减去,并将其存储在另一种形式的字段中。该进程将以javascript的形式运行,如在change/on save/on load上运行。但这不起作用。我们正在使用Dynamic365Version8.2。由于这个原因,我被困在了CalculateSurplusdeficit方法中。我打算使用XMLHttpRequest API。如果您可以通过向我展示如何使用下面的查询(fetchXml)检索多条记录并创建处理程序来指导我,那么当我向这些文本框中输入数据时,它们就会被触发,以便它们可以在框中添加数字。在保存时,从Totapplicationtotalrevenue总收入中减去添加的数字,并在该实体表单上设置一个具有此值的字段。

代码语言:javascript
复制
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;         
    }
   

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-19 21:30:33

下面的几处修改应该能使它发挥作用。

  1. 实体名应该是复数名称。验证这个:new_applicationrevenues

var query = "/api/data/v8.2/new_applicationrevenues?fetchXml=" + encodedFetchXML;

  1. 将请求方法更改为GET而不是POST。调用可以是异步的,所以我将其保留为true

req.open("GET",finalpathwithquery,true);

  1. 将代码移动到成功块中。

如果(this.status === 200) { var结果= JSON.parse(this.response);data = result;for(var i=0;i< data.value.length;i++){ Totapplicationtotalrevenue= Totapplication之和收入+ data.valuei.new_applicationtotalrevenue;}

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65789591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档