Implementing Reason for Delete in D365 CE

Requirement
In Dynamics 365 Customer Engagement (CE), managing data is crucial for maintaining a clean and efficient database. When we delete records, out of the box auditing feature can help to see audit information of the deleted record but what if we want to see the reason why this record was deleted?. With a simple customization, we can implement this feature to store why a record is being deleted, providing transparency and a valuable audit trail for your data management processes. In this blog post, we’ll walk you through the steps to implement “Reason For Deletion” feature in Dynamics 365 CE using html and JavaScript.
reasonfordeletion
Implementation
We can implement this feature using following steps
Step 1
First we can add a field in our entity where we want to store reason for deletion, it can be a single line of text or multiline of text field depending on our requirements.
Step 2
We need to hide out of the box delete button and need to add our custom delete button where we can call our custom JavaScript to open dialog to get the reason. To implement dialog you can refer my earlier post here

Step 3
Next, we need to write script to update and delete record, here we need to write two methods first for the update and delete to delete record. We can use following JavaScript to implement both the functions

async function ProcessRecords(id, reasonfordelete, entityname) {
        Xrm.Utility.showProgressIndicator("Deleting Records....");
        var record = {};
        record.him_reasonfordeletion = reasonfordelete;

        await Xrm.WebApi.updateRecord(entityname, id, record).then(

            async function success(result) {
                console.log("record updated");
                await Xrm.WebApi.deleteRecord(entityname, id).then(
                    function success(result) {
                        console.log("record deleted");
                        Xrm.Utility.closeProgressIndicator();
                    },
                    function (error) {
                     console.log(error.message);
                     Xrm.Utility.closeProgressIndicator();

                    }
                );

            },
            function (error) {
                console.log(error.message);
                Xrm.Utility.closeProgressIndicator();
            }
        );

    }

In above code we are using async keyword indicates that this function will contain asynchronous operations, typically involving promises and await. It takes three parameters: id, reasonfordelete, and entityname. To update record we need to create a object where we can setup entities field, in our case we just want to update him_reasonfordeletion, which is set to the value of the reasonfordelete parameter. As this process can take time so I am using progress indicator to show as soon as deletion process will start and stop it when process is over or any error occurred.

Summary
This is how we can implement “Reason For Deletion” in Dynamics 365 CE, which is a valuable step toward improving data management, compliance, and accountability in your organization. By following the steps outlined in this blog post, you can ensure that records are deleted with clear and documented reasons, helping you maintain a clean and reliable database.

Hope it will help someone !!
Keep learning and Keep Sharing !!

Leave a Reply

Your email address will not be published. Required fields are marked *