Quantcast
Channel: Microsoft Dynamics CRM
Viewing all articles
Browse latest Browse all 154803

Forum Post: Plugin triggers but does not update entity

$
0
0
I am creating a plugin to update an account record with some data from the related activity (in this case, Appointment). The plugin works retrieving the records and attributes etc , but just does not execute the Update. Anyone point me in the right direction, going code blind and just can't see what I'm missing. My code is as follows (many thanks in advance) ITracingService tracingService = (ITracingService)serviceprovider.GetService(typeof(ITracingService)); // Obtain the execution context from the service provider. tracingService.Trace("AdvancedPlugin: Getting context depth."); IPluginExecutionContext context = (IPluginExecutionContext)serviceprovider.GetService(typeof(IPluginExecutionContext)); // Get a reference to the Organization service. IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceprovider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = factory.CreateOrganizationService(context.UserId); if (context.InputParameters != null) { tracingService.Trace("AdvancedPlugin: Get Current Entity."); Entity activityEntity = (Entity)context.InputParameters["Target"]; if (context.PostEntityImages.Contains("actvityImage") && context.PostEntityImages["actvityImage"] is Entity) { Entity postMessageImage = (Entity)context.PostEntityImages["actvityImage"]; OptionSetValue statecode = (OptionSetValue)postMessageImage["statecode"]; tracingService.Trace("AdvancedPlugin: Got Statecode."); if (statecode.Value != 1) return; if (postMessageImage.GetAttributeValue ("Statecode") == "1") return; tracingService.Trace("AdvancedPlugin: In Statecode IF"); // need to check entity name string entityName = postMessageImage.LogicalName; tracingService.Trace("AdvancedPlugin: Got Entity Name"); Guid actID; if (entityName != "phonecall") { tracingService.Trace("AdvancedPlugin: Not Phone Call"); EntityReference entityRef = (EntityReference)postMessageImage.Attributes["regardingobjectid"]; actID = entityRef.Id; tracingService.Trace("AdvancedPlugin: Got Act ID"); } else { EntityReference entityRef = (EntityReference)postMessageImage.Attributes["to"]; actID = entityRef.Id; } ColumnSet columns = new ColumnSet(new string[] { "name", "new_lastactivitydate", "new_ladate" }); tracingService.Trace("AdvancedPlugin: Got Columns"); Entity parentAccount = new Entity("account"); tracingService.Trace("AdvancedPlugin: set Account Entity"); parentAccount = service.Retrieve("account", actID, columns); tracingService.Trace("AdvancedPlugin: Got Parent Entity"); if (parentAccount.Id != null) { tracingService.Trace("AdvancedPlugin: Account Update code"); parentAccount["new_LastActivityDate"] = postMessageImage.GetAttributeValue ("actualend"); tracingService.Trace("AdvancedPlugin: Set Last Activity Date "+ parentAccount.Attributes["new_LastActivityDate"].ToString()); parentAccount["new_LADate"] = postMessageImage.GetAttributeValue ("actualend"); tracingService.Trace("AdvancedPlugin: Set Last Appt Date"); } else { //Entity parentContact = new Entity("contact"); ColumnSet concolumns = new ColumnSet("name", "parentcustomerid"); Entity parentContact = service.Retrieve("contact", actID, concolumns); ColumnSet parentcolumns = new ColumnSet(new string[] { "name", "new_lastactivitydate", "new_ladate" }); EntityReference entityRef2 = (EntityReference)parentContact.Attributes["parentcustomerid"]; Guid parentactID = entityRef2.Id; parentAccount = service.Retrieve("account", parentactID, parentcolumns); if (parentAccount.Id != null) { parentAccount["new_LastActivityDate"] = postMessageImage.GetAttributeValue ("actualend"); parentAccount["new_LADate"] = postMessageImage.GetAttributeValue ("actualend"); //service.Update(parentAccount); } } // update Account tracingService.Trace("AdvancedPlugin: Ready to Update"); service.Update(parentAccount); tracingService.Trace("AdvancedPlugin: Update"); }

Viewing all articles
Browse latest Browse all 154803

Trending Articles



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