Back to Basics # 55: Delete Record using XRM WebApi with Webresource in Dynamics CRM

Introduction:

In Dynamics 365 CRM, for certain requirements we need to delete records in tables.  As an example, for a selected contact record ,record gets deleted.

Step 1:

Login to the required environment and select required solution [Contact Customizations Solution in this case] as shown in the   below figure.

Step 2:

After Step 1, select contact web resource in solution and click on Edit as shown in the below figure.

Step 3:

After Step 2, we prepare a contactGUID for a selected contact record with the below code

let contactGUID=Xrm.Page.data.entity.getId().replace(“{“,””).replace(“}”,””);

As shown in the below figure

Step 4:

After Step 3, prepare request to delete record on deleteRecord method of Xrm.WebApi after passing arguments “contact” and guid of contact like below code  

Xrm.WebApi.deleteRecord(“contact”, contactGUID).then(

                function success(result) {

                    console.log(“Contact deleted”);

                    // perform operations on record deletion

                },

                function (error) {

                    console.log(error.message);

                    // handle error conditions

                }

                );

As shown in the below figure

Step 5:

After Step 4, final code looks like below

if (typeof (ContosoVaccination) == “undefined”)

{

    var ContosoVaccination = {__namespace: true};

}

if (typeof (ContosoVaccination.Scripts) == “undefined”)

{

    ContosoVaccination.Scripts = {__namespace: true};

}

ContosoVaccination.Scripts.ContactForm =

{

    handleOnLoad: function (executionContext)

        {

        console.log(‘on load – contact form’);

        deleteContact(executionContext);

        },

    __namespace: true

}

function  deleteContact(executionContext)

{

    let formContext = executionContext.getFormContext();

    if (formContext !== null && formContext != ‘undefined’)

    {

        let contactGUID=Xrm.Page.data.entity.getId().replace(“{“,””).replace(“}”,””);

                // delete the record

                Xrm.WebApi.deleteRecord(“contact”, contactGUID).then(

                function success(result) {

                    console.log(“Contact deleted”);

                    // perform operations on record deletion

                },

                function (error) {

                    console.log(error.message);

                    // handle error conditions

                }

                );

    }

}

As shown in the below figure

Step 6:

After Step 5, save Webresource and publish it and register this Webresource in form load event of contact form and publish all the customisations and open a contact record and observe console window that Contact Deleted , and once you observe in the list of contacts, then the selected contact will not be present as  shown in the below figure.

Note:

  1. Make sure to publish all customizations and upload JavaScript (js) file.
  2. Make sure to provide exact logical name of contact entity and contact table.
  3. Microsoft documentation can be found here

Conclusion: In this way, one can easily perform delete operation on crm table using XRM Web API deleteRecord method.

2 thoughts on “Back to Basics # 55: Delete Record using XRM WebApi with Webresource in Dynamics CRM

  1. Pingback: Back To Basics :Curated List of Articles in a Single Page – Common Man Tips for Power Platform, Dynamics CRM,Azure

  2. Pingback: Rewind May 2022 – Common Man Tips for Power Platform, Dynamics CRM,Azure

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s