Friday, 4 September 2015

Get the Stage and Process of the Business Process


Today we got a requirement to do some action based on Opportunity Stage.
Even though there is a field as stepname on the Opportunity entity. I see that field is not updated all the times. So, here is the C# code for knowing the Stage of the record.
string strStageName = string.Empty;
// Get StageId, ProcessId of the record.
QueryExpression objQueryExpr = new QueryExpression("opportunity");
objQueryExpr.Criteria.AddCondition(new ConditionExpression("opportunityid", ConditionOperator.Equal, "<OpportunityId>"));
objQueryExpr.ColumnSet = new ColumnSet(new string[] { "stepname", "stageid", "processid" });

EntityCollection entColProcessStages = iService.RetrieveMultiple(objQueryExpr);
// Query Process Stage by passing StageId and get the name of the stage.
QueryExpression objQueryExpr1 = new QueryExpression("processstage");
objQueryExpr1.Criteria.AddCondition(new ConditionExpression("processid", ConditionOperator.Equal, entColProcessStages.Entities[0].Attributes["processid"]));
objQueryExpr1.ColumnSet = new ColumnSet(true);
entColProcessStages = iService.RetrieveMultiple(objQueryExpr1);
if (entColProcessStages != null && entColProcessStages.Entities.Count > 0)
     strStageName = entColProcessStages.Entities[0]["stagename"].ToString();

Hope this helps.

Happy CRM'ing

No comments:

Post a Comment