Skip to main content

Notifications

Announcements

No record found.

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 36

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

  • KoJaPD Profile Picture
    KoJaPD 36 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?

  • KoJaPD Profile Picture
    KoJaPD 36 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!

  • Verified answer
    Wahaj Rashid Profile Picture
    Wahaj Rashid 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
    KoJaPD 36 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!

  • KoJaPD Profile Picture
    KoJaPD 36 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...

  • David Jennaway Profile Picture
    David Jennaway 14,063 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
    KoJaPD 36 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...

  • Verified answer
    Wahaj Rashid Profile Picture
    Wahaj Rashid 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

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,321 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans