Dynamics CRM 365 - 零基础入门学习后端实现实体ID自增插件(代码)

Dynamics CRM 365 - 零基础入门学习后端实现实体ID自增插件(代码)

Dynamics CRM 365 中没有id自增的选项,所以我们只能时自己去写一个插件来实现这个功能了,代码如下

public void Execute(IServiceProvider serviceProvider)
        {
            #region 相关服务的初始化
            //访问插件执行上下文
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            //访问组织服务
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            OrganizationServiceContext orgContext = new OrganizationServiceContext(service);
            #endregion
            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                Entity entity = (Entity)context.InputParameters["Target"];
                try
                {
                    #region 自动生成编号
                    string subjectsid = "";
                    //获取当前年份
                    string year = DateTime.Now.Year.ToString();
                    //查询记录
                    QueryExpression qe = new QueryExpression(entity.LogicalName);
                    //降序排序
                    qe.Orders.Add(new OrderExpression() { AttributeName = "iws_projectno", OrderType = OrderType.Descending });
                    qe.ColumnSet.AddColumns("iws_projectno");
                    EntityCollection result = service.RetrieveMultiple(qe);
                    //判断是否有数据
                    if (result.Entities.Count > 0)
                    {
                        //如果有取最大一条
                        if (result.Entities[0].Contains("iws_projectno"))
                        {
                            string sid = result.Entities[0]["iws_projectno"].ToString();
                            //取得加1后编号
                            int ssid = Convert.ToInt32(sid.Substring(2)) + 1;
                            //判断加1后的编号位数,不足4位加0
                            int length = 3 - ssid.ToString().Length; ;
                            for (int i = 0; i < length; i++)
                            {
                                subjectsid += "0";
                            }
                            //拼接
                            subjectsid += ssid;
                        }
                    }
                    else
                    {
                        subjectsid = "001";
                    }
                    #endregion
					//赋值
                    entity["iws_projectno"] = year + subjectsid;
                }
                catch (Exception ex)
                {
                    throw new InvalidPluginExecutionException(ex.Message + ex.StackTrace);
                }
            }
        }

其他文章:

插件的注册.

插件的调试.

到此就结束啦,快去练习一下吧!欢迎大佬和小Monkey沟通。
在这里插入图片描述

感谢大佬指正 小Monkey
如果你觉得有用的话,就留个赞吧!蟹蟹

你可能感兴趣的:(Dynamics,CRM,crm,id,c#)