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 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 the “Reason For Deletion” feature in Dynamics 365 CE using html and JavaScript.
Implementation
We can implement this feature using the following steps.
Step 1. First, we can add a field in our entity where we want to store the reason for deletion. It can be a single line of text or a multiline 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 a dialog to get the reason. To implement dialog, you can refer to my earlier post here.
Step 3. Next, we need to write a script to update and delete the record. Here, we need to write two methods: first, for the update and delete to delete the record. We can use the following JavaScript to implement both 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 the above code, we are using the async keyword, which indicates that this function will contain asynchronous operations, typically involving promises and await. It takes three parameters: id, reasonfordelete, and entityname. To update the record, we need to create an object where we can set up the 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, I am using a progress indicator to show as soon as the deletion process starts and stop when the process is over or if any error occurs.
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.
I hope it will help someone !!
Keep learning and Keep Sharing !!