- D365FO
- X++
- DateTime
- Dynamics
In the previous article, we have already discussed Custom Table Methods Through an Extension in Microsoft Dynamics 365 Finance & Operations. In this article, we are discussing one of the interesting topics Delete Actions in D365 FO. If you are a beginner or learner use Microsoft's free VM, there you can access both the Technical and Functional environments for developing your knowledge in D365 Finance and Operations.
When a Delete Action comes into the picture? Normally delete actions come in to 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 has to take care of the delete actions to implement in the project. Then"How is the database consistency maintained?".
Actually, a delete action is performed when related tables are existing in the project, and deleting records from any table has to be restricted for keeping the database consistent.
Imagine we have two tables PP_EmpTable and PP_EmpFamily, where PP_EmpTable keeps all the basic details of the employee, but PP_EmpFamily keeps the family details of each employee existing in the EmpTable. Here PP_EmpTable is the parent table and PP_EmpFamily is the child table.
EmpFamily Table refers EmplId as the reference key. PP_EmpFamily alone has no existence without PP_EmpTable. So deleting any records from both tables has to 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 have major roles in Dynamics 365 development process.
Consider two tables PP_EmpTable and PP_EmpFamily tables, here EmpId is the foreign key for PP_EmpFamily which refers to PP_EmpTable. Now we are going to set the deletion action property to the table PP_EmpFamily.
1. None
If you are choosing None, As the name shows None means Delete Action is disabled. In this case, while deleting records from the table nothing will occur on the related table.
2. Cascade
In our example, if we are deleting any record from PP_EmpTable it also deletes the matching records from the table PP_EmpFamily where the primary key EmpId matches the foreign key of the PP_EmpFamily.
Note: - Cascade Delete Action takes place both records are deleted through code or directly by the user through the user interface.
Read More: Learn How to add cascade delete action in d365 FO
3. Restricted
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.
4. Cascade + Restricted
Setting up the Delete Action property to Cascade + Restricted also extends the functionality of the tables' delete method. In this case, deleting a record from the parent table will show a warning and if we delete the record from the parent table also delete the related child records from the child table.
As a result, super(), in validateDelete, ascertains whether records exist on related tables. Whether deleting records from forms or X++, if validateDelete returns false, the primary record isn't deleted and the cascading delete isn't performed. You should first delete the records in the related table before deleting the primary record.
If the primary record is being deleted as part of a cascading delete, the primary record and the records in the related table will be deleted.
Example
The Cascade+Restricted delete action is used in the standard application for LedgerJournalTrans on LedgerJournalTable.
This type of delete action is useful when you prefer a total clean-up—when you delete a customer, you also delete all the transactions associated with that customer.
In our example deleting records from PP_EmpTable also deleted the records from the related records from PP_EmpFamily. So here this property brings the functionality of both Cascade and Restricted.
Note: - However if the delete operation is done through X++ code it will not show any error and will delete the records from both tables.
This article explains the basic concept of delete action and what are the different types of delete action in D365 FO. In the next article, I will explain different types of delete actions in detail. if this article is useful, please try to share this with your friends.
Happy coding with D365 Snippets
Conclusion
So as a D365 Developer you must have made and idea about Delete Actions in D365 Finance & Operations. At the initial stage of project development, we have to consider which delete actions to be set for the tables. Delete action is one of the important & interesting features of D365 Finance & Operations. These articles are mainly for beginners and learners to get the basic important concepts of Dynamics 365 Finance and Operations. Please put your comments to improve the quality of the articles.