locked
Dynamics CRM Plugin: Update record base on another record RRS feed

  • Question

  • Hello,

    We have a form with 2 categories “technical and controls”. When the user creates the control record, we have a workflow to create the Technical record. On the update of the control record the technical record should be updated based on the Control record. I created a plugin on the pre operation update, the code retrieves the correct values but doesn’t save the changes. Please check the below code

    namespace PTD.Plugins

    {

        using System;

        using System.ServiceModel;

        using Microsoft.Xrm.Sdk;

        using Microsoft.Xrm.Sdk.Query;

        using System.Linq;

        public class PreProjectTechnicalDataUpdate01: Plugin

        {

            public PreProjectTechnicalDataUpdate01()

                : base(typeof(PreProjectTechnicalDataUpdate01))

            {

                base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(20, "Update", "new_projecttechnicaldata", new Action<LocalPluginContext>(ExecutePreProjectTechnicalDataUpdate01)));

     }

            protected void ExecutePreProjectTechnicalDataUpdate01(LocalPluginContext localContext)

            {

                if (localContext == null)

                {

                    throw new ArgumentNullException("localContext");

                }

                Entity entity = null;

                Entity preentity = null;

                if (localContext.PluginExecutionContext.InputParameters.Contains("Target") && localContext.PluginExecutionContext.InputParameters["Target"] is Entity)

                {

                    entity = (Entity)localContext.PluginExecutionContext.InputParameters["Target"];

              

                }

                else

                {

                    return;

                }

                if (localContext.PluginExecutionContext.PreEntityImages.Contains("test"))

                {

                    preentity = (Entity)localContext.PluginExecutionContext.PreEntityImages["test"];

                }

                   

                else

                {

                    return;

                }

                new_projecttechnicaldata company = entity.ToEntity<new_projecttechnicaldata>();

                new_projecttechnicaldata project = preentity.ToEntity<new_projecttechnicaldata>();

                using (GeneratedEntities orgContext = new GeneratedEntities(localContext.OrganizationService))

                {

                    if (company.new_Type == "Technical")

                    {

                    }

                    else if (project.new_Type == "Technical")

                    {

                    }

                    else

                    {

                        var CT = (from b in orgContext.new_projecttechnicaldataSet

                                      where (b.GetAttributeValue<Guid>("new_company") == project.new_Company.Id)

                                      && (b.GetAttributeValue<Guid>("new_project") == project.new_Project.Id)

                                      && (b.GetAttributeValue<DateTime>("new_date") == project.new_Date)

                                      && b.GetAttributeValue<string>("new_type") == "Technical"

                                      select b).FirstOrDefault();

                        Int32 CertifiedClaims = 0;

                        if (company.Contains("new_certifiedclaims"))

                        {

                            CertifiedClaims = (Int32)company.new_CertifiedClaims;

                        }

                        else if (project.Contains("new_certifiedclaims"))

                        {

                            CertifiedClaims = (Int32)project.new_CertifiedClaims;

                        }

                        CT["new_certifiedclaims"] = CertifiedClaims;

                        decimal GrossProfitt = 0;

                        if (company.Contains("new_grossprofitt"))

                        {

                            GrossProfitt = (decimal)company.new_GrossProfitt;

                        }

                        else if (project.Contains("new_grossprofitt"))

                       {

                            GrossProfitt = (decimal)project.new_GrossProfitt;

                        }

                        CT["new_grossprofitt"] = GrossProfitt;

                       

                    }   

                }

            }

        }

    }

    Thursday, March 2, 2017 1:33 PM

All replies

  • Hi Eman,

    It seems like eventhough you are setting the values you are not updating the record.

    At the end you need to either call

    localContext.OrganizationService.Update(CT);
                      
                       Or                 
    orgContext.UpdateObject(CT);     
    orgContext.SaveChanges();

    Thank you.


    Sachith Chandrasiri

    Thursday, March 2, 2017 9:55 PM
  • I tried to use the following:orgContext.UpdateObject(CT);  orgContext.SaveChanges();  OR olocalContext.OrganizationService.Update(CT);

    but without any luck.

    I can view the updated value during the debugging but it doesn't update the record.

    Friday, March 3, 2017 5:58 AM
  • Look at the following code
                                        Entity entity = new Entity();
                                        entity.Id = [IdOfRecord];
                                        entity.LogicalName = [EntityName];
                                        entity.Attributes["rpa_sharepointfileid"] = targetDocId.ToString();
                                        service.Update(entity);


    Regards Faisal

    Monday, March 6, 2017 10:09 AM