我正在尝试为CRM 2011制作一个插件,该插件会在禁用时更新联系人实体中的某些值。
当联系人被禁用时:我希望它将3个单选按钮更改为“Nei”(挪威语中没有)。这将禁止访问我为我的客户提供的自助服务门户。您可以通过其中的单选按钮here查看我的联系人实体的图片。当联系人被禁用时,我想强制所有这些单选按钮为“Nei”。
我是CRM插件开发的初学者,也是C#的新用户。所以请尽量保持简单。
我已经阅读了几周的手册,我似乎无法到达任何地方。 (好吧,微软并不以其精心编写的手册而闻名)。
答案 0 :(得分:1)
您需要在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);
}
}
}
}