c# - The given key was not present in the dictionary Microsoft CRM Plugin -


another microsoft crm plugin related question. plugin firing when invoice record updated. want copy contents of of invoice fields corresponding fields on commission record. if contains due date seems work ok if containing invoiceamount gives key not present error. have checked field names correct , exist in correct entities.

any ideas what's happening? need use images , if so, me code, please?

thanks

// <copyright file="preinvoiceupdate.cs" company=""> // copyright (c) 2013 rights reserved // </copyright> // <author></author> // <date>8/8/2013 10:40:22 am</date> // <summary>implements preinvoiceupdate plugin.</summary> // <auto-generated> //     code generated tool. //     runtime version:4.0.30319.1 // </auto-generated> namespace updatecommission {     using system;     using system.servicemodel;     using microsoft.xrm.sdk;     using microsoft.xrm.sdk.query;       /// <summary>     /// preinvoiceupdate plugin.     /// fires when following attributes updated:     /// attributes     /// </summary>         public class preinvoiceupdate : plugin     {         /// <summary>         /// initializes new instance of <see cref="preinvoiceupdate"/> class.         /// </summary>         public preinvoiceupdate()             : base(typeof(preinvoiceupdate))         {             base.registeredevents.add(new tuple<int, string, string, action<localplugincontext>>(20, "update", "invoice", new action<localplugincontext>(executepreinvoiceupdate)));              // note : can register more events here if plugin not specific individual entity , message combination.             // may need update registerfile.crmregister plug-in registration file reflect change.         }          /// <summary>         /// executes plug-in.         /// </summary>         /// <param name="localcontext">the <see cref="localplugincontext"/> contains         /// <see cref="ipluginexecutioncontext"/>,         /// <see cref="iorganizationservice"/>         /// , <see cref="itracingservice"/>         /// </param>         /// <remarks>         /// improved performance, microsoft dynamics crm caches plug-in instances.         /// plug-in's execute method should written stateless constructor         /// not called every invocation of plug-in. also, multiple system threads         /// execute plug-in @ same time. per invocation state information         /// stored in context. means should not use global variables in plug-ins.         /// </remarks>         protected void executepreinvoiceupdate(localplugincontext localcontext)         {             if (localcontext == null)             {                 throw new argumentnullexception("localcontext");             }              // todo: implement custom plug-in business logic.              // obtain execution context service provider.             ipluginexecutioncontext context = localcontext.pluginexecutioncontext;             iorganizationservice service = localcontext.organizationservice;             iserviceprovider serviceprovider = localcontext.serviceprovider;             itracingservice tracingservice = localcontext.tracingservice;               // obtain target entity input parmameters.             //entity contextentity = (entity)context.inputparameters["target"];              if (context.depth == 1)             {                  #region set variables                  entity targetinvoice = null;                 targetinvoice = (entity)context.inputparameters["target"];                 guid invoiceid = targetinvoice.id;                   columnset invoicecols = new columnset("new_totalcomm", "new_grossprofit", "new_invoiceamount", "duedate");                   //entity contact = service.retrieve("contact", cid, cols);                 //contact.attributes["jobtitle"] = "sometitle";                 // contact.attributes["address1_line1"] = "some address";                 //service.update(contact);                   string fetchcommissionquery = @" <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>   <entity name='tbw_commission'>     <attribute name='tbw_commissionid' />     <attribute name='tbw_name' />     <attribute name='createdon' />     <order attribute='tbw_name' descending='false' />     <filter type='and'>       <condition attribute='new_relatedinvoice' operator='eq' uitype='invoice' value='";                  fetchcommissionquery += invoiceid;                 fetchcommissionquery += @"' />     </filter>   </entity> </fetch> ";                   entitycollection commissioncollection = service.retrievemultiple(new fetchexpression(fetchcommissionquery));                  //targetinvoice = service.retrieve("invoice", invoiceid, invoicecols);                  #endregion                  foreach (var commission in commissioncollection.entities)                 {                     if (targetinvoice.attributes["duedate"] != null)                     {                          commission.attributes["tbw_duedate"] = targetinvoice.attributes["duedate"];                     }                      if (targetinvoice.attributes["new_invoiceamount"] != null)// && commission.attributes["tbw_paymentrate"] != null)                     {                         //dynamic commrate = commission.attributes["tbw_paymentrate"];                          //dynamic invoicetotal = ((money)targetinvoice.attributes["new_invoiceamount"]).value;                          //dynamic commtotal = commrate * invoicetotal;                          //commission.attributes["tbw_total"] = new money(commtotal);                         //commission.attributes["tbw_total"] = 1.02;                     }                      //commission.attributes["tbw_name"] = "testcommname";                     service.update(commission);                  }                   // dynamic currentinvoicetotal = ((money)targetinvoice.attributes["new_invoiceamount"]).value;                  // currentinvoicetotal = currentinvoicetotal - 10;                  // entity invoice = service.retrieve("invoice", invoiceid, invoicecols);                 // invoice.attributes["new_grossprofit"] = new money(currentinvoicetotal);                 //service.update(invoice);                  //}              }         }     } } 

in update, have value of fields updated. other fields won't there in target entity. need register plugin preimage. add fields preimage want access in plugin.

preimage = (entity)context.preentityimages["preimage"]; 

how register plugin image


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -