Quantcast
Channel: CRM Development forum
Viewing all articles
Browse latest Browse all 8223

How to trigger a plugin when an opportunity is won

$
0
0

 I am trying to sum a value on the account field when an opportunity is won. I have the following code but it does not retrieve the value of the current record been update. What am i missing. Please Help.


 if(! this.PluginExecutionContext.PrimaryEntityName.Contains(Xrm.Opportunity.EntityLogicalName))                
                    return;                                         
                    if (this.InputTargetEntity.Contains("statuscode"))
                    {
                         var CrmContext = new Xrm.XrmServiceContext(this.OrganizationService);
                        var StatusReason = ((OptionSetValue)(this.InputTargetEntity.Attributes["statuscode"])).Value;
                        if (StatusReason == 695030015 || StatusReason == 695030016 || StatusReason == 695030017 || StatusReason == 695030018 || StatusReason == 695030019 || StatusReason == 695030020)
                        {
                            var Opportunity = (from a in CrmContext.OpportunitySet
                                              where a.OpportunityId == this.PluginExecutionContext.PrimaryEntityId
                                              select new { a.CustomerId.Id }).SingleOrDefault();
                            if(Opportunity != null)
                            {
                               var ActualValue = CrmContext.OpportunitySet.Where(o => o.CustomerId.Id == Opportunity.Id && (o.StatusCode.Value == 695030015 || o.StatusCode.Value == 695030016 || o.StatusCode.Value == 695030017 || o.StatusCode.Value == 695030018 || o.StatusCode.Value == 695030019 || o.StatusCode.Value == 695030020)).ToList().Sum(o => o.ActualValue == null ? 0 : o.ActualValue.Value);
                               Xrm.Account oAccount = new Xrm.Account();
                               oAccount.AccountId = Opportunity.Id;
                               oAccount.new_LifetimeValue = new Money(ActualValue);
                               this.OrganizationService.Update(oAccount);
                            }
                        }
                    }


Viewing all articles
Browse latest Browse all 8223

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>