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
1. Expand the parent table PrdCategoryTable. Find the delete action node, and right-click delete actions.
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.
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.
Now we are going to test the delete action applied to the tables. For this, Delete a record from the parent table
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.
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.