Wednesday, October 02, 2013

Code snippet to execute workflow crm 2011


Use the code template from here

Code:
ExecuteWorkflowRequest request = new ExecuteWorkflowRequest();
                //Assign the ID of the workflow you want to execute to the request.        
                request.WorkflowId = new Guid("129112BD-A601-43DB-97DA-59AD13743069");

                string[] cust_str = {"000000XXXXX"};
                foreach (string cust in cust_str)
                {
                    ConditionExpression con_ce1 = new ConditionExpression("accountnumber", ConditionOperator.Equal, cust);
                    FilterExpression con_fe = new FilterExpression(LogicalOperator.And);
                    con_fe.AddCondition(con_ce1);
                    QueryExpression con_qe = new QueryExpression();
                    con_qe.EntityName = "account";
                    con_qe.Criteria = con_fe;
                    con_qe.ColumnSet = new ColumnSet("accountid", "accountnumber");
                    con_qe.PageInfo.ReturnTotalRecordCount = true;
                    EntityCollection con_coll = _service.RetrieveMultiple(con_qe);

                    foreach (Entity ent in con_coll.Entities)
                    {
                        request.EntityId = ent.Id;
                        ExecuteWorkflowResponse response = (ExecuteWorkflowResponse)_service.Execute(request);
                    }
                }

No comments: