{No Code Approach} Generic framework to delete all child records when a parent record is deleted in Dynamics 365 using Microsoft Flows

Here comes another requirement and here comes Microsoft flows again to bail me out. So let’s see what the requirement was and let’s see how can we implement Microsoft flows to achieve these easily which otherwise would have required extensive coding to achieve.

So it goes like this. Basically, there are lot of custom entities in the environment and there are many parent-child relationships in between those entities and they are referential in nature. Ideally parental would have suited for some, however due to the number of parental relationship limitation in Dynamics, they would set it up as referential.

Now whenever a parent record is deleted, the associated child records would be deleted as well. Since this is referential relationship, whenever we delete a parent record, the child records are left orphaned in the system.

Microsoft flows offers you an easy way to delete records in bulk. You could easily define a flow for each child record type and use the delete record step of Microsoft flows to achieve the same.

But wait? In the above approach, we need to create a flow for each child entity type and if the number of child record types are pretty large then suggesting to build flows for each of them is not something you would want. Also if a new entity or a new relationship comes into the picture, you would need to again write a flow for the same.

So now what? After a lot of brainstorming I finally came up with a solution which is pretty generic in nature and pretty scalable as well. So let’s see the design here.

  • Create a custom entity. I named it – ‘Child Record Delete Trigger’. The following are the fields I created for this entity with their purpose.
    • EntitySet Name – This would hold the entity set schema name for the child record type to be deleted
    • Child Record Id Name – This field would hold the schema name for the Unique Identifier of the child record
    • Parent Field name – The field would hold the schema name of the parent record field associated with the child record. Basically this would be the lookup field name of the parent record on the child record.
  • For testing this, I create a new entity named ‘Flow Test’ which has a N:1 relationship with Contact entity. Screenshot for reference.

image

  • The lookup field of the contact has the schema name – new_contactid as seen from the above screenshot.
  • The basic idea is to create a ‘Child Record Delete Trigger’ record whenever a parent record is deleted.
  • I created two records of Flow Test with Contact = ‘Catalina Weeks’

image

  • I go ahead and delete the contact ‘Catalina Weeks’. On delete of the contact record, I create a record of ‘Child Record Delete Trigger’ entity with the following values
    • EntitySet Namenew_flowtests (since this is entity set name)
    • Child Record ID Namenew_flowtestid (this is schema name of the primary key of the child record to be deleted)
    • Parent Field Name – new_contactid (lookup of the contact field on the child record)

I leave it up to you to determine on how to create this record, whether through a plugin or through a workflow or some other methods. As long as you create the record properly with all the fields populated, it is going to work.

So all set and done. The next step is to create the generic flow to delete the child records.

The first step is to select the Dynamics 365 Step – When a record is created.

image

Below are the configurations for this step.

image

Now when a parent record is deleted, all the child records parent reference would be null. So the next step is to determine all the child record for which the parent reference is empty.

For this, I select the Dynamics 365 – List Records step.

image

Below is the configuration for this step.

image

As you can see, I have selected dynamic properties for all the steps in List Records. I am getting all the child records where Parent reference is null. As you can see, no entity names and field names are hardcoded. It takes from the ‘Child Record Delete Trigger’ record we created when the Parent record has been deleted.

So far so good. So now what? Now we need to iterate through each of these records and  delete them

So we add the ‘Apply Each’ Step.

image

image

Now comes the hard part. To delete the record you would need to use the Dynamics 365 – Delete a record step which requires a unique identifier. However the problem here is that the flow designer at the design time does not know which entity record you are trying to delete. So it does not show up the UniqueIdentifier field in the designer.

So we have to basically make the runtime understand the Unique Identifier value (GUID of the record) so that the delete step is able to delete the record. For this we would use the Compose action.

image

image

So what we have done here? Remember when we created the Child Record Delete Trigger record, we populate the field ‘Child Record Id Name’ with the schema name of the primary key of the child record.

So we use the compose function to parse that Unique ID field schema name.

Great! Now the second task it get the schema name and pass it to the framework to get the GUID. For this we use another “Compose’ action but this time, we plugin some custom expression of our own.

image

So what are we doing above? We are taking each item of the iteration which is identified by item() and then getting the value of the Guid of the record by passing the name of the primary field which we composed from the earlier step. This is similar to accessing a value using an indexer.

The final step is the Delete Record Step of Dynamics 365 which is very simple as shown in the below screenshot. All we are doing is getting the guid value of the child record from the previous Compose action step and passing it to the delete step.

image

And we are done!

So I deleted the contact. As expected, the child records were left without the parent contact reference.

image

 

A record of Child Record Delete Trigger entity got created with the following information.

image

So the Flow should fire now.

I check that and confirm that my flow ran successfully. And when I visit my child records list, I could see that all my child records have been deleted.

Simple and powerful isn’t it.? The same generic flow can be used for any entity/ relationship and in the whole flow you have deleted the child entity records without even know which entity it is.

 

Hope you liked this.

 

-Debajit

(Visit our products page at www.xrmforyou.com to know about our offerings)

Advertisements

About Debajit
I am a Dynamics CRM Most Valuable Professional (MVP) with 10 years of experience in Microsoft .NET Technologies and 7 years of dedicated experience in Microsoft Dynamics CRM. I have worked with companies like Microsoft, SanDisk, PwC, TMF Group and have extensive experience of implementing complex CRM solutions from both offshore and client side. Currently the face of XrmForYou.com with significant experience in delivering corporate training on Dynamics CRM and have already delivered multiple projects to client through XrmForYou.com Author of multiple tools on codeplex including the 'Role Based Views' and 'CRM-Sharepoint Metadata manager & Attachment Extractor' which are available for commercial use under XrmForYou.com For consulting/ training, drop me a note at info@xrmforyou.com or visit our website www.xrmforyou.com

One Response to {No Code Approach} Generic framework to delete all child records when a parent record is deleted in Dynamics 365 using Microsoft Flows

  1. Nishant Rana says:

    Reblogged this on Nishant Rana's Weblog and commented:
    Interesting implementation using Microsoft Flow

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: