title: Dynamics 365 - Plugin Does Not Update Values
body: <p>I have been chasing this down since yesterday, and nothing makes sense.  I have gone through various permutations of what the code can look like - changed the Decimal type function to String and returned a String instead of a decimal, Used the decimal number, hard coding the value within the if statement (not using a variable), and using the variable itself, but nothing seems to work.  If I just set the fields value in the beginning of the try block, and don't do any type of logic, the fields are being udpdated.  As I step through the code via Plugin Profiles/Debug in plugin registration tool, I can see that the lines of code to set the values are being hit, and no exceptions are being thrown, but again, the values are not being updated.  I even tried to add the service.Update(entity); but that did not work either (and when I set the values of the fields without any logic, I did not need the service.Update call to save the values).</p>
  
  <p>The field I am trying to write to in CRM is of Decimal type with 2 precision.  Of course, decimal types in C# are of 8 or 10 precision, so I have even tried to trim the decimal, convert the decimal to a string, etc etc- but nothing works.</p>
  
  <p>Any ideas to help me figure out what's going on here?</p>
  
  <p>I have posted the <em>simplest</em> version of my code right here- before I went and changed the Decimal type function to string, etc.  </p>
  
  <pre><code>    using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.ServiceModel;
  using Microsoft.Xrm.Sdk;
  using Microsoft.Xrm.Sdk.Client;
  using System.Net;
  using System.Net.Mail;
  
  
  
  
  namespace ClientNTE
  {
      public class UpdateVals : IPlugin
      {
          public void Execute(IServiceProvider serviceProvider)
          {
  
              IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
  
  
              if (context.InputParameters.Contains("Target") &amp;&amp; context.InputParameters["Target"] is Entity)
              {
  
                  Entity entity = (Entity)context.InputParameters["Target"];
  
                  IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                  IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                  ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
  
  
                  try
                  {
                      //Get current record's GUID, which will always be in the attributes collection
                      Guid MainEntityID = new Guid(entity["msdyn_workorderid"].ToString());
  
                      //Get related entity record's GUID from generic field value grabber function against the main entity
  
                      Guid RefEntityID = GetGUIDFieldValueFrmID(service, "msdyn_workorder", "msdyn_workorderid", MainEntityID, "msdyn_agreement");
  
                      //if it has a value, continue
  
                      if (RefEntityID != Guid.Empty)
                      {
                          Decimal RefEntityFieldValue = GetDecFieldValueFrmID(service, "msdyn_agreement", "msdyn_agreementid", RefEntityID, "client_ntepercent");
  
                          //if it has a value, continue
  
                          if (RefEntityFieldValue &gt; -99999999)
                          {
                              entity["client_ntepercent"] = RefEntityFieldValue;
                          }
                      }
  
                  }
                  catch (Exception ex)
                  {
                      //write errors to the CRM Plugin Trace Log
                      tracingService.Trace("clientNTE - Agreement To Work Order - ", ex.ToString());
                      //Throw error through UI
                      throw new InvalidPluginExecutionException("Error, Please See Plugin Log");
                  }
              }
          }
  
          public Guid GetGUIDFieldValueFrmID(IOrganizationService svc, String EntityNm, String EntityIDField, Guid EntityIDValue, String ReturnFieldNm)
          {
              Guid retval = Guid.Empty;
  
              try
              {
                  OrganizationServiceContext orgContext = new OrganizationServiceContext(svc);
  
                  var ReturnRecords = from a in orgContext.CreateQuery(EntityNm)
                                      where (Guid)a[EntityIDField] == EntityIDValue
                                      select new
                                      {
                                          FieldVal = a[ReturnFieldNm]
                                      };
  
                  if (ReturnRecords != null)
                  {
                      foreach (var EvalRec in ReturnRecords)
                      {
                          retval = ((Microsoft.Xrm.Sdk.EntityReference)EvalRec.FieldVal).Id;
                      }
                  }
                  else
                  {
                      retval = Guid.Empty;
                  }
  
  
              }
              catch (Exception ex)
              {
                  retval = Guid.Empty;
                  //Throw error through UI
                  throw new InvalidPluginExecutionException(ex.ToString());
              }
  
              return retval;
          }
  
          public Decimal GetDecFieldValueFrmID(IOrganizationService svc, String EntityNm, String EntityIDField, Guid EntityIDValue, String ReturnFieldNm)
          {
              Decimal retval = -99999999;
  
              try
              {
                  OrganizationServiceContext orgContext = new OrganizationServiceContext(svc);
  
                  var ReturnRecords = from a in orgContext.CreateQuery(EntityNm)
                                      where (Guid)a[EntityIDField] == EntityIDValue
                                      select new
                                      {
                                          FieldVal = a[ReturnFieldNm]
                                      };
  
                  if (ReturnRecords != null)
                  {
                      foreach (var EvalRec in ReturnRecords)
                      {
                          retval = Convert.ToDecimal(EvalRec.FieldVal);
                      }
                  }
                  else
                  {
                      retval = -99999999;
                  }
  
  
              }
              catch (Exception ex)
              {
                  retval = -99999999;
                  //Throw error through UI
                  throw new InvalidPluginExecutionException(ex.ToString());
              }
  
              return retval;
          }
  
          //public static Boolean UpdateParentRecord(IOrganizationService svc, Guid ParentRecordID, String FieldValue)
          //{
  
          //    Boolean retval = false;
  
          //    try
          //    {
          //        Entity parent_entityrec = new Entity("parent_entity");
  
          //        parent_entityrec["fieldtoupdate"] = FieldValue;
  
          //        svc.Update(parent_entityrec);
  
          //        retval = true;
  
          //    }
  
          //    catch (Exception ex)
          //    {
          //        retval = false;
          //    }
  
          //    return retval;
  
          //}
      }
  }
  </code></pre>
  
  <p>Now, here you can see my current code, where I am doing a bunch of crazy stuff - First, using a String type function to get the decimal value back.  Second, calling a function to actually set the value, just to prove that there's not something wrong with the existing function (and to allow for easier formatting down the line).  No matter what I do, I can't get this dang thing to  work!</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.ServiceModel;
  using Microsoft.Xrm.Sdk;
  using Microsoft.Xrm.Sdk.Client;
  using System.Net;
  using System.Net.Mail;
  
  
  
  namespace CLIENTNTE
  {
      public class AgreementToWorkOrder : IPlugin
      {
          public void Execute(IServiceProvider serviceProvider)
          {
  
              IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
  
  
              if (context.InputParameters.Contains("Target") &amp;&amp; context.InputParameters["Target"] is Entity)
              {
  
                  Entity entity = (Entity)context.InputParameters["Target"];
  
                  IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                  IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                  ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
  
  
                  try
                  {
                      //string Num = 12.500000.ToString();
                      //entity["CLIENT_testdecimal"] = Num;
                      //entity["CLIENT_ntepercent"] = Num;
  
                      //Get current record's GUID, which will always be in the attributes collection
                      Guid MainEntityID = new Guid(entity["msdyn_workorderid"].ToString());
  
                      //Get related entity record's GUID from generic field value grabber function against the main entity
  
                      Guid RefEntityID = GetGUIDFieldValueFrmID(service, "msdyn_workorder", "msdyn_workorderid", MainEntityID, "msdyn_agreement");
  
                      //if it has a value, continue
  
                      if (RefEntityID != Guid.Empty)
                      {
                          String RefEntityFieldValue = GetDecFieldValueFrmID(service, "msdyn_agreement", "msdyn_agreementid", RefEntityID, "CLIENT_ntepercent");
  
                          decimal decVal = Convert.ToDecimal(RefEntityFieldValue);
                          //if it has a value, continue
                          if (decVal &gt; -99999999)
                          {
                              // entity["CLIENT_ntepercent"] = RefEntityFieldValue;
                              // entity["CLIENT_ntepercent"] = "12.5";// RefEntityFieldValue;
                              //entity["CLIENT_testdecimal"] = "13.5";// RefEntityFieldValue;
                              setDecimal(RefEntityFieldValue, serviceProvider);
                              //service.Update(entity);
                          } 
                      }
  
                  } 
                  catch (Exception ex)
                  {
                      //write errors to the CRM Plugin Trace Log
                      tracingService.Trace("CLIENTNTE - Agreement To Work Order - ", ex.ToString());
                      //Throw error through UI
                      throw new InvalidPluginExecutionException("Error, Please See Plugin Log");
                  }
              }
          }
  
          public void setDecimal(String RefEntityFieldValue, IServiceProvider serviceProvider)
          {
  
              IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
              //IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
              //IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
              //ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
              Entity entity = (Entity)context.InputParameters["Target"];
              //Guid MainEntityID = new Guid(entity["msdyn_workorderid"].ToString());
              //Guid RefEntityID = GetGUIDFieldValueFrmID(service, "msdyn_workorder", "msdyn_workorderid", MainEntityID, "msdyn_agreement");
  
              //String RefEntityFieldValue = GetDecFieldValueFrmID(service, "msdyn_agreement", "msdyn_agreementid", RefEntityID, "CLIENT_ntepercent"); */
  
              // entity["CLIENT_testdecimal"] = RefEntityFieldValue;
              entity["CLIENT_testdecimal"] = 12;
              entity["CLIENT_ntepercent"] = RefEntityFieldValue;
  
  
          }
  
          public Guid GetGUIDFieldValueFrmID(IOrganizationService svc, String EntityNm, String EntityIDField, Guid EntityIDValue, String ReturnFieldNm)
          {
              Guid retval = Guid.Empty;
  
              try
              {
                  OrganizationServiceContext orgContext = new OrganizationServiceContext(svc);
  
                  var ReturnRecords = from a in orgContext.CreateQuery(EntityNm)
                                      where (Guid)a[EntityIDField] == EntityIDValue
                                      select new
                                      {
                                          FieldVal = a[ReturnFieldNm]
                                      };
  
                  if (ReturnRecords != null)
                  {
                      foreach (var EvalRec in ReturnRecords)
                      {
                          retval = ((Microsoft.Xrm.Sdk.EntityReference)EvalRec.FieldVal).Id;
                      }
                  }
                  else
                  {
                      retval = Guid.Empty;
                  }
  
  
              }
              catch (Exception ex)
              {
                  retval = Guid.Empty;
  
                  throw new InvalidPluginExecutionException(ex.ToString());
              }
  
              return retval;
          }
  
          public String GetDecFieldValueFrmID(IOrganizationService svc, String EntityNm, String EntityIDField, Guid EntityIDValue, String ReturnFieldNm)
          {
              Decimal retval = -99999999;
              String stringVal = "";
  
              try
              {
                  OrganizationServiceContext orgContext = new OrganizationServiceContext(svc);
  
                  var ReturnRecords = from a in orgContext.CreateQuery(EntityNm)
                                      where (Guid)a[EntityIDField] == EntityIDValue
                                      select new
                                      {
                                          FieldVal = a[ReturnFieldNm]
                                      };
  
                  if (ReturnRecords != null)
                  {
                      foreach (var EvalRec in ReturnRecords)
                      {
                          retval = Convert.ToDecimal(EvalRec.FieldVal);
                          // stringVal = retval.ToString().TrimEnd('0', '.');
                          stringVal = retval.ToString();
                      }
                  }
                  else
                  {
                      retval = -99999999;
                  }
  
  
              }
              catch (Exception ex)
              {
                  retval = -99999999;
                  throw new InvalidPluginExecutionException(ex.ToString());
              }
  
              return stringVal;
          }
      }
  }
  </code></pre>
  
  <p>Any and all input appreciated.  </p>