Book Image

Microsoft Dynamics 365 Extensions Cookbook

Book Image

Microsoft Dynamics 365 Extensions Cookbook

Overview of this book

Microsoft Dynamics 365 is a powerful tool. It has many unique features that empower organisations to bridge common business challenges and technology pitfalls that would usually hinder the adoption of a CRM solution. This book sets out to enable you to harness the power of Dynamics 365 and cater to your unique circumstances. We start this book with a no-code configuration chapter and explain the schema, fields, and forms modeling techniques. We then move on to server-side and client-side custom code extensions. Next, you will see how best to integrate Dynamics 365 in a DevOps pipeline to package and deploy your extensions to the various SDLC environments. This book also covers modern libraries and integration patterns that can be used with Dynamics 365 (Angular, 3 tiers, and many others). Finally, we end by highlighting some of the powerful extensions available. Throughout we explain a range of design patterns and techniques that can be used to enhance your code quality; the aim is that you will learn to write enterprise-scale quality code.
Table of Contents (19 chapters)
Title Page
About the Author
About the Reviewer
Customer Feedback

Setting up the rollup fields

Rollup fields are one of the most exciting new features that were introduced in recent years that significantly reduced the amount of custom code required. Rollup fields can aggregate values from other attributes. You can calculate sums, averages, minimums, maximums, and counts.

In this recipe, we will create a rollup field that counts how many active activities are associated with a contact.

Getting ready

Similar to the previous recipes, a System Customizer or higher security role is required to perform the configuration as well as a solution to contain the changes.

How to do it

  1. Navigate to Settings | Solutions | Packt.
  2. Create a new field by clicking on Entity | Contact | Fields | New and enter the following details:
    • Display Name: Number of Activities
    • Data Type: Whole Number
    • Field Type: Rollup
  3. Click on Edit next to the Field Type dropdown.
  4. In the Rollup Field dialog, under Related Entity, select Activities (Regarding) and click the tick box.
  5. In the Filters section, select the following:
    • Activity Status under Field
    • Equals under Operator
    • Value under Type
    • Check Open and Scheduled under Value
  6. Click on the tick box.
  7. Under Aggregation, select the following:
    • Aggregate Function under Count
    • Activity under Aggregated Related Entity field
  8. Click on the tick box. Your ROLLUP FIELD dialog will look as follows:
  1. Click on Save and Close on the ROLLUP FIELD dialog and the attribute dialogue.
  2. Click on the Publish All Customizations button for your solution.

How it works...

In step 2 to step 7, we created a new rollup field of type whole number that calculates the total count of active activities associated with a contact.


A couple of things to note about rollup fields: the fields are read-only and the calculation does not take into account a user's security roles.




When a rollup field is created, behind the scenes, a system asynchronous job is scheduled to execute 12 hours after the field is created. Navigate to Settings | System Jobs; you will see a job of type Mass Calculate Rollup field with the name of your new field in System Job Name. You can force the job to run sooner by clicking on Action | Postpone and entering an earlier date/time. It is not recommended to do so when you have a large number of records in a production environment. The first time the job runs, it will have to update all fields, which might lead to a performance impact.

Subsequently, the System Jobs option will have a Calculated Rollup field per entity that has rollup fields configured. By default, subsequent rollup field calculations will take place every hour, (which is the fastest schedule). If an hourly update is not necessary, you can update the value by double-clicking on the System Job option, and selecting Actions | Modify Recurrence. This will present a dialog box that helps you redefine the recurrence of the execution:


Given that the rollup fields are executed as asynchronous jobs, if the calculation is not occurring, consider checking the status of the asynchronous service. For on-premise installation, log on to your backend server and check its status. For online implementations, log a support call with Microsoft to check the status of the service.

Programmatic Rollup Field execution

On top of the automated schedules, rollup fields can also be executed programmatically by executing CalcualteRollupFieldRequest. Furthermore, if you search online for "rollup field workflow", you will find custom workflow activities built by the community to force recalculations in your configurable workflows.

There's more...

Rollup fields are a powerful feature in Dynamics 365. On top of the counting capability described in this recipe, rollup fields offer other types of aggregations. Furthermore, in some cases (activities), the aggregation can span across multiple relationship levels.

Different types of aggregation

Counting the number of records is one of the many capabilities of a rollup field. You can also get the maximum, minimum, average, and sum of a field, assuming the field type allows it. As you select a different aggregate function, the entity fields will be filtered accordingly.

Indirectly related activities

Given that our rollup field is aggregating activities, the field can also include indirectly related activities by defining Activity Parties (activities) under indirectly related activities. This will expand the aggregation, not only when the contact is in the regarding field, but also in the parties field.

For more details about rollup fields, visit

See also

  • Setting up calculated fields