locked
Updating The Primary Field via a C# Console App RRS feed

  • Question

  • Hi,

    CRM 2011 On-Premise
    Visual Studio Express 2013 for Windows Desktop

    I'm very new to both C# and the CRM 2011 SDK.

    I'm writing a simple console application which updates existing values on an Entity.

    The field "new_name" is the Primary Field for the Entity in CRM. It's set to have No Constraints.
    I'm able to update this field without any errors if I put it on a Form in CRM.

    If I run the following code however, the field new_background gets updated, but new_name retains its old value:

    How does CRM determine what fields can or cannot be updated when using the SDK? I'm probably missing something very obvious here.

    Thanks for your help.

    - Bob

    private static bool updTest(IOrganizationService crmService)
            {
                try
                {        
                    var lateBoundContext = new OrganizationServiceContext(crmService);
    
                    var inspQuery = (from inspRec in lateBoundContext.CreateQuery("new_inspection")
                                     where ((String)inspRec["new_shop"]).Contains("5250")                                 
                                    select inspRec); 
                                         
                    var iCount = inspQuery.AsEnumerable();
    
                    if (iCount.Any())
                    {
                        Console.WriteLine("Got Record(s)");
                        foreach (var inspRec in inspQuery)
                        {
                            lateBoundContext.UpdateObject(inspRec); // Tell CRM we want to update the Entity.
                            inspRec.Attributes["new_background"] = "TEST DATA XYZ"; // Works fine.
                            inspRec.Attributes["new_name"] = "Bob_1234X"; // Fails, with no error
    
                            Console.WriteLine("new_name is... {0}", inspRec.Attributes["new_name"]);
                                                   
                            crmService.Update(inspRec);                     
                        }
                    }                                
                    return true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error Updating in CRM.");
                    Console.WriteLine("Exception: " + ex.Message);
                    Console.ReadKey();
                    return false;
                }       
            }

    Friday, August 22, 2014 4:40 PM

Answers

  • It turns out there was a Plugin registered against the Entity which was overwriting my changes. Every time I updated the entity, the plugin fired. I've unregistered it (for now) and the update works fine.

    • Edited by barmstr Monday, August 25, 2014 1:57 PM
    • Marked as answer by barmstr Monday, August 25, 2014 1:58 PM
    Monday, August 25, 2014 1:57 PM

All replies

  • Hi ,

    First of all pls read crm sdk. There are some logical errors in ur code.

    And the below code works fine.

    inspRec.Attributes["new_background"] = "TEST DATA XYZ"; // Works fine. inspRec.Attributes["new_name"] = "Bob_1234X"; // Fails, with no error

    lateBoundContext.UpdateObject(inspRec);
    lateBoundContext.SaveChanges();

    //crmService.Update(inspRec); --no need this. Console.WriteLine("new_name is... {0}", inspRec.Attributes["new_name"]);




    Polat Aydın Crm Software Developer


    Friday, August 22, 2014 8:27 PM
  • It turns out there was a Plugin registered against the Entity which was overwriting my changes. Every time I updated the entity, the plugin fired. I've unregistered it (for now) and the update works fine.

    • Edited by barmstr Monday, August 25, 2014 1:57 PM
    • Marked as answer by barmstr Monday, August 25, 2014 1:58 PM
    Monday, August 25, 2014 1:57 PM