在销售线索实体中新建,跟进任务自动创建一条数据并实现自动编号

该博客介绍了如何在CRM系统中编写一个插件,该插件在销售线索创建后自动执行。插件首先检查并生成唯一的编号,然后更新或创建存放编号的实体记录。同时,它会创建一个新的跟进任务,用于跟踪销售线索。注册插件时,选择了'创建'事件,针对'销售线索'实体,并在操作后执行。
摘要由CSDN通过智能技术生成

1.创建一个实体存放编号

2.代码如下:

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 PostCreateLeads
{
    public class PostCreatefollowup : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            //获取插件上下文
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            //组织服务工厂     
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            //组织服务        
            IOrganizationService service = serviceFactory.CreateOrganizationService(null);
            Entity targer = (Entity)context.InputParameters["Target"];//传入单条数据
            
                if (context.Depth > 1) return;//深度防止死循环
                string subjectsid = "";
                var a = DateTime.Now.Year.ToString();
                var b= DateTime.Now.Month.ToString();
                if (DateTime.Now.Month < 10)
                {
                     b = "0" + b;
                }
                var c= DateTime.Now.Day.ToString();
                if (DateTime.Now.Day < 10)
                {
                    c = "0" + c;
                }
                string date = a + b + c;
                QueryExpression qe = new QueryExpression("new_zdbh");//存放编号的实体名称
                qe.Orders.Add(new OrderExpression() { AttributeName = "new_name", OrderType = OrderType.Descending });
                qe.ColumnSet.AddColumns("new_name");//存放编号的实体的字段名称
                EntityCollection result = service.RetrieveMultiple(qe);
                if (result.Entities.Count > 0)
                {
                    if (result.Entities[0].Contains("new_name"))
                    {
                        string sid = result.Entities[0]["new_name"].ToString();
                        DateTime dt = DateTime.ParseExact(sid.Substring(0, 8), "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
                        if (dt.Year < DateTime.Now.Year)
                        {
                            sid = "0";
                        }
                        else if (dt.Month < DateTime.Now.Month)
                        {
                            sid = "0";
                        }
                        else if (dt.Day < DateTime.Now.Day)
                        {
                            sid = "0";
                        }
                        if (sid.Length > 8)
                        {
                            sid = sid.Substring(8).ToString();
                        }
                        int ssid = Convert.ToInt32(sid) + 1;
                        int length = 4 - ssid.ToString().Length;
                        for (int i = 0; i < length; i++)
                        {
                            subjectsid += "0";
                        }
                        subjectsid = date += subjectsid += ssid;
                    Entity entity = new Entity("new_zdbh");//存放编号实体
                    entity.Id = result.Entities[0].Id;
                    entity["new_name"] = subjectsid;
                    service.Update(entity);
                    Entity entity3 = new Entity("new_followup");//跟进任务实体
                    entity3["new_accountid"] = targer["new_accountid"]; //查找类型
                    entity3["ownerid"] = targer["ownerid"];
                    entity3["modifiedby"] = targer["modifiedby"];
                    entity3["new_name"] = "FW" + subjectsid;
                    service.Create(entity3);
                }
                   
            }
                else
                {
                    subjectsid = "0001";
                    Entity entity1 = new Entity("new_zdbh");//存放编号实体
                    entity1["new_name"] = date+subjectsid;
                    service.Create(entity1);
                    Entity entity2 = new Entity("new_followup");//跟进任务实体
                    entity2["new_accountid"] = targer["new_accountid"]; //查找类型
                    entity2["ownerid"] = targer["ownerid"];
                    entity2["modifiedby"] = targer["modifiedby"];
                    entity2["new_name"] = "FW" + date + subjectsid;
                    service.Create(entity2);
            }
            

        }
    }
}

3.注册插件的时候:事件:选择Create、实体选择:销售线索 、选择Post-operation

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值