Wednesday, October 29, 2008

CRM 3.0 Retrieve Workflow Process By Name

Just wanted to put this on the blog.  I used this snippet of code to retrieve the id of a wfprocess, and set the id to an instance of ExeWFProcessRequest for post create execution.  Of course doing this way, we have to ensure that the Workflows' names are unique to each other.


// Use a generic request to retrieve multiple records.
RetrieveMultipleRequest request = new RetrieveMultipleRequest ();

// Create the ConditionExpression object.
ConditionExpression nameCondition = new ConditionExpression();
ConditionExpression processTypeCondition = new ConditionExpression();
ConditionExpression stateCondition = new ConditionExpression();

// Set the condition to be the name equals workflowName.
nameCondition.AttributeName = "name";
nameCondition.Operator = ConditionOperator.Equal;
nameCondition.Values = new string [] {workflowName};

//specify a workflow process TAKE NOTICE
processTypeCondition.AttributeName = "processtypecode";
processTypeCondition.Operator = ConditionOperator.Equal;
processTypeCondition.Values = new string [] {"1"};

// Create the FilterExpression object.
FilterExpression filter = new FilterExpression();

// Set the filter's properties.
filter.FilterOperator = LogicalOperator.And;
filter.Conditions = new ConditionExpression[] {nameCondition, processTypeCondition};

// Create the QueryExpression object.
QueryExpression query = new QueryExpression();

// Set the QueryExpression object's properties.
query.EntityName = EntityName.wfprocess.ToString();
query.ColumnSet = new AllColumns();
query.Criteria = filter;

// Set the Request object's properties.
request.Query = query;

// Execute the request.
RetrieveMultipleResponse response = (RetrieveMultipleResponse) service.Execute(request);

// Get the results from the response.
BusinessEntityCollection entities = response.BusinessEntityCollection;

if(entities.BusinessEntities.Length == 1)
{
return ((wfprocess)(entities.BusinessEntities[0])).processid.Value.ToString();
}



Cheers,

Hong Tan
Microsoft Dynamics CRM Developer

No comments: