0
我正在尝试编写我的第一个Dynamics Crm 2011插件。CRM 2011样本 - 帐号生成错误
因此,我下载了CRM11 sdk并查看了插件示例。我终于使用提供的工具安装并注册了我的第一个插件(至少对我来说这是一个非常陡峭的学习曲线)。
我从基础入手,编写了样本中提供的“账号插件”,并使用了注册工具。所有被报告为安装成功,但创建一个新的帐户生成的帐号根本没有显示出来。我最初认为插件没有正确安装,但在注意到代码会抛出一个错误,如果帐号已经存在,我着手尝试让插件引发错误。这是第一次,所以我很高兴安装是正确的。
查看示例代码后,我失去了如何将生成的帐号保存回Dynamis Crm。
// An accountnumber attribute should not already exist because it is system generated.
if (entity.Attributes.Contains("accountnumber") == false)
{
// Create a new accountnumber attribute, set its value, and add
// the attribute to the entity's attribute collection.
Random rndgen = new Random();
entity.Attributes.Add("accountnumber", rndgen.Next().ToString());
}
else
{
// Throw an error, because account numbers must be system generated.
throw new InvalidPluginExecutionException("The account number can only be set by the system.");
}
翻看一些其他样本,我注意到一些额外的代码似乎被称为创建新的实体。我调整了这一点并将其复制到新的帐号代码中,并部署了帐号并按预期生成帐号。
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)
serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
service.Update(entity); // Originally - service.Create(entity);
这是示例代码中的错误还是我做错了什么?如果实体向数据库提交其更改时不需要此代码?