Skip to primary content
Skip to secondary content

Passion Dynamics

Microsoft Power Platform & Azure blog by Rawish Kumar

Passion Dynamics

Main menu

  • Home
  • About Me
  • Contact

Tag Archives: how to count child records and show on parent record created in last 1 month

Count Related Child Record Created In Last X Days (Rollup Field Limitation)

Posted on April 27, 2018 by Rawish Kumar Prajapati
Reply

I have recently posted a blog where i have showed to you guys how a rollup field can be useful if you need to count related child records : https://rawishblog.wordpress.com/2018/04/24/count-the-number-of-related-child-records-using-a-rollup-field/

however, everything has its limitation, in this case rollup field will not give you an option to to filter record from last x days or from a specific time period.

lets for e.g you needed to count records which are created in specific time period? may last 50 days or 90 days. Thats where you will have to move on and search for other methods – i have come up with a simple supported combination of WebApi + FetchXML.

in this example i am going count related cases for an account.

  1. Create a whole number field and put this on the form
  2. Use below script :
    function CountRecords() {
    
    var recordId = Xrm.Page.data.entity.getId();
    
    var fetchXml = "<fetch mapping='logical' distinct='false' aggregate='true'>"
     + "<entity name='incident'>"
     + "<attribute name='title' alias='recordcount' aggregate='count' />"
     + "<filter type='and'>"
     + "<condition attribute='createdon' operator='last-x-days' value='100' />"
     + "</filter>"
     + "<link-entity name='account' from='accountid' to='customerid' link-type='inner' alias='ab'>"
     + "<filter type='and'>"
     + "<condition attribute='accountid' operator='eq' value='" + recordId + "' />"
     + "</filter>"
     + "</link-entity>"
     + "</entity>"
     + "</fetch>";
    
    var fetch = encodeURI(fetchXml);
     var entityname = "incidents";
     var serverURL = Xrm.Page.context.getClientUrl();
     var Query = entityname + "?fetchXml=" + fetch;
     var req = new XMLHttpRequest();
     req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/" + Query, false); //
     req.setRequestHeader("Accept", "application/json");
     req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
     req.setRequestHeader("OData-MaxVersion", "4.0");
     req.setRequestHeader("OData-Version", "4.0");
     req.send();
     if (req.readyState == 4)
     {
     req.onreadystatechange = null;
     if (200 == req.status) {
     var results = JSON.parse(req.response);
     var records = results.value[0].recordcount;
     Xrm.Page.getAttribute("new_totalcasescount").setValue(records);
     }
     else {
     console.log(req.statusText);
     }
     }
     }

     

  3. Call this function on on load of your account – it will fetch the related cases and fill the newly created field.
    you will the field has updated on Onload of the form :
    1

 

few things :

  • You can get the required fetch XML from advanced find if you need to apply on other entities or with other condition.
  • You can use this field to put on views however it might give you the updated information as the script runs on -onload of the form and update the value. Hence , if you need that functionality – please go for a plugin.

 

i hope this helps.

 

 

 

Share this:

  • Twitter
  • Facebook

Like this:

Like Loading...
Posted in CRM WebApi/Javascript, Uncategorized | Tagged Author:Rawish, count child records using javascript, Count Related Child Record Created In Last X Days, Count Related Child Record Created In Last X Days (Rollup Field Limitation), count related opportunities created in last x days, CRM, crm javascript, how to count child records and show on parent record created in last 1 month, how to count related cases of an account created in last x days, how to count related records created in last 1 month, javascript to count record, Microsoft Dynamics CRM, rollup field, rollup field limitation, web API | Leave a reply
Follow Passion Dynamics on WordPress.com

I'm not an expert; I'm learning!

Top Posts & Pages

  • How to insert dynamic values from custom entities in an email template - Microsoft Dynamics CRM
  • Map A Multi Target Lookup Field In Azure Data Factory - Dynamics 365 Data Import
  • Dynamics 365 For Marketing - Retrieve Contacts From A Segment
  • Plugin on Retrieve Multiple in CRM.
  • Enable Custom Entities For Marketing Segment Dynamic Query
  • Alternate Keys
  • Audit History
  • Business Rules
  • Calculated & Rollup Fields
  • Certifications
  • CRM Code Helpers
  • CRM WebApi/Javascript
  • Data Migration
  • Deployment
  • Dynamics 365 For Marketing
  • Dynamics CRM 365 Online
  • Dynamics Portal
  • Microsoft Dynamics CRM Emails
  • Microsoft Dynamics CRM Reporting
  • Microsoft Dynamics CRM SDK
  • Microsoft Dynamics CRM Services
  • Microsoft Dynamics CRM Solutions
  • Reblogged
  • Uncategorized
  • Unified Service Desk for Dynamics CRM
  • Voice Of Customer
  1. Maheshwar on Enable Custom Entities For Marketing Segment Dynamic QueryApril 4, 2022

    Hi Rawish Kumar , thank you for explaining how to add related entities in segments. I am unable to add…

  2. Amritha on How to insert dynamic values from custom entities in an email template – Microsoft Dynamics CRMMarch 18, 2022

    Can we add attributes from custom entities into a marketing email template?

  3. George Rizza on How to insert dynamic values from custom entities in an email template – Microsoft Dynamics CRMNovember 4, 2021

    This is the best I have seen but I am still not clear on the format for the lookup value.…

  4. Michelle on Enable Custom Entities For Marketing Segment Dynamic QuerySeptember 10, 2021

    Do you need to have a Customer Insights license in order to turn this feature on?

  5. How to use JavaScript to fill parent lookup on form in Dynamics 365 CRM on-premise? - Tutorial Guruji on Get lookup value from other entity and set it on the form using Web API in Microsoft Dynamics CRM 2016April 9, 2021

    […] What you need is, a Javascript function to trigger on change of first lookup to query necessary fields from…

Create a website or blog at WordPress.com
  • Follow Following
    • Passion Dynamics
    • Join 42 other followers
    • Already have a WordPress.com account? Log in now.
    • Passion Dynamics
    • Customize
    • Follow Following
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar
 

Loading Comments...
 

    %d bloggers like this: