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

Delete Actions in D365 FO

0
delete action different types of delete action in dynamics 365 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?".

In Dynamics 365 Finance and Operations (D365 FO), the "Deleted" 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.

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.

Delete Actions in Dynamics 365 Finance and Operations D365 Snippets
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.

In visual studio right click on the foreign key EmpId and go to the properties set the Related Table Property to PP_EmpTable as shown in the figure and select the On Delete Property to Cascade as shown below.

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

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.



Delete Actions in Dynamics 365 Finance and Operations D365 Snippets

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.


Delete Actions in Dynamics 365 Finance and Operations D365 Snippets

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.


Delete Actions in Dynamics 365 Finance and Operations D365 Snippets

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.

Post a Comment

0Comments
Post a Comment (0)