Monday, 22 July 2019

Move New Stage in Buisness Process Flow Praogramitcally

pdateSalesProcessStage(IOrganizationService service)
        {
            Guid oppId = new Guid("2b685c81-4f1c-e511-80d3-3863bb347ba8");
            Entity opportunity = service.Retrieve("opportunity", oppId, new ColumnSet(true));
            //Entity entity = this.Opportunity;
            //var source = opportunity.GetAttributeValue<OptionSetValue>("lvo_source").Value;
            var source = 100000000;
            string fetch = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                              <entity name='opportunitysalesprocess'>
                                <attribute name='businessprocessflowinstanceid' />
                                <attribute name='name' />
                                <attribute name='createdon' />
                                <attribute name='opportunityid' />
                                <attribute name='activestageid' />
                                <attribute name='statecode' />
                                <attribute name='statuscode' />
                                <attribute name='processid' />
                                <order attribute='opportunityid' descending='false' />
                                <filter type='and'>
                                  <condition attribute='opportunityid' operator='eq' uiname='4' uitype='opportunity' value='{0}' />
                                </filter>
                              </entity>
                            </fetch>";
            fetch = string.Format(fetch, oppId);
            EntityCollection ec = service.RetrieveMultiple(new FetchExpression(fetch));
            QueryExpression qe = new QueryExpression("processstage");
            qe.ColumnSet = new ColumnSet(true);
            qe.Criteria.AddCondition(new ConditionExpression("processid", ConditionOperator.Equal, ec.Entities[0].GetAttributeValue<EntityReference>("processid").Id));
            EntityCollection pc = service.RetrieveMultiple(qe);
            Dictionary<string, Guid> dict = new Dictionary<string, Guid>();
            foreach (var p in pc.Entities)
            {
                dict.Add(p.GetAttributeValue<string>("stagename"), p.GetAttributeValue<Guid>("processstageid"));
            }
            Guid processStageId = RetrieveProcessStageId(dict, source);
            //if (dict.ContainsKey("Qualify"))
            //{
            //    dict.TryGetValue("Qualify",out processStageId);
            //}
            Entity pro = new Entity(ec.Entities[0].LogicalName);
            pro.Id = ec.Entities[0].Id;
            pro.Attributes["activestageid"] = new EntityReference("processstage", processStageId);
            service.Update(pro);
            Console.WriteLine("done");
        }
   
        private static Guid RetrieveProcessStageId(Dictionary<string, Guid> dict, int sourceValue)
        {
            Guid processId = Guid.Empty;
            if (sourceValue == 100000000)
                processId = dict.ContainsKey("Qualify") ? dict["Qualify"] : Guid.Empty;
            else if (sourceValue == 100000002)
                processId = dict.ContainsKey("Qualify") ? dict["Qualify"] : Guid.Empty;
            else if (sourceValue == 100000003)
                processId = dict.ContainsKey("Propose") ? dict["Propose"] : Guid.Empty;
            else if (sourceValue == 100000001)
                processId = dict.ContainsKey("Develop") ? dict["Develop"] : Guid.Empty;
            return processId;
        }
    }
}

No comments:

Post a Comment