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.

 

 

 

Advertisement

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

  • Microsoft Dynamics CRM Import Error 0x80040237: Cannot insert duplicate key
  • Lock all fields on a form using Javascript- Microsoft Dynamics CRM
  • No import packages were found. Exiting the program...
  • Count the number of related child records using a Rollup field
  • how to retrieve lookup value and set it on another lookup field in Dynamics CRM 2016 Javascript
  • 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. Tamilselvan on Dynamics 365 For Marketing – Retrieve Contacts From A SegmentDecember 23, 2022

    Ok, Got it... To access this schema information, Marketing app has to be installed. It was explained in the MS…

  2. Tamilselvan on Dynamics 365 For Marketing – Retrieve Contacts From A SegmentDecember 23, 2022

    Entity msdyncrm_segmentid is not accessible anymore. Couldn't find any solution to access the dynamic member list.

  3. Ssi on Get User’s Teams Using WebApi JavaScript In Dynamics 365December 13, 2022

    Hi ravish kumar i am also getting all the teams i want only logged in user teams please help me

  4. sai on Get User’s Teams Using WebApi JavaScript In Dynamics 365December 13, 2022

    Hi ravish by using your code i am getting all teams but not current logged in user team please help…

  5. Faraz on Color Subgrid in Microsoft Dynamics CRM 365August 30, 2022

    how do I change the background color of my columns in editable grid views in model driven app?

Create a website or blog at WordPress.com
  • Follow Following
    • Passion Dynamics
    • Join 44 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: