In this webinar, our experts showcase a variety of demo use cases of how different components of the...
Workflows are an extremely useful tool in Microsoft Dynamics CRM. In today's blog, we'll discuss how to execute a workflow using the CRM ExecuteWorkflowRequest message in a console application.
In this scenario, a workflow has failed a couple of times. Instead of re-running this workflow manually, we can use the code below to automate the process.
The screenshot below shows the System Jobs view of the workflow named Workflow Test that has failed.
The ExecuteWorkflowRequest message can be used to execute any given workflow. The ExecuteWorkflowMessage requires two values:
Note: You must ensure that the WorkflowId is of an active workflow.
You can manually set these values by using the CRM interface to attain each of these values and simply call the ExecuteWorkflowRequest in the console application.
The following code automates this process by doing the following:
Console application code
After the console application executed, the failed workflow was rerun.
System Jobs view after console application was executed and workflow succeeded
When a workflow is created, CRM creates two entries in the workflow entity. When retrieving failed workflows from the asyncoperation entity, you will notice the workflowactivationid attribute has a reference to the workflow. If you attempt to use this value as the WorkflowId, you will get an error stating "Workflow must be in a published state" when the ExecuteWorkflowRequest message is executed.
The GetWorkflowId helper method is used to retrieve the published workflow by querying the workflow entity and retrieving the parentworkflowid attribute. This value will correspond to the WorkflowId that you will get when using the CRM interface.
You will want to expand the logic to determine a distinct set of failed workflows and regarding entity that have not succeeded.
There you have it! An easy way to automatically execute a workflow using a console application. If you are looking for help with your workflows, PowerObjects can help! Contact us for more information.
Happy CRM'ing!
I followed your step to resume workflow which status is "waiting" but it does not work. Can you please help me out, what i'm missing there.Below are my code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using Microsoft.Crm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm.Sdk.Messages;
using System.Runtime.Serialization;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Client.Services;
using System.IO;
using Das.crm.Entites;
using Microsoft.Xrm.Sdk.Query;
namespace ActivateWaitingWorkFlow
{
class Program
{
private static OrganizationServiceProxy _serviceProxy;
private static ClientCredentials _clientCreds;
static void Main(string[] args)
{
// Setup credential
_clientCreds = new ClientCredentials();
_clientCreds.Windows.ClientCredential.UserName = "username";
_clientCreds.Windows.ClientCredential.Password = "password";
_clientCreds.Windows.ClientCredential.Domain = "domain";
/
using (_serviceProxy = new OrganizationServiceProxy(new Uri("Webserviceurl/XRMServices/2011/Organization.svc"), null, _clientCreds, null))
{
_serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
OrganizationServiceContext orgContext = new OrganizationServiceContext(_serviceProxy);
var workflowQuery = new QueryExpression("asyncoperation");
workflowQuery.ColumnSet.AllColumns = true;
workflowQuery.Criteria = new FilterExpression();
workflowQuery.Criteria.AddCondition(new ConditionExpression("recurrencestarttime", ConditionOperator.Null));
workflowQuery.Criteria.AddCondition(new ConditionExpression("createdon", ConditionOperator.LastXDays, "1"));
workflowQuery.Criteria.AddCondition(new ConditionExpression("statuscode", ConditionOperator.Equal, "10"));
workflowQuery.Criteria.AddCondition(new ConditionExpression("name", ConditionOperator.Equal, "Set Initial Event Data Fields")); ;
hs_event getevent = (from e in orgContext.CreateQuery() select e).FirstOrDefault();
var v = getevent.Id;
//var value = _serviceProxy.RetrieveMultiple(workflowQuery);
//var result = value.Entities.FirstOrDefault();
EntityCollection value = _serviceProxy.RetrieveMultiple(workflowQuery);
foreach (Entity result in value.Entities)
{
try
{
var request = new ExecuteWorkflowRequest()
{
WorkflowId = GetWorkflowId(result.GetAttributeValue("workflowactivationid").Id), //new Guid("{c37905b5-b29c-425b-8995-9bd8a4293a46}"),//
EntityId = result.GetAttributeValue("regardingobjectid").Id
};
//EntityReference regardingRef = (EntityReference)result["regardingobjectid"];
//bool isContact = regardingRef.LogicalName == "hs_event";
orgContext.Execute(request);
//_serviceProxy.Execute(request);
}
catch
{
}
orgContext.SaveChanges();
}
}
}
private static Guid GetWorkflowId(Guid guid)
{
var workflowQuery = new QueryExpression("workflow");
workflowQuery.ColumnSet.AllColumns = true;
workflowQuery.Criteria = new FilterExpression();
workflowQuery.Criteria.AddCondition(new ConditionExpression("workflowid", ConditionOperator.Equal, guid));
var result = _serviceProxy.RetrieveMultiple(workflowQuery);
return result.Entities[0].GetAttributeValue("parentworkflowid").Id;
}
}
}