Thursday, January 18, 2024

Unlink Entities -Account and contact on Button Click in Sub-grid | Dynamics 365

Unlinking Contacts from Accounts on A button Click: A Step-by-Step Guide in Dynamics 365

Hello everyone, and happy New Year! In this blog post, we will explore the process of working with the account form in Dynamics 365. Specifically, we'll delve into the creation of a custom button that unlinks related contacts from the account. This involves utilizing the Ribbon Workbench, configuring JavaScript, and ensuring a seamless user experience.

Part 1 video below-

Part 2 video below-



Ribbon Workbench Configuration

Watch the videos provided to observe the practical implementation. Follow the step-by-step instructions outlined in the videos for a hands-on understanding-
  • Step 1: Install Ribbon WorkBench
  • Step 2: Creating a New Solution
  • Step 3: Adding Buttons to the Form
  • Step 4: Handling Multiple Selections
  • Step 5: JavaScript Configuration
  • Step 6: Debugging and Testing

JavaScript Code for above implementation-

function deselectContact(selectedItemIds) {

    // Check if any records are selected

    if (selectedItemIds.length > 0) {

        // Ask for confirmation before proceeding

        var confirmDialog = confirm("Are you sure you want to deselect the selected contact(s)?");   

        if (confirmDialog) {

            // Loop through selected records and perform the delink operation

            selectedItemIds.forEach(function (contactId) {

                // Log the current Contact ID to the console

                console.log("Deselecting Contact ID: " + contactId);

                // Construct the Web API endpoint to update the Contact record (replace version according to your Dynamics 365 version)

                var endpoint = "/api/data/v9.2/contact(" + contactId.replace("{", "").replace("}", "") + ")";   

                // Prepare the data to update the 'accountid' field to null

                var data = {

                    "accountid@odata.bind": null

                };

                // Perform the Web API update request

                Xrm.WebApi.online.updateRecord("contact", contactId, data).then(

                    function success(result) {

                        // Log success message to the console

                        console.log("Contact record deselected successfully.");

                        // Refresh the Account form and subgrid to reflect the changes

                        Xrm.Page.data.refresh(true);

                        Xrm.Page.getControl("Subgrid_new_1").refresh();

                        // Show success message

                        alert("Contact record(s) have been successfully deselected.");

                    },

                    function error(error) {

                        // Log the error message and details to the console

                        console.log("Error deselecting Contact record:", error.message, error);

                        // You may want to handle the error in a way that helps diagnose the issue

                    }

                );

            });

        } else {

            // User canceled the operation

            console.log("Deselection operation canceled by the user.");

        }

    } else {

        // No records selected, display a message or perform other actions as needed

        console.log("No records selected to deselect.");

    }

}


2 comments:

  1. hello sir, I have some doubt. Please clarify. for (var endpoint = "/api/data/v9.2/cr922_contactlist(" + contactId.replace("{", "").replace("}", "") + ")";), cr922_contactlist is my contactlist entity logical name. is the procedure wrong or correct?

    ReplyDelete
  2. var data = {
    "ws_acountid@odata.bind": null
    }; here ws_acount is the lookup in contactlist entity . Is it correct or not?

    ReplyDelete

Followers

Power Dynamix YouTube Videos