【问题标题】:CRM 2011 Samples - Account Number Generation ErrorCRM 2011 示例 - 帐号生成错误
【发布时间】:2012-06-21 22:59:15
【问题描述】:

我正在尝试编写我的第一个 Dynamics 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);

这是示例代码中的错误还是我做错了什么?如果不需要此代码,实体何时将其更改提交到数据库?

【问题讨论】:

    标签: dynamics-crm-2011


    【解决方案1】:

    刚刚解决了。您需要将此插件注册为“预操作”步骤。

    这将教会我将注册工具保留为默认值。将其设置为“预操作”时,实体的设置会在保存前更改。因此,您无需再次调用 save。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-05
      • 2011-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多