简体   繁体   English

使用Dynamics CRM 2011 SDK和C#更新帐户

[英]Update Account using Dynamics CRM 2011 SDK and C#

I am using the Dynamics CRM 2011 C# SDK to write and read data from my on premise DynamicsCRM 2011. 我正在使用Dynamics CRM 2011 C#SDK从本地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 我目前正在从https://msdn.microsoft.com/zh-cn/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' 但是在执行此操作时,出现以下错误:'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. 另外,如果有人能推荐有关DynamicsCRM SDK的书籍或视频课程,我将不胜感激。

in your examples you are using early bound and the XrmContext to add and modify the account. 在示例中,您使用的是Early bound和XrmContext来添加和修改帐户。

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: 如果您已经在上下文中拥有该帐户(这意味着您在创建更新后立即执行更新,则只需更改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: 如果要更新不在上下文中的记录,则需要提供该记录的ID,如下所示:

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. 如果您只是从CRM SDK和CRUD操作开始,我建议使用后期绑定和IOrganizationService而不是XrmContext ,这会更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM