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


CRM 365 Online - TypeScript - Execute Workflow


Please find a snippet code below for executing a workflow from TypeScript.

The only requirement here is to set the WorkflowId and the Id of the record plus the errorMessage and successMessage to use.

Xrm.Utility.showProgressIndicator("Creating .......");



let request =

{

    entity: { id: workflowID, entityType: "workflow" },

    EntityId: { guid: Id },

    getMetadata: function () {

        return {

            boundParameter: "entity",

            operationType: 0,

            //operation name is always “Execute Workflow” independent of workflow name

            operationName: "ExecuteWorkflow", parameterTypes: {

                "entity": {

                    "typeName": "mscrm.workflow",

                    "structuralProperty": 5

                },

                "EntityId": {

                    "typeName": "Edm.Guid",

                    "structuralProperty": 1

                }

            }

        }

    }

};



Xrm.WebApi.online.execute(request).then(

    function (result: any) {

        //alert("The confirmation email will be generated.");

        debugger;


        Xrm.Utility.closeProgressIndicator();


        let alertStrings = { confirmButtonLabel: "Close", text: successMessage, title: "Success" };

        let alertOptions = { height: 200, width: 400 };

        Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then(

            function (success) {

                console.log("Alert dialog closed");

            },

            function (error) {

                console.log(error.message);

            }

        );

    },

    function (error: any) {


        debugger;


        Xrm.Utility.closeProgressIndicator();


        let alertStrings = { confirmButtonLabel: "Close", text: errorMessage, title: "Error" };

        let alertOptions = { height: 200, width: 400 };

        Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then(

            function (success) {

                console.log("Alert dialog closed");

            },

            function (error) {

                console.log(error.message);

            }

        );


    });


Hope it helps.

Wednesday, 20 November 2024

CRM 365 Cloud - Open Form in new window or the same window with Typescript

Please find below the code for opening a form in Typescript.


 export function OpenForm(entityName: string, entityId : string, openInNewWindow : boolean) {


     let entityFormOptions = {

         entityName: entityName,

         entityId: entityId,

         openInNewWindow: openInNewWindow

     };

     // Open the form.

     Xrm.Navigation.openForm(entityFormOptions).then(

         function (success) {

             console.log(success);

         },

         function (error) {

             console.log(error);

         });


 }


Hope it helps.

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...