web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

How to create a business rule based on count of related records?

(0) ShareShare
ReportReport
Posted on by 38

Dear,

I have 2 custom entities in the CDS: 

  • WarehouseLocation
    • ID
    • CanHoldMoreThanOneItem (YES/NO, Default NO)
    • Items (List of items)
  • Item 
    • ID
    • CurrentLocation

with a one to many relation so that one item can have zero or one location and one location can have zero, one or many items.

How can I make a business rule that makes it impossible to add an item to a location when WarehouseLocation.CanHoldMoreThanOneItem  == NO and  COUNT(WarehouseLocation.Items) >= 1 ?

Thanks,

Koen

I have the same question (0)
  • Verified answer
    Wahaj Rashid Profile Picture
    11,321 on at
    RE: How to create a business rule based on count of related records?

    Hi,

    You have to write a plugin to implement this logic.

    Here is the logic:

    • Register a plugin on pre create step of Item Entity.
    • Retrieve all the Items related to the current WarehouseLocation and check count.
    • If CanHoldMoreThanOneItem = No and count of related items>1 throw InvalidPluginException with a custom message.

    Let me know if you need further details.

    Best,

    Wahaj

    (if it helps, mark it as verified)

    Best,

    Wahaj

  • KoJaPD Profile Picture
    38 on at
    RE: How to create a business rule based on count of related records?

    Dear, thanks for your response, it seems a reasonable solution. I will need to check the documentation, because I have no experience with developing plugins for Dynamics, and I thought plugins were only possible on on-premise installations...

  • David Jennaway Profile Picture
    14,065 on at
    RE: How to create a business rule based on count of related records?

    You should be able to do this with a real-time workflow, rather than needing to write a plugin. The main steps are:

    • Create a rollup field on the Warehouse entity that counts the current items
    • By default, rollup fields are only recalculated every hour, which may cause an issue. You can create a separate workflow using the Dynamics-365-Workflow-Tools to automatically update the rollup field when items are added to a warehouse
    • Create a real-time workflow on the Item entity that has a check condition to apply the logic (i.e. WarehouseLocation.CanHoldMoreThanOneItem  == NO and  COUNT(WarehouseLocation.Items) >= 1) and if so stop the workflow with a status = Cancelled
  • KoJaPD Profile Picture
    38 on at
    RE: How to create a business rule based on count of related records?

    That would indeed be easier I guess. I will have to investigate this solution as well; I have experience with creating triggered flows, but not with realtime ones...

  • KoJaPD Profile Picture
    38 on at
    RE: How to create a business rule based on count of related records?

    Dear,

    Could you point me where to find information on creating these real-time workflows? This is a PowerApps environment (without a Dynamics environment); I don't know where to look for it...

    Regarding rollup fields, I thought those were updated only once every 12 hours?!?

    Thanks for your help!

  • Verified answer
    Wahaj Rashid Profile Picture
    11,321 on at
    RE: How to create a business rule based on count of related records?

    Hi,

    Rollup fields are not updated in real-time. This means, by the time count of items is updated in the roll-up field, the user can add as many as possible line items.

    This is the reason I suggested a plugin. If you are considering real-time workflow, you have to write a custom action that returns the count of related items (similar to the plugin).

    Best,

    Wahaj

  • KoJaPD Profile Picture
    38 on at
    RE: How to create a business rule based on count of related records?

    The suggestion for plugins opened a whole new world for me; it seems to be quite powerful to extend the possibilities in the Power Platform. Thanks for the help!

  • KoJaPD Profile Picture
    38 on at
    RE: How to create a business rule based on count of related records?

    Dear, How can I show the custom message from the InvalidPluginExecutionException in the Canvas App?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Sahan Hasitha Profile Picture

Sahan Hasitha 271

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 91 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 76 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans