Dynamics 365 call workflow from c# (in code)

Let’s suppose you have a requirement to run a workflow on several records, so rather than going through selecting multiple records and running workflows, we can do this by calling workflow in code

Below is a sample code where we can get records first by fetch xml and then loop through each record to run workflow

CrmServiceClient _service = new CrmServiceClient(ConfigurationManager.ConnectionStrings["MyCRMConfig"].ConnectionString);
if (!_service.IsReady)
Console.WriteLine("No Connection was Made.");

Console.WriteLine(“Connected”);

//you can get workflow id by going to advanced find, select Processes entity and then look for process you want, export and you will get ID of process

var _workflowId = Guid.Parse(“543F265A-D9E3-40A4-BF08-410199BAE994”);

// get list of records – this is where you will defined your method to fetch more than one records
var _dataToProcess = GetAsbestosRecords(_service);
var _recordId = String.empty();

foreach (var item in _dataToProcess)
{
_recordId = item.Id;

// Create an ExecuteWorkflow request.
ExecuteWorkflowRequest request = new ExecuteWorkflowRequest()
{
WorkflowId = _workflowId,
EntityId = _recordId
};

ExecuteWorkflowResponse res =
(ExecuteWorkflowResponse)_service.Execute(request);
}

Comments are closed.