在vs2019中的主体代码
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PostCreateAccount
{
public class PostCreateAccountPhone : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
//获取插件上下文
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
//组织服务工厂
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
//组织服务
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
Entity targer = (Entity)context.InputParameters["Target"];//传入单条数据
if (targer.Attributes.Contains("new_mobile"))
{
var mobile = targer["new_mobile"].ToString();
var fetchXML = $@"<fetch mapping='logical' version='1.0'>
<entity name='new_account'>
<attribute name='new_mobile' />
<attribute name='new_mobile2' />
<filter type='or'>
<condition attribute='new_mobile' operator='eq' value='" + mobile+"'/>" +
"<condition attribute='new_mobile2' operator='eq' value='" + mobile + "'/>" +
"</filter></entity></fetch>";
var response = service.RetrieveMultiple(new FetchExpression(fetchXML));
if (response.Entities.Count()>0)
{
throw new Exception("手机号码重复!");
}
}
}
}
}
1.插件注册的时候 选择post会导致查询到本身,所有要选择 pre-operation