如何在Microsoft Dynamics CRM 2016本地版中用插件创建Account后更新其邮箱
在Dynamics CRM 2016本地版中通过插件自动更新Account邮箱地址
插件开发与部署步骤
- 配置触发规则:插件需绑定到Account实体的
Create消息,执行阶段选择Post-operation(保证记录创建完成后再执行更新),执行模式设为Synchronous(同步执行)。 - 编写核心代码:
用C#编写插件类,实现IPlugin接口,核心逻辑是获取新创建的Account ID,构造更新请求设置硬编码邮箱。示例代码如下:using Microsoft.Xrm.Sdk; using System; namespace CRMPlugins { public class UpdateAccountEmail : IPlugin { public void Execute(IServiceProvider serviceProvider) { // 获取插件执行上下文与组织服务实例 IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService orgService = serviceFactory.CreateOrganizationService(context.UserId); // 校验触发条件,非Create消息或目标非Account则直接退出 if (context.MessageName != "Create" || context.PrimaryEntityName != "account" || !context.InputParameters.Contains("Target")) { return; } Entity newAccount = (Entity)context.InputParameters["Target"]; Guid accountId = newAccount.Id; // 构造更新实体,设置硬编码邮箱地址 Entity updateAccount = new Entity("account") { Id = accountId, ["emailaddress1"] = "fixed-account-email@yourdomain.com" // 替换为你的硬编码邮箱 }; // 执行更新操作 orgService.Update(updateAccount); } } } - 编译插件:引用Dynamics CRM SDK中的
Microsoft.Xrm.Sdk.dll和Microsoft.Crm.Sdk.Proxy.dll,将代码编译为类库(DLL)文件。 - 注册插件到CRM:使用Plugin Registration Tool(PRT)连接本地CRM组织,导入编译好的DLL,然后创建插件步骤:
- 关联实体:Account
- 触发消息:Create
- 执行阶段:Post-operation
- 执行模式:Synchronous
- 选择对应的插件类
UpdateAccountEmail
关键注意事项
- 确保注册插件的用户账号拥有Account实体的更新权限,否则插件执行会报错。
- 测试时创建新的Account记录,检查
emailaddress1字段是否被自动填充为硬编码值。 - 如需调试插件,可在PRT中启用调试模式,将Visual Studio调试器附加到CRM的
w3wp.exe进程进行断点调试。
内容的提问来源于stack exchange,提问作者Muhammad Usman Khan
相关产品推荐
相关产品推荐

