Quantcast
Channel: CRM Development Forum
Viewing all articles
Browse latest Browse all 888

Updating Custom Field Using Plugin

$
0
0
What am I missing here. I am trying a simple update to a custom field, that is on the form, with the following plug in. I am using strongly typed class generated using the CRMSvcUtil called MyServiceContext. Code is below. If this is not correct, how should I be updating the field? The step for this plug in is registered Pre-Exection on Update message for quotedetail entity. There are no errors but the update never happens. Any pointers. Thank you.
  try
            {
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

                //throw new InvalidPluginExecutionException("Got the context.");
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                //if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference)
                {
                    //throw new InvalidPluginExecutionException("In target");
                    //EntityReference QuoteDetailEnity = (EntityReference)context.InputParameters["Target"];
                    Entity QuoteDetailEntity = (Entity)context.InputParameters["Target"];

                    //throw new InvalidPluginExecutionException("ENTITY IS " + QuoteDetailEntity.LogicalName);

                    if (QuoteDetailEntity.LogicalName == "quotedetail")
                    {
                        //throw new InvalidPluginExecutionException("ENTITY IS " + QuoteDetailEnity.LogicalName);

                        IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

                        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                        ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

                        //Get an image of the referenced entity and return a collection of the fields needed  in this case we are concerned with  the quote  id
                        Entity entityImage = service.Retrieve(QuoteDetailEntity.LogicalName, QuoteDetailEntity.Id, new ColumnSet(true));

                        Guid quoteDetailId = (Guid)entityImage.Attributes["quotedetailid"];
                        var prodLineNotes = entityImage.Attributes["vcs_productlinenotes"];

                        prodLineNotes = "Updated through plugin";

                        //throw new Exception("Prod Line Note was updated to  " + " " + prodLineNotes);

                        ////use the service to get all proposals related to the opportunity 
                        using (var crm = new MyServiceContext(service))
                        {
                            //var ent = crm.QuoteDetailSet.Where(q => q.QuoteDetailId == ownerGuid).ToList();

                            var ent = crm.QuoteDetailSet.Where(q => q.QuoteDetailId == quoteDetailId).ToList();

                            //throw new Exception("There is " + " " + ent.Count() + " " + "quotedetaildid is " + " " + quoteDetailId);
                            foreach (QuoteDetail qtd in ent)
                            {
                                qtd.vcs_ProductLineNotes = prodLineNotes.ToString();
                            }
                            crm.SaveChanges();
                            //throw new Exception("Save Changes Passes ");// + " " + qtd.vcs_ProductLineNotes);
                        }
                    }
                    {
                        //throw new InvalidPluginExecutionException("Save Failed");
                    }
                }
            }


Viewing all articles
Browse latest Browse all 888

Trending Articles



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