Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Not able to show the total count of updated inactive records through pluigins

(0) ShareShare
ReportReport
Posted on by 85

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.

8168.Screenshot-_2800_103_2900_.png




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

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Not able to show the total count of updated inactive records through pluigins

    Hi,

    Change "if (context.InputParameters["Target"] is EntityReference)" to if (context.InputParameters["Target"] is Entity). This should fix the current issue.

    Hope this helps.

  • Suggested answer
    Pawar Pravin  Profile Picture
    Pawar Pravin 5,227 on at
    RE: Not able to show the total count of updated inactive records through pluigins

    Hi,

    I'm curious to know about to on which event you want to trigger plugin.

    Very first thing I can see here that "EntityReference quote = (EntityReference)context.InputParameters["Target"];" and it refers that your plugin should be on delete of record where as "if (context.MessageName.ToUpper() == "UPDATE")" state that you want to check plugin context as UPDATE

  • Suggested answer
    Saad Kabarousse Profile Picture
    Saad Kabarousse 732 on at
    RE: Not able to show the total count of updated inactive records through pluigins

    Hi,

    i am not sure but i think the collection of quotes is null and seeing the exception it really refers to this., since you try to access Quote.Entities  which may cause the problem.

    docs.microsoft.com/.../system.collections.generic.keynotfoundexception

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans