我正在尝试为 CRM 2011 制作一个插件,当它被禁用时,它将更新联系人实体中的一些值。
当联系人被禁用时:我希望它将 3 个单选按钮更改为“Nei”(挪威语中的否)。这将禁止我的客户访问我的自助服务门户。您可以在此处查看我的联系人实体的图片,其中包含单选按钮。当联系被禁用时,我想强制所有这些单选按钮为“Nei”。
我是 CRM 插件开发的完整初学者,也是 C# 的一个相当新的用户。所以请尽量保持简单。
我已经阅读手册好几个星期了,但我似乎无处可去。(嗯,微软并不以其写得很好的手册而闻名)。
我正在尝试为 CRM 2011 制作一个插件,当它被禁用时,它将更新联系人实体中的一些值。
当联系人被禁用时:我希望它将 3 个单选按钮更改为“Nei”(挪威语中的否)。这将禁止我的客户访问我的自助服务门户。您可以在此处查看我的联系人实体的图片,其中包含单选按钮。当联系被禁用时,我想强制所有这些单选按钮为“Nei”。
我是 CRM 插件开发的完整初学者,也是 C# 的一个相当新的用户。所以请尽量保持简单。
我已经阅读手册好几个星期了,但我似乎无处可去。(嗯,微软并不以其写得很好的手册而闻名)。
您需要将插件注册到消息SetState和SetStateDynamicEntity消息,Pre-Operation作为执行阶段。以这段代码为例:
using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace TestPlugin
{
public class UpdateBoolFields : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
try
{
if (context.InputParameters.Contains("EntityMoniker") &&
context.InputParameters["EntityMoniker"] is EntityReference)
{
EntityReference targetEntity = (EntityReference)context.InputParameters["EntityMoniker"];
OptionSetValue state = (OptionSetValue)context.InputParameters["State"];
if (state.Value == 1)// I'm not sure is 1 for deactivate
{
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
Entity contact = service.Retrieve(targetEntity.LogicalName, targetEntity.Id, new ColumnSet(true));
contact["field1"] = false;
contact["field2"] = false;
contact["field3"] = false;
service.Update(contact);
}
}
}
catch (Exception e)
{
throw new InvalidPluginExecutionException(e.Message);
}
}
}
}