Pages

Tuesday, 26 June 2012

Run ondemand workflow from C# code in CRM 2011


In this article , I am going to explain how to run ondemand workflow using C# code.

Code To get ondemand workflow


String _workflowName = "workflowname";
try
{
   ServerConnection serverConnect = new ServerConnection();
   ServerConnection.Configuration serverConfig = serverConnect.GetServerConfiguration();

   _serviceProxy = new     OrganizationServiceProxy(serverConfig.OrganizationUri,serverConfig.HomeRealmUri,serverConfig.Credentials,serverConfig.DeviceCredentials);
_serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
    IOrganizationService service = (IOrganizationService)_serviceProxy;


   Workflow _Workflow = null;
   // Create the ColumnSet that indicates the properties to be retrieved.
   ColumnSet _columnsWorkflow = new ColumnSet(new string[] { "name", "workflowid" });

  // Create the ConditionExpression for association Name field.
  ConditionExpression _conditionAssociationName = new ConditionExpression();

  //Set the condition for the retrieval to be based on the workflow name.
  _conditionAssociationName.AttributeName = "name";
  _conditionAssociationName.Operator = ConditionOperator.Equal;
  _conditionAssociationName.Values.Add(_workflowName);

  ConditionExpression _type = new ConditionExpression();
  _type.AttributeName = "type";
  _type.Operator = ConditionOperator.Equal;
  _type.Values.Add("1");

  //Create the FilterExpression.
  FilterExpression _filterRetrieveWorkflow = new FilterExpression();
  _filterRetrieveWorkflow.FilterOperator = LogicalOperator.And;
 _filterRetrieveWorkflow.AddCondition(_conditionAssociationName);
 _filterRetrieveWorkflow.AddCondition(_type);

 //Create the QueryExpression object.
 QueryExpression _queryRetrieveWorkflow = new QueryExpression();
 _queryRetrieveWorkflow.EntityName = Workflow.EntityLogicalName;
 _queryRetrieveWorkflow.ColumnSet = _columnsWorkflow;
 _queryRetrieveWorkflow.Criteria = _filterRetrieveWorkflow;

 //Retrieve the workflow
 EntityCollection workflows = service.RetrieveMultiple(_queryRetrieveWorkflow);
 if (workflows.Entities.Count > 0)
 {
   _Workflow = (Workflow)workflows.Entities[0];
   EntityCollection entityCollection = GetentityCollections(service);

   //Workflow execute request and response
   ExecuteWorkflowRequest _requestExecuteWorkflow = new ExecuteWorkflowRequest();
   ExecuteWorkflowResponse _responseExecuteWorkflow = new ExecuteWorkflowResponse();
   _requestExecuteWorkflow.WorkflowId = _Workflow.Id;//.workflowid.Value;

   //Opportunity entity
   foreach (Entity entity in entityCollection.Entities)
  {
    Entity _association = (Entity)entity;
   _requestExecuteWorkflow.EntityId = new Guid(_association.Attributes["opportunityid"].ToString());
    _responseExecuteWorkflow =  (ExecuteWorkflowResponse)service.Execute(_requestExecuteWorkflow);

   }

  }
}
catch (System.Web.Services.Protocols.SoapException ex)
{
    string _error = ex.Detail.InnerText;
}
catch (Exception ex)
{
   if (ex.InnerException != null)
   {
    string _error = ex.InnerException.Message;
    }
}




Code To get all opportunity on which we need to run workflow


private EntityCollection GetentityCollections(IOrganizationService _crmService)
{
   QueryExpression fullUserQuery = new QueryExpression();
   fullUserQuery.EntityName = Opportunity.EntityLogicalName;

   ColumnSet _columns = new ColumnSet(new string[] { "opportunityid" });
   fullUserQuery.ColumnSet = _columns;
   fullUserQuery.Criteria = new FilterExpression();

   fullUserQuery.Criteria.Conditions.Add(new  ConditionExpression("opportunityratingcode", ConditionOperator.Equal, "3"));
   fullUserQuery.Criteria.Conditions.Add(new ConditionExpression("statecode", ConditionOperator.Equal, 0));

   RetrieveMultipleRequest retrieveRequest = new RetrieveMultipleRequest();

   retrieveRequest.Query = fullUserQuery;

   return ((RetrieveMultipleResponse)_crmService.Execute(retrieveRequest)).EntityCollection;
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.