• 插件实现对数据的增删改操作


    1.创建一个库类程序--》选择框架为4.6.2,右键属性设置签名(新建,不勾选使用密码保护密钥保护文件)--》引用

    将其他操作写在其他类中调用PluginBase接口重写其中的方法--》打开PluginRegistration注册工具,连接上dynamic365 ,进行插件注册--》详细步骤:https://www.cnblogs.com/luoyong0201/p/Dynamics_365_Write_Register_Plug-in.html

    添加

    public class Create : PluginBase
        {
            public override void DoExecute(IServiceProvider serviceProvider)
            {
                if (context.Depth > 1) return;//深度防止死循环
                Entity entitys = new Entity(targer.LogicalName);
                entitys["new_name"] = "YH";//单行文本类型
                entitys["new_client_name"] = "Hello Word";//单行文本类型
                entitys["new_client_id"] = 123456;//整形
                entitys["new_client_float"] = 54.8;//浮点类型
                entitys["new_client_sex"] = false;//两个选择
                entitys["new_client_addtime"] = DateTime.Parse("2019-1-1 12:30:12");//日期类型
                entitys["new_client_money"] = new Money(3000);//货币类型
                entitys["new_client_cardnumber"] = decimal.Parse("50000");//十进制类型
                entitys["new_client_summary"] = "全力以赴";//多行文本类型
                entitys["new_client_a"] = new OptionSetValue(100000000);//单项选项集
                entitys["new_client_select"] = new EntityReference(targer.LogicalName, Guid.Parse("596B44E4-AF24-EB11-956F-E03F49115DFE"));//查找类型
                 //多多选项集
                List<OptionSetValue> optionset_list = new List<OptionSetValue>();
                OptionSetValue setvalue_1 = new OptionSetValue(1);
                OptionSetValue setvalue_2 = new OptionSetValue(2);
                OptionSetValue setvalue_3 = new OptionSetValue(3);
                optionset_list.Add(setvalue_1);
                optionset_list.Add(setvalue_2);
                optionset_list.Add(setvalue_3);
                OptionSetValueCollection setvaluecollection = new OptionSetValueCollection(optionset_list);
                entitys["new_test_duoxuan"] = setvaluecollection;
                service.Create(entitys);
            }
        }

    修改

    public class Upd: PluginBase
        {
            public override void DoExecute(IServiceProvider serviceProvider)
            {
                if (context.Depth > 2) return;
                //修改(需要获取修改名和修改的相应字段)
                //查询字段
                QueryExpression q = new QueryExpression();
                q.EntityName =targer.LogicalName ;
                q.ColumnSet = new ColumnSet(true);
                q.Criteria.AddCondition("new_name", ConditionOperator.Equal, "A");
                EntityCollection e = service.RetrieveMultiple(q);
                Entity entitys = new Entity(targer.LogicalName);
                entitys["new_name"] = "YH1";
                entitys.Id = Guid.Parse("CFA1B445-4E29-EB11-956F-E03F49115DFE");
                entitys["new_client_name"] = e.Entities[0].Attributes["new_client_name"];
                entitys["new_client_id"] = e.Entities[0].Attributes["new_client_id"];
                entitys["new_client_float"] = e.Entities[0].Attributes["new_client_float"];
                entitys["new_client_sex"] = e.Entities[0].Attributes["new_client_sex"];
                entitys["new_client_addtime"] = e.Entities[0].Attributes["new_client_addtime"];
                entitys["new_client_money"] = e.Entities[0].Attributes["new_client_money"];
                entitys["new_client_cardnumber"] = e.Entities[0].Attributes["new_client_cardnumber"];
                entitys["new_client_summary"] = e.Entities[0].Attributes["new_client_summary"];
                entitys["new_client_a"] = (OptionSetValue)e.Entities[0].Attributes["new_client_a"];//单项选项集
                //获取查找类型的id
                Guid a = e.Entities[0].GetAttributeValue<EntityReference>("new_client_select").Id;
                entitys["new_client_select"] = new EntityReference(targer.LogicalName, a);
                //多多选项集
                OptionSetValueCollection setvaluecollectiona = (OptionSetValueCollection)targer.Attributes["new_test_duoxuan"];
                entitys["new_test_duoxuan"] = setvaluecollectiona;
                service.Update(entitys);
            }
        }

    删除

    public class Del : PluginBase
        {
            public override void DoExecute(IServiceProvider serviceProvider)
            {
                //删除当前信息(实体和实体id)  
                service.Delete(targer.LogicalName, targer.Id);//当前实体名和实体名id
     
            }
        }

     查找

     //多条查询
    QueryExpression q = new QueryExpression();
    q.EntityName =targer.LogicalName ;//实体名称
    q.ColumnSet = new ColumnSet(true);//查询列
    q.Criteria.AddCondition("new_name", ConditionOperator.Equal, "A");//查询条件
    EntityCollection e = service.RetrieveMultiple(q);//查询结果
    
    
    
    单条查询
    Entity e=service.Retrieve(实体名, Guid.Parse(targer.id), new ColumnSet(true)获取列);//获取到客户的数据
    
    
    
    fetch查询
    //查询对应实体信息,返回一个json字符串
    string fetchxml = $@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                        <entity name='tx_testentity_1'>
                        <attribute name='tx_testentity_1id' />
                        <attribute name='tx_name' />
                        <attribute name='createdon' />
                        <order attribute='tx_name' descending='false' />
                      </entity>
                    </fetch>";
    var fetch = new FetchExpression(fetchxml);
    EntityCollection response = sev.RetrieveMultiple(fetch);//获取实体信息

    单行查询:Entity e=service.Retrieve(实体名实体id, new ColumnSet(true)获取列);//获取到客户的数据

    throw new Exception("");--》抛异常

    entitys["new_client_a"] = (OptionSetValue)e.Entities[0].Attributes["new_client_a"];//选项集赋值方式

    Guid a = e.Entities[0].GetAttributeValue<EntityReference>("new_client_select").Id;//获取查找类型的id

    entitys["new_client_select"] = new EntityReference(targer.LogicalName, a);//查询类型赋值

     e.Attributes["mcs_amount"] =new Money(((Money)ec3.Entities[0].Attributes["mcs_wholesalesprice"]).Value * item.mcs_sptotalnum);//货币类型做计算,Money类型为自定义类型与基本类型无关系,((Money)ec3.Entities[0].Attributes["mcs_wholesalesprice"]).Value是将此类型转化为decimal十进制类型的

    获取操作之前的信息

    需要在注册时添加一个image,操作之前就在操作之前打沟,操作之后就在操作之后打沟,

     

     

     

     

     

     

     

     

     

     

    在代码中获取操作之前的数据

     pretarger = context.PreEntityImages["Image"];--》这个是获取更新之前的数据,image为上面name的名称

    targer为获取更新之后的数据,一般为混合使用。

    获取数据的方式为:pretarger.Attributes["new_record_clientname"]

    注意:如果new_record_clientname为空无论在之前还是在之后都将获取不到数据!

     

    这代码是在C个实体更新时间时要保证更新的时间要大于A实体的更新时间,且不能超过当前时间,下一次跟进时间要大于这次跟进的时间,如果下一次跟进时间为空,那么所填时间加3天为下一次更新时间!

     //深度

    if (context.Depth > 1) return;
                    pretarger = context.PreEntityImages["Image"];
                    //获取到当前的客户名称
                    EntityReference e = (EntityReference)pretarger.Attributes["new_record_clientname"];
                    Entity e1 = service.Retrieve(e.LogicalName, e.Id, new ColumnSet(true));//获取到客户的数据                                                                      
                    DateTime d = (DateTime)targer.Attributes["new_record_updatetime"];//当前用户输入的时间
                    if (d < ((DateTime)e1.Attributes["new_client_updatetime"]) || d > DateTime.Now)
                    {
                        //抛异常
                        throw new Exception("当前的时间不符合更新规则!");
                    }
                    else//进行判断当前的规则的判断
                    {
                        //new_record_next_updatetime字段为空,直接更新字段信息
                        if (!targer.Contains("new_record_next_updatetime")&&!pretarger.Contains("new_record_next_updatetime"))
                        {
                            //为空在填入时间的基础上加三天
                            Entity e2 = service.Retrieve(pretarger.LogicalName, pretarger.Id, new ColumnSet(true));
                            e2.Attributes["new_record_next_updatetime"] = d.AddDays(3);
                            service.Update(e2);
                            //更新客户实体数据
                            e1.Attributes["new_client_updatetime"] = d;
                            service.Update(e1);
                        }
                        else
                        {
                            //判断是targer不存在,还是pretarger不存在
                            if (!targer.Contains("new_record_next_updatetime")&& pretarger.Contains("new_record_next_updatetime"))
                            {
                                //不为空判断是否大于当前填入时间
                                if (DateTime.Parse(pretarger.Attributes["new_record_next_updatetime"].ToString()).AddHours(8) > d.AddHours(8))
                                {
                                    //更新客户实体数据
                                    e1.Attributes["new_client_updatetime"] = d;
                                    service.Update(e1);
                                }
                                else
                                {
                                    //抛异常
                                    throw new Exception("下次跟进时间,不能在当前跟进时间之前!");
                                }
                            }
                            else
                            {
                                //不为空判断是否大于当前填入时间
                                if (DateTime.Parse(targer.Attributes["new_record_next_updatetime"].ToString()).AddHours(8) > d.AddHours(8))
                                {
                                    //更新客户实体数据
                                    e1.Attributes["new_client_updatetime"] = d;
                                    service.Update(e1);
                                }
                                else
                                {
                                    //抛异常
                                    throw new Exception("下次跟进时间,不能在当前跟进时间之前!");
                                }
                            }
                        }
                    }

    注:插件目前只能做增删改的操作,有targer(操作后数据),pretarger(操作之前的数据),posttarger(操作之后的数据,只有插件为操作后执行才会拿到此条数据),所有的数据,无论在前还是在后如果有字段为空没有填写内容,在页面上无法获取到该字段。

     

    插件中多多选项集怎么赋值

  • 相关阅读:
    ReactNative 分享解决listView的一个郁闷BUG
    SDWebImage 图片下载缓存框架 常用方法及原理
    巧谈GCD
    Core Bluetooth下实现两个设备进行互联
    iOS开发Delegate,Notification,Block使用心得
    iOS开发之性能优化
    iOS开发之git学习
    iOS开发之自定义输入框(利用UITextField及UITextView)
    iOS开发之网络请求(基于AFNetworking的再封装)
    iOS开发之设置界面的抽取
  • 原文地址:https://www.cnblogs.com/LanHai12/p/15258011.html
Copyright © 2020-2023  润新知