crm通过插件对手机号码去重

在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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值