Here is my code ,it's working fine for the create scenario but it's throwing error to the update scenario.I am new to plugin development, Please help me with this. Actually I have written a plugin for the count total active quote and total in active quote on create/Update.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; namespace Plugin.Demo { public class UpdateAccountCounts : IPlugin { public void Execute(IServiceProvider serviceProvider) { IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = (IOrganizationService)serviceFactory.CreateOrganizationService(context.UserId); ITracingService tracingservice = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); tracingservice.Trace("MessageName = " + context.MessageName.ToUpper()); //As of now we are updating only Active Policy count on Member record on creation and deletion of the policy if (context.InputParameters["Target"] is EntityReference) { EntityReference quote = (EntityReference)context.InputParameters["Target"]; if (context.MessageName.ToUpper() == "UPDATE") { tracingservice.Trace("context.PreEntityImages.Contains(Member) : " + context.PreEntityImages.Contains("Member")); if (context.PreEntityImages.Contains("Member") && context.PreEntityImages["Member"] is Entity) { Entity preMessageImage = (Entity)context.PreEntityImages["Member"]; tracingservice.Trace("preMessageImage" + preMessageImage); // get topic field value before database update perform EntityReference member = (EntityReference)preMessageImage.Attributes["spl_member"]; //EntityReference member1 = (EntityReference)quote.KeyAttributes["spl_member"]; tracingservice.Trace("member.Id : " + member.Id); //if (quote.KeyAttributes.Contains("spl_member")) //{ QueryExpression inactiveQuote = new QueryExpression(); inactiveQuote.EntityName = "new_quote"; ColumnSet col = new ColumnSet("spl_reference"); inactiveQuote.ColumnSet = col; //inactiveQuote.Criteria.AddCondition("statecode", ConditionOperator.Equal, 1); FilterExpression childFilter11 = inactiveQuote.Criteria.AddFilter(LogicalOperator.And); childFilter11.AddCondition("spl_member", ConditionOperator.Equal, member.Id); FilterExpression newchildFilter = inactiveQuote.Criteria.AddFilter(LogicalOperator.Or); newchildFilter.AddCondition("spl_status", ConditionOperator.Equal, new Guid("1FBF73C5-F134-E911-A82E-000D3AB2DAFF")); newchildFilter.AddCondition("spl_status", ConditionOperator.Equal, new Guid("A97F6CD1-F134-E911-A82E-000D3AB2DAFF")); newchildFilter.AddCondition("spl_status", ConditionOperator.Equal, new Guid("D6E766EF-F134-E911-A82E-000D3AB2DAFF")); newchildFilter.AddCondition("spl_status", ConditionOperator.Equal, new Guid("8BEA5FF5-F134-E911-A82E-000D3AB2DAFF")); newchildFilter.AddCondition("spl_status", ConditionOperator.Equal, new Guid("37514582-551A-E911-A827-000D3AB2DDD3")); try { EntityCollection Inactivequotes = service.RetrieveMultiple(inactiveQuote); tracingservice.Trace("Quote Count = " + Inactivequotes.Entities.Count); updateAccountQuoteCount(Inactivequotes, member.Id, context.MessageName.ToUpper(), service, tracingservice); } catch (Exception ex) { tracingservice.Trace(" Plugin Error" + ex.Message.ToString()); throw new InvalidPluginExecutionException("Exception occured...."); } //} } } // throw new InvalidPluginExecutionException(); } if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { Entity quote = (Entity)context.InputParameters["Target"]; EntityReference member = (EntityReference)quote.Attributes["spl_member"]; if (context.MessageName.ToUpper() == "CREATE"){ if (quote.Attributes.Contains("new_member")) { // query.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0); QueryExpression oActiveQuote = new QueryExpression(); oActiveQuote.EntityName = "new_quote"; ColumnSet col = new ColumnSet("new_reference"); oActiveQuote.ColumnSet = col; FilterExpression childFilter1 = oActiveQuote.Criteria.AddFilter(LogicalOperator.And); childFilter1.AddCondition("spl_member", ConditionOperator.Equal, member.Id); FilterExpression childFilter = oActiveQuote.Criteria.AddFilter(LogicalOperator.Or); childFilter.AddCondition("spl_status", ConditionOperator.Equal, new Guid("301657E9-F134-E911-A82E-000D3AB2DAFF")); childFilter.AddCondition("spl_status", ConditionOperator.Equal, new Guid("F7EA5FF5-F134-E911-A82E-000D3AB2DAFF")); oActiveQuote.Criteria.AddCondition("spl_enddate", ConditionOperator.OnOrAfter, DateTime.Today); try { EntityCollection activeQuote = service.RetrieveMultiple(oActiveQuote); // EntityCollection ActiveQuote = service.RetrieveMultiple(oActiveQuote); updateAccountQuoteCount(activeQuote, member.Id, context.MessageName.ToUpper(), service, tracingservice); } catch (Exception ex) { tracingservice.Trace(" Plugin Error" + ex.Message.ToString()); throw new InvalidPluginExecutionException("Exception occured...."); } // throw new InvalidPluginExecutionException(); } } } } private void updateAccountQuoteCount(EntityCollection Quotes, Guid id, string message, IOrganizationService service, ITracingService tracingservice) { int quoteCount = 0; int inquoteCount = 0; string fieldName = ""; string fieldName1 = ""; tracingservice.Trace("message = " + message); if (message == "CREATE") { quoteCount = Quotes.Entities.Count + 1; fieldName = "spl_counmemberactivequotes"; } tracingservice.Trace("message = " + message); if (message == "UPDATE") { inquoteCount = Quotes.Entities.Count + 1; fieldName1 = "spl_counmemberinactivequotes"; } try { tracingservice.Trace("Quotes.Entities.Count; = " + Quotes.Entities.Count); Entity updateMember = new Entity("account", id); Entity updateMember1 = new Entity("account", id); if (message == "CREATE") { updateMember[fieldName] = quoteCount; } if (message == "UPDATE") { updateMember1[fieldName1] = inquoteCount; } // throw new InvalidPluginExecutionException(); tracingservice.Trace("Updating the Member count "); service.Update(updateMember); service.Update(updateMember1); } catch (Exception ex) { tracingservice.Trace(" Plugin Error" + ex.Message.ToString()); throw new InvalidPluginExecutionException(); } } } }
*This post is locked for comments