1

I am using the Dynamics CRM 2011 C# SDK to write and read data from my on premise DynamicsCRM 2011.

I am currently starting out with this basic example from https://msdn.microsoft.com/en-us/library/gg695803%28v=crm.5%29.aspx

So, creating new Account or Contact and writing it to the Dynamics seems to be no problem and works like this:

var companyTest = new Xrm.Account
{
  Name = "Company Test1",
  AccountNumber = "1",
  Address1_Country = "D",
  Address1_City = "M",
  Telephone1 = "12345678",
  EMailAddress1 = "oldmail@gg.com"
};

 xrm.AddObject(companyTest);
 xrm.SaveChanges();

Now I don't fully understand how I can change some information on the Account I created.

So I tried this:

var companyTest = new Xrm.Account
{
  Name = "Company Test1",
  AccountNumber = "1",
  Address1_Country = "D",
  Address1_City = "M",
  Telephone1 = "12345678",
  EMailAddress1 = "newmail@gg.com" // change the email for instance
};

xrm.UpdateObject(companyTest);
xrm.SaveChanges();

But when doing this I get the following error: 'System.InvalidOperationException'

How do I do this properly?

Also, I would be very grateful if someone could recommend a book or a video course on DynamicsCRM SDK.

Simon Karlsson
  • 4,090
  • 22
  • 39
Ivan Bilan
  • 2,379
  • 5
  • 38
  • 58

1 Answers1

2

in your examples you are using early bound and the XrmContext to add and modify the account.

If you already have the account inside the context (meaning you are executing the update right after you create it, you just need to change the values inside the companyTest:

var companyTest = new Xrm.Account
{
  Name = "Company Test1",
  AccountNumber = "1",
  Address1_Country = "D",
  Address1_City = "M",
  Telephone1 = "12345678",
  EMailAddress1 = "oldmail@gg.com"
};

 xrm.AddObject(companyTest);
 xrm.SaveChanges();

companyTest.AccountNumber = "2";
xrm.UpdateObject(companyTest);
xrm.SaveChanges();

if you are doing the update of a record that is not inside the context yet, you will need to provide the Id of the record, something like this:

Guid accountId = new Guid(""); // account id here
var companyTestUpdate = new Xrm.Account
{
  Id = accountId,
  AccountNumber = "2"
};

xrm.UpdateObject(companyTest);
xrm.SaveChanges();

If you are just starting with CRM SDK and CRUD operations, I suggest to use late bound and the IOrganizationService instead of XrmContext, it's easier.

Guido Preite
  • 14,905
  • 4
  • 36
  • 65
  • Thanks a lot. I had to use this before doing the update though: xrm.Attach(companyTestUpdate); Found it here: http://stackoverflow.com/questions/11455592/ms-dynamics-crm-2011-sdk-update-entity-record-using-late-binding – Ivan Bilan Feb 05 '16 at 09:57
  • 1
    I would advise against using the XrmServiceContext for updates, as it might trigger workflows even if the attributes didn't change. Better to use the IOrganizationService directly for that. Also, would be good, if you are starting, to have a look at using a testing framework to keep your customizations as robust as possible, like [this](https://github.com/jordimontana82/fake-xrm-easy) one on Git. – Jordi Feb 06 '16 at 00:50
  • @Jordi I'm going to be giving a talk on unit testing for the XrmVirtual UG. I plan on showing both of our frameworks. – Daryl Feb 06 '16 at 00:58
  • Great! :) will that be broadcasted anywhere? Would be good to see. Going to bed now, it is 2 AM here. – Jordi Feb 06 '16 at 01:04
  • I would like to know too ) – Ivan Bilan Feb 12 '16 at 14:35