locked
Updating Custom Field Using Plugin RRS feed

  • Question

  • 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");
                        }
                    }
                }

    Tuesday, February 26, 2013 7:25 PM

Answers

  • Hi,

    You are missing the UpdateObject method you must execute on every tracked object you have modified.

    foreach (QuoteDetail qtd in ent)
    {
       qtd.vcs_ProductLineNotes = prodLineNotes.ToString();
       crm.UpdateObject(qtd);
    }
    
    crm.SaveChanges();

    Regards,

    Henk


    If this post is an answer or helpful, please do not forget to vote!

    • Proposed as answer by Henk van Boeijen Wednesday, February 27, 2013 1:27 PM
    • Marked as answer by valCrmDev2 Thursday, March 7, 2013 12:02 AM
    Wednesday, February 27, 2013 11:34 AM
  • check this!!!

    http://stackoverflow.com/questions/10329610/update-contact-middlename-field-on-form-load-ms-crm-2011-plugin

    http://crmconsultancy.wordpress.com/2010/10/25/plugins-in-crm-2011/


    ms crm

    • Marked as answer by valCrmDev2 Thursday, March 7, 2013 12:02 AM
    Wednesday, February 27, 2013 1:16 PM

All replies

  • Hi,

    You are missing the UpdateObject method you must execute on every tracked object you have modified.

    foreach (QuoteDetail qtd in ent)
    {
       qtd.vcs_ProductLineNotes = prodLineNotes.ToString();
       crm.UpdateObject(qtd);
    }
    
    crm.SaveChanges();

    Regards,

    Henk


    If this post is an answer or helpful, please do not forget to vote!

    • Proposed as answer by Henk van Boeijen Wednesday, February 27, 2013 1:27 PM
    • Marked as answer by valCrmDev2 Thursday, March 7, 2013 12:02 AM
    Wednesday, February 27, 2013 11:34 AM
  • check this!!!

    http://stackoverflow.com/questions/10329610/update-contact-middlename-field-on-form-load-ms-crm-2011-plugin

    http://crmconsultancy.wordpress.com/2010/10/25/plugins-in-crm-2011/


    ms crm

    • Marked as answer by valCrmDev2 Thursday, March 7, 2013 12:02 AM
    Wednesday, February 27, 2013 1:16 PM
  • Thanks guys, i got it to work.
    Thursday, March 7, 2013 12:02 AM