pnduke
pnduke

Reputation: 183

Getting related entity count through FetchXML

I have the following fetchXML code for returning the total number of related entities. Main entity name is new_transaction and related entity name is new_transactionproduct.

The code below is placed in a web resource javascript but when this function is called it never gets to success or error portions, it simply hangs.

 function countLineItems()
 {
 var ID = Xrm.Page.data.entity.getId();
 var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' aggregate='true'>";
 fetchXml += "<entity name='new_transactionproduct'>";
 fetchXml += "<attribute name='new_name' alias='recordcount' aggregate='countcolumn' />";
 fetchXml += "<filter type='and'>";
 fetchXml += "<condition attribute='new_transactionid' operator='eq' value='" + ID + "' />";
 fetchXml += "</filter>";
 fetchXml += "</entity>";
 fetchXml += "</fetch>";
 alert(fetchXml);
 XrmSvcToolkit.fetch({
        fetchXml: fetchXml,
        async: false,
        successCallback: function (result) {
            var countValue =  result.entities[0].recordcount;
 alert(countValue);
    //Xrm.Page.getAttribute(new_totalqty).setValue(countValue);
        },
        errorCallback: function (error) {
            throw error;
        }
 });
 }

Upvotes: 2

Views: 1393

Answers (2)

Andrew
Andrew

Reputation: 10033

Just a quick side note you could always setup your fetchXML like below to minimise the amound of times you append to the string.

string fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' aggregate='true'>
                        <entity name='new_transactionproduct'>
                             <attribute name='new_name' alias='recordcount' aggregate='countcolumn' />
                               <filter type='and'>
                                 <condition attribute='new_transactionid' operator='eq' value='" + ID + @"' />
                               </filter>
                        </entity>
                    </fetch>";

Upvotes: 2

pnduke
pnduke

Reputation: 183

XrmSvcToolkit needed to be added as a web resource and referenced on the page.

Upvotes: 1

Related Questions