简体   繁体   English

使用调用 HTTPRequest 的 FetchXml 查询检索 CRM Dynamics 365 版本 8.2 中的多条记录,但 HTTPRequest 将不起作用

[英]Retrieve multiple records in CRM Dynamics 365 version 8.2 using FetchXml query calling HTTPRequest but HTTPRequest will not work

On a form there are for example two fields new_paymenttypeid and new_revenueid (these are their id's/field names) I am creating a delegate for them as each time they change I want them to add together and store it in a local variable.例如,在一个表单上,有两个字段 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);

I read the value of TotalRevenue from the method below using FetchXml There could be many records containing TotalRevenue under the parent case.我使用 FetchXml 从下面的方法中读取了 TotalRevenue 的值。在父案例下可能有很多包含 TotalRevenue 的记录。 I am tring to do a RertriveAllREcords from the FetchXml below.我正在尝试从下面的 FetchXml 中执行 RertriveAllREcords。 And then Subtract SUM(TotalRevenue) from SUM(new_paymenttypeid + new_revenueid) and store it in a field of another form.然后从 SUM(new_paymenttypeid + new_revenueid) 中减去 SUM(TotalRevenue) 并将其存储在另一个表单的字段中。 The process will run from this other form as javascript on change/on save/on load.该过程将以 javascript 在更改/保存/加载时从其他形式运行。 But it just does not work.但它只是行不通。 We are using Dynamics 365 version 8.2.我们使用的是 Dynamics 365 版本 8.2。 I am stuck in the method CalculateSurplusdeficit due to it.由于它,我被困在CalculateSurplusdeficit方法中。 I intend to use the XMLHttpRequest API.我打算使用 XMLHttpRequest API。 If you can give me direction by showing me how to be able to retrieve multiple records using the query below (fetchXml) and creating handlers so that they get fired on change when I enter data into those text boxes so that they can add up numbers in the boxes.如果您可以通过向我展示如何使用下面的查询(fetchXml)检索多条记录并创建处理程序来指导我,以便在我将数据输入这些文本框时它们会在更改时触发,以便它们可以将数字相加那些盒子。 And on save subtract the added numbers from the Totapplicationtotalrevenue and set a field on this entity form with this value.并在保存时从 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;         
    }
   

}

Few modifications as below should make it work.下面的一些修改应该可以使它工作。

  1. Entity name should be plural name.实体名称应为复数名称。 Verify this: new_applicationrevenues验证这一点: new_applicationrevenues

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

  2. Change the request method to GET instead of POST .将请求方法更改为GET而不是POST Call can be asynchronous, so I kept it as true调用可以是异步的,所以我保持它为true

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

  3. Move the code inside success block将代码移到成功块内

     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; } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM