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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to loop through entities under a parent with a workflow

(0) ShareShare
ReportReport
Posted on by

I have a parent entity (Group) with child entities (Reg). I need to design a workflow that checks each child entity individually, after it has been created, calculating the costs of the individual Reg. Afterwards I need it to look through the group of Reg's again to see which one has not been calculated, when it is satisfied that all Reg's have been processed it will stop.

How do I do this?

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Hi,

    This is an unusual requirement and I would probably design it a little differently, but you could implement it as is in a workflow (with an Add-On). If you install Workflow Elements (https://kaskela.wordpress.com ) you could build a workflow to do it.

    Create a workflow that runs on the Reg record to calculate the cost. Then, you'll need to use the activities "Query - Get a List" to query for Regs that need to be updated, and "Workflow - Run on Query Results" to kick off the workflow on the Reg records.

    1) Call "Workflow - Run on Query Results". You'll pass in the workflow you want to run, and the query for the records that need to be updated. Make sure your query limits to only related records and only ones that still need to be updated (documentation is on the website).

    2) Sleep for a few minutes. This will let the workflows complete.

    3) Call "Query - Get a List" with the same query as #1 above. This will tell you how many need to be updated (use the output parameter Number of items in the List).

    4) Check if "Number of items in the List" > 0. If so, wait a few minutes and kick off this same workflow again.

    5) If "Number of items in the List" = 0, Complete.

    I don't love this approach because you could get into an infinite loop if one of the items won't calculate, but this would be how to implement that requirement.

    Hope this helps! I'd appreciate if you'd mark this as Answering your question.

    Thanks,

     Aiden

  • Community Member Profile Picture
    on at

    Thank you! I am having to tweak a system that was pre-existing, and though I know this is irregular it saves me from having to redesign a very complicated registration processes set completely since it will be phased out soon.

  • Community Member Profile Picture
    on at

    Using your tools, how might I tell the workflow to go through each Reg. entity one at a time? Our problem is that when we give the system more than 1 reg to calculate fees for it makes mistakes but when it goes one at a time it works properly.

  • Verified answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Hi,

    Okay, this will be a little tougher. You can still do it in a workflow but you should really consider moving to custom code to tackle this. You could still have your first trigger be a workflow, but you'd have to call a custom activity that retrieves the related records and performs the calculation before moving on to the next one. It's more work than a configuration-only workflow but it will be much more reliable in the medium-long run. If it's just a band-aid for now, you can still accomplish this in a workflow:

    Essentially, you'd need your workflow to run once for each record coming back in the query:

    1. Query - Do I need to run the workflow on any records? If yes,
      1. Run Workflow on Query (but limit query to the first record)
      2. Sleep for a few minutes so the workflow can complete
      3. Query - Do I need to run the workflow on any records?
      4. Respawn this workflow or complete

    To limit the workflow to run on just one record, instead of using a view in the activity you'll want to convert your view to FetchXml (in the advanced find screen there's a button for this). When you get your Fetch result it'll be an exact copy of the view, and you'll need to edit it to set the "count" property to 1 so the query just returns one record (http://burnignorance.com/ms-crm-app-development-tips/top-10-or-x-records-from-crm-2011-using-fetch-xml/)

    So now you have a workflow that'll call itself once for each related record, but that adds an extra problem. CRM limits how many times a workflow can call itself in a time period, in order to prevent infinite loops. If you have more than 7 related records, this will be a problem. A tedious way to get around this in your case is to duplicate the workflow steps a couple of times so the workflow handles, say, 5 records:

    1. Run the child workflow of the first query result and sleep
    2. Query to see how many more records there are. If 0, complete WF
    3. Run the child workflow of the first query result and sleep
    4. Query to see how many more records there are. If 0, complete WF
    5. {...}
    6. respawn this workflow if there are still records

    This will let you handle as many records as you feel like, sequentially, inside a single workflow.

    Hope this helps! I'd appreciate if you'd mark this as Answering your question.

    Thanks,

      Aiden

    EDIT: When you paste the FetchXml into the workflow activity, make sure to remove any line breaks before you copy and paste it otherwise the text gets cut off and the query will be invalid.

  • Community Member Profile Picture
    on at

    Thanks again, it runs perfectly now and your Processes plug-in is incredibly useful

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans