Unleashing Dynamics 365 Excellence: Your Source for Pro Tips and Snippets at D365Snippets.com

Our blog provides a curated collection of tips, tricks, and code snippets that streamline your workflow and enhance your proficiency.
Unleashing Dynamics 365 Excellence: Your Source for Pro Tips and Snippets at D365Snippets.com

Cascade Delete Action in Dynamics 365 FO

0

cascade delete action in dynamics 365 finance and operations

When a Delete Action comes into the picture? Normally delete actions come into the picture when a user is trying to delete a database record. Delete actions are used to maintain the data integrity and consistency of the database. At the time of development, a developer must take care of the delete actions to implement in the project.

In Dynamics 365 Finance and Operations (D365 FO), the delete action refers to the deletion of a record from a table in the database.

When a record is deleted, it is permanently removed from the system and cannot be recovered. Therefore, it is important to exercise caution when performing this action and ensure that it is necessary and appropriate to do so. In D365 FO, the Deleted action can be performed using various methods, including:

  • Using the "Delete" button on a record's form.
  • Using the "Delete" command on a record's right-click menu.
  • Using X++ code to delete records programmatically.

It is important to note that some records in D365 FO cannot be deleted due to system constraints or dependencies on other records. In such cases, the system will prevent the deletion and display an error message. A delete action is performed when related tables are existing in the project, and deleting records from any table must be restricted for keeping the database consistent.

Imagine we have two tables. One is a product category table with the name PrdCategoryTable and another is a product table with the name PrdTable. Where PrdCategoryTable stores different product categories, and PrdTable stores the details of different products. PrdTable refers CategoryId of PrdCategoryTable as the reference key. So PrdCategoryTable is the parent table and PrdTable is the child table. PrdTable alone has no existence without PrdCategoryTable.  So, deleting any records from both tables must be restricted.

In Microsoft Dynamics 365 Finance & Operations, there are four types of Delete Actions.

  • None.
  • Cascade.
  • Restricted.
  • Cascade + Restricted

Read more: What are the different types of delete actions in Dynamics 365 fo

All four Delete Action methods have major roles in Dynamics 365 development process. In this article, I am going to explain the cascade deletion action property with a simple example.

Cascade delete Action.

In Dynamics 365 Finance and Operations, the cascade delete action refers to a feature that enables the automatic deletion of related records when a parent record is deleted. It is a database-level functionality that helps maintain data integrity and ensures that the database remains consistent.

When you configure a relationship between tables/entities in Dynamics 365 Finance and Operations, you can define the cascade delete action for that relationship. 

Setting up the Delete Action property to Cascade extends the functionality of Tables' delete method. The Cascade Delete action deletes all the records in the related tables where the primary key is equivalent to the primary key of the parent table. which means that deleting records from the parent table also deleting the child records from the child table where the primary key of the parent table matches the foreign key of the child table without any warning.

Add Cascade Delete Action in Dynamics 365 FO

At the time of development, a developer must take care of the delete actions to implement in the project. Here I will show you how to set a Cascade delete action in Dynamics 365 Finance and Operations. For this,

1. Create a normal relation

Imagine we have two tables one is a product category table with the name PrdCategoryTable which has three columns CategoryId, CategoryCode, and CategoryName, and another product table with the name PrdTable, which has four columns PrdId, PartNumber, Description, and CategoryId where PrdCategoryTable stores different product categories, and PrdTable stores the product details. Here PrdCategoryTable is the parent table and PrdTable is the child table.

PrdTable refers CategoryId of PrdCategoryTable as the reference key. PrdTable alone has no existence without PrdCategoryTable.

Once you created these two tables, you can read the article on how to create a normal relation in Dynamics 365 fo,

After creating the normal relationship between the above tables, we need to set the cascade delete action to the table, for best practice, you always set the delete action in the parent table.

Add a cascade delete action

For setting up the cascade delete action, you need to follow the below steps,

1. Expand the parent table PrdCategoryTable. Find the delete action node, and right-click delete actions.

Cascade Delete Action in Dynamics 365 FO
2. Then click New Delete Action and right-click the new delete action, and then click Properties.
Cascade Delete Action in Dynamics 365 FO
3. Select a related table in the table reference property from the Table property list. In our example select PrdTable. Select action property as Cascade.
Cascade Delete Action in Dynamics 365 FO

This is the basic setup for adding cascade delete action in Dynamics 365 finance and operations. Once you have done all the above steps, you need to build the project successfully. After the successful build of the project, Right-click on the table PrdCategoryTable and select Open Table Browser to open the table in the browser.

Wait for a minute, a table browser will open in the browser, and you can enter some dummy data into the PrdCategoryTable as shown in the below figure.

Cascade Delete Action in Dynamics 365 FO

Similarly, Right click on the table PrdTable and select Open Table Browser to open the table in the browser.

Wait for a minute, a table browser will open in the browser, and you can add some dummy data into the PrdTable as shown in the below figure. here I added some product records by referring to the CategoryId 1000 from the table PrdCategoryTable.

Cascade Delete Action in Dynamics 365 FO

Now we are going to test the delete action applied to the tables. For this, Delete a record from the parent table

Cascade Delete Action in Dynamics 365 FO

Here I deleted one record from PrdCategoryTable with CategoryId as "1000", because PrdTable refers to the category id 1000. If the deletion action is set or working properly, it will automatically delete the related data from the child table PrdTable.

Open the child table PrdTable and refresh the browser to refresh the data in the PrdTable. Now you can see all the related records from the child table PrdTable has been deleted automatically. Yes, the related records are automatically deleted from the child table, and the cascade deletes action is working properly.

Cascade Delete Action in Dynamics 365 FO

I think this article will be very useful for beginners and if the article is useful, please try to share it with your friends who are planning a career in D365 FO. In the next article, I will show you how to add restricted delete action in Dynamics 365 fo.

Post a Comment

0Comments
Post a Comment (0)