How to call custom workflow activity (c# code) to initiate a rest call On button click IN Dynamics 365

896 Views Asked by At

Could anyone please help me to reach out this question -

How to call custom workflow activity to initiate a rest call in opportunity entity while clicking a button in Microsoft Dynamics CRM?

If this is possible please tell me how to archive this. Thanks in advance

1

There are 1 best solutions below

1
On

Code snippet for calling workflow from Front end (Js) in crm

var parameters = {};
parameters.EntityId = "1236666666666666666666666666666666";
parameters.InputArguments =  "Your custom parameter";

var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/workflows(E633A91A-CFD8-40D5-A3BD-10E55CE941D6)/Microsoft.Dynamics.CRM.ExecuteWorkflow", false);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            var results = JSON.parse(this.response);
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send(JSON.stringify(parameters));

As you are on D365 I would suggest use Xrm.Webapi call as below

var parameters = {};
var entity = {};
entity.id = "E633A91A-CFD8-40D5-A3BD-10E55CE941D6";
entity.entityType = "workflow";
parameters.entity = entity;
parameters.EntityId = "1236666666666666666666666666666666";
parameters.InputArguments = "Your custom parameter";

var executeWorkflowRequest = {
    entity: parameters.entity,
    EntityId: parameters.EntityId,
    InputArguments: parameters.InputArguments,

    getMetadata: function() {
        return {
            boundParameter: "entity",
            parameterTypes: {
                "entity": {
                    "typeName": "mscrm.workflow",
                    "structuralProperty": 5
                },
                "EntityId": {
                    "typeName": "Edm.Guid",
                    "structuralProperty": 1
                },
                "InputArguments": {
                    "typeName": "mscrm.InputArgumentCollection",
                    "structuralProperty": 5
                }
            },
            operationType: 0,
            operationName: "ExecuteWorkflow"
        };
    }
};

Xrm.WebApi.online.execute(executeWorkflowRequest).then(
    function success(result) {
        if (result.ok) {
            var results = JSON.parse(result.responseText);
        }
    },
    function(error) {
        Xrm.Utility.alertDialog(error.message);
    }
);