简体   繁体   中英

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.

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. I am tring to do a RertriveAllREcords from the FetchXml below. And then Subtract SUM(TotalRevenue) from SUM(new_paymenttypeid + new_revenueid) and store it in a field of another form. The process will run from this other form as javascript on change/on save/on load. But it just does not work. We are using Dynamics 365 version 8.2. I am stuck in the method CalculateSurplusdeficit due to it. I intend to use the 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. And on save subtract the added numbers from the Totapplicationtotalrevenue and set a field on this entity form with this value.

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

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

  2. Change the request method to GET instead of POST . Call can be asynchronous, so I kept it as 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; } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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