In this article i will show How to add a cascade delete in Dynamics 365 finance and operations. In this article, I will show you how to add a restricted 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. Here PrdCategoryTable is the parent table and PrdTable is the child table. Since 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
All four Delete Action methods In Microsoft Dynamics 365 Finance & Operations have major roles in Dynamics 365 development. In this article, I am going to explain the Restricted deletion action property with a simple example.
What is Restricted Delete Action?
Setting up the Delete Action property to Restricted also extends the functionality of the tables' delete method. In this case, the system checks that while deleting a record from the parent table first the system checks whether any related records exist in the related child tables if exist the system will generate a warning message. This warning message will generate only if the delete action is performed through the user interface. If you are trying to delete through X++ code this will delete only the records from the child table.
Note: - A programmer can use .validateDelete() and this will return true or false values, based on the result, a developer can do the action.
As a result, super(), in validateDelete, checks whether records exist on related tables. If records do exist, validateDelete returns false. The forms system ensures that the deletion is not performed. In your own X++ code, check the return value of validateDelete. Don't delete the primary or related records if the method returns false.
Example: - On the VendTable table, a restricted delete action has been defined for the VendTrans table. When a vendor is deleted in the VendTable table, the validateDelete method ascertains whether transactions exist for the vendor in the VendTrans table. If so, validateDelete returns false.
Add Restricted Delete Action in Dynamics 365 FO
Here I will show you the steps of how to set a restricted delete action in Dynamics 365 Finance and Operations. For this,
1. Create a normal relation
Restore the table structure, we have had tables, one is a product category table PrdCategoryTable which has three columns CategoryId, CategoryCode, and CategoryName, and another product table 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. You remember that PrdTable refers PrdCategoryTable, PrdTable alone has no existence without PrdCategoryTable.
Once you created these two tables,
After creating the normal relationship between the two tables, you need to set the restricted delete action to the table, you always note one important point is that , always set the delete action in the parent table.
Add a restricted delete action.
For setting up the restricted delete action, you need to follow the below steps,
- Expand the parent table PrdCategoryTable. Find the delete action node, and right-click delete actions.
- Then click New Delete Action and right-click the new delete action, and then click Properties.
- Select Table Reference property from the Table property list. In our example select PrdTable.
- Choose Restricted in the Action property as shown in the below screen shot.
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 restricted delete action applied to the tables. For this, Delete a record from the parent table.
When I tried to delete a record from PrdCategoryTable with CategoryId as "1000", which showing a warning message (This warning message will generate only if the delete action is performed through the user interface). This is because PrdTable refer cateoryId "1000" from the table PrdCategoryTable. if we set the delete action property to restricted, if any related records exist, system will not allow to delete record from parent table untill the child records deleted. In this case you need to delete the related recods from child table.
Delete record from PrdTable
Delete record from PrdCategoryTable
As a result, super(), in validateDelete, checks whether records exist on related tables. If records do exist, validateDelete returns false. The forms system ensures that the deletion is not performed. In your own X++ code, check the return value of validateDelete. Don't delete the primary or related records if the method returns false.
I think this article will be very useful for beginners to get the exact concept ot restricted delete action. if the article is useful, please try to share it with your friends who are planning a career in D365 FO. Happy coding with D365Snippets.
Comments
Post a Comment