State Update of Child Records via Custom Activity Workflow and Flow

Suppose we have an entity relationship in which Entity A is the parent of Entity A.1, and Entity A.1 in turn, is the parent of Entity A.1.1. Further, the status transition is as described in the picture. What we want to achieve is to update the status of all related records in Entity A.1 and Entity A.1.1, when a change in the status of Entity A happens.

Update of Child Records

The classic workflow engine of Dynamics 365 has in it many actions that readily can be tapped into; unfortunately, retrieving child records and updating  the same is not one of them. In this post, what I’ll show are the two solution approaches that can be taken: one, building a custom workflow activity and two, setting up an MS Flow.

But why even try custom workflow activity, an archaic approach perhaps, when a low-code solution is available? The real answer to this is that in a prior engagement, I just became fascinated and too curious about how something was built that I made it a personal mission to learn how it was done. Secondly, in this post, I just want to contrast the ease of building the solution between the two. Last but not the least, there might be some person looking for a solution out there in the web, so I thought this might help in the same way that countless posts online have for me.

Custom Worfklow Activity

I’m just going to assume here that the reader already knows how to use Visual Studio. In Custom Workflow Activities, it requires installing specific NuGet packages plus some other steps. I’m not going to go into the details of such. This page helps a great deal in that regard.

(By the way, the redaction in the images below is nothing but to hide the name of a real business entity that I used in my development environment. It might get me into some trouble if I didn’t redact it. It should have no real consequence in achieving the goal of the code.)

  1. Set the input parameter to be the parent record of the entity that is being monitored for status changes. In this case, it’s the request entity which should be Entity A on the top-most diagram.Parameters
  2. Write the query that will retrieve the related child records of Entity A. I used the FetchXML approach only because we can readily download the query via advanced find with some changes to make it dynamic. So really, it lessens the burden of writing the code. The string ‘request’ value = {zzz} is the filter where we set the lookup field to reference a specific record. Just below the block of FetchXML-structured query is a line that replaces the string ‘zzz’ with the guid of the input record parameter from Step 1.Query Entity A1
  3. Now that we have the related Entity A.1.1 records retrieved from Step 2, we will have to loop through each of the records to update each with the correct status  and status reasons. This block of code does that.Update Entity A1
  4. Inside the loop above is a code to retrieve its related records, which should be for Entity A.1.1 in the topmost diagram. Again using advanced find, we are able to let it do the job of generating the FetchXML query as below. As in step 2, the string ‘zzz’ is being replaced dynamically with the current Entity A.1 record the loop is on.Query Entity A11
  5. Lastly, create another block of code that will loop through each of the retrieved Entity A.1.1 records and perform the status and status reason with each.Update Entity A11

After building the code, this needs to be registered via the Plugin Registration Tool. I’m not going to go there. A lot of resources online is available for the instructions on how to do just that.

An experienced professional developer would find this whole exercise easy peasy. But the challenge I would say to novice developers or to “citizen” developers to get this done is to know the c# structure, syntax ,terminologies, and the like, plus being able to research the classes that are available specific to Dynamics 365.

MS Flow

Now that we have the custom workflow activity built, I thought it might be worthwhile to get to what we’re trying to achieve, only this time via MS Flow. Of course, as many know, MS Flow is a component of the Power Platform. The platform enables rapid development of solutions because of its low-code approach to things. It also empowers citizen developers to build solutions which otherwise only pro developers can performed. I believe the term here is “democratization” which is about enabling access to building applications/solutions to a wider audience.

  1. Set the trigger to be as below. The filtering attribute here is statuscode, which is the logical name of the Status Reason. This trigger means that the Flow will set-off when the status reason of a Request entity record changes. Request is Entity A in our diagram.MS Flow Trigger
  2. We obtain the record that triggered the start of the workflow as we’re to use it on some condition checks in succeeding steps.MS Flow Get the Request
  3. Retrieve all related records of the REQUEST record. This is Entity A.1 in the diagram at the very top of this post. The filter query here is that the REQUEST record of the licence record should be equal to the triggering record. The syntax is _[insert the logical name of the lookup field here]_value eq [guid of the triggering record]. 3 MS Flow Retrieve the Licence Record
  4. With the records retrieved in Step 3, we are now ready to perform actions to each record. Use the Apply to Each control and inside it, is a conditional check to set the appropriate status and status reason values depending on the status of Entity A.4 Ms Flow Loop Through Each Licence Record
  5. Inside the loop in Step 4, we are to retrieve the child records of each record in the loop. The filter query here is that the LICENCE lookup should be equal to current record the loop is at. Again, the filter query syntax is _[insert the logical name of the lookup field here]_value eq [guid of the record]. 5 MS Flow Retrieve the Licence Component Records
  6. A loop is setup inside the loop created in Step 4. This does the job of updating the status and status reason of the retrieved records in Step 5. This of course comes with conditional checks to set the correct values.6 Ms Flow Loop Through Each Licence Component Record

As can be observed, the over-all flow of the MS Flow solution has the same pattern as that of the custom workflow activity. This is certainly easier and faster to build than writing a code because of its point-and-click customization UI. As I am not yet adept at constructing OData queries, the only tricky part I encountered here is on how I will be entering the filter query for a lookup field. With some research, it turned out to be as simple as entering it this way: _lookup field logical name_value eq.

🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s