Wednesday, 27 November 2024

CRM 365 Cloud - Disassociate 2 records using typescript

In case you need to Disassociate 2 records, please find below the code that allows you to do that.     


export async function DissociateEntities(primaryEntityType: string, primaryEntityTypeId: string, secondaryEntityName: string, secondaryEntityId: string, relationshipName: string) {


        debugger;

        var Sdk = window.Sdk || {};


        Sdk.DisassociateRequest = function (target: string, relatedEntityId: string, relationship: string) {

            this.target = target;

            this.relatedEntityId = relatedEntityId;

            this.relationship = relationship;

        };


        // NOTE: The getMetadata property should be attached to the function prototype instead of the

        // function object itself.

        Sdk.DisassociateRequest.prototype.getMetadata = function () {

            return {

                boundParameter: null,

                parameterTypes: {},

                operationType: 2, // Associate and Disassociate fall under the CRUD umbrella

                operationName: "Disassociate"

            }

        };


        // Construct the target EntityReference object

        var target = {

            entityType: primaryEntityType,

            id: primaryEntityTypeId

        };


        // The GUID of the related entity record to disassociate.

        var relatedEntityId = secondaryEntityId;


        // The name of the existing relationship to disassociate from.

        var relationship = relationshipName;


        var manyToManyDisassociateRequest = new Sdk.DisassociateRequest(target, relatedEntityId, relationship)


        await Xrm.WebApi.online.execute(manyToManyDisassociateRequest).then(

            function success(result) {

                console.log("Disassiciate Success!");

                // perform operations on record deletion

            },

            function (error) {

                console.log("Error Disassociating Entities " + error.message);

                console.log(error.message);

                // handle error conditions

            }

        );

    }

}


Hope it helps


No comments:

Post a Comment

CRM 365 Cloud - Disassociate 2 records using typescript

In case you need to Disassociate 2 records, please find below the code that allows you to do that.      export async function DissociateE...