首页 > 其他分享 >Dynamic CRM最常用的3种查询方式

Dynamic CRM最常用的3种查询方式

时间:2022-08-18 11:45:46浏览次数:67  
标签:return string Dynamic 查询 query new Entity CRM

在crm系统中 接口或者插件中 通常都会有查询的校验 实际上单查询的话 不用crm提供的方式也可以 直接用sql的方式去查,一般涉及到很多表的联动查询比如报表查询时,还是用sql更方便   一、QueryExpression查询方式 1.花挎号实例化方式,可以做多表联动查询

 1 private static Entity GetDeliveryOrderDetail(string deliveryOrderNo, string deliveryRowNo)
 2         {
 3             QueryExpression queryExp = new QueryExpression("foton_deliveryorderdetail")
 4             {
 5                 ColumnSet = new ColumnSet("foton_deliveryorderdetailid"),
 6                 NoLock = true,
 7                 Criteria = new FilterExpression(LogicalOperator.And)
 8                 {
 9                     Conditions =
10                     {
11                         new ConditionExpression("foton_rowno", ConditionOperator.Equal, deliveryRowNo)
12                     }
13                 },
14                 LinkEntities =
15                 {
16                     new LinkEntity("foton_deliveryorderdetail", "foton_deliveryorder", "foton_deliveryorderid", "foton_deliveryorderid", JoinOperator.Inner)
17                     {
18                         LinkCriteria = new FilterExpression(LogicalOperator.And)
19                         {
20                             Conditions =
21                             {
22                                 new ConditionExpression("foton_no", ConditionOperator.Equal, deliveryOrderNo),
23 new  ConditionExpression("crm_orderlinestatus",ConditionOperator.NotEqual,171060003),
24                             }
25                         }
26                     }
27                 }
28             };
29 
30             Entity deliveryOrderDetail = null;
31             try
32             {
33                 deliveryOrderDetail = OrgServiceUtil.Client.RetrieveMultiple(queryExp).Entities.FirstOrDefault();
34             }
35             catch (Exception e)
36             {
37                 return null;
38             }
39             return deliveryOrderDetail;
40         }

2.属性赋值方式,看起来更整洁一些,个人比较常用此方式

 1 private Entity checkAccountAmountIsExist(IOrganizationService service,Entity entity)
 2         {
 3             QueryExpression query = new QueryExpression(AccountAmount.new_amount);
 4             query.Criteria.AddCondition("new_account_amount", ConditionOperator.Equal, entity.GetAttributeValue<EntityReference>("new_account_amount").Id);
 5             query.Criteria.AddCondition("new_type", ConditionOperator.Equal, entity.GetAttributeValue<OptionSetValue>("new_type").Value);
 6             query.ColumnSet = new ColumnSet(true);
 7             query.NoLock = true;
 8             Entity checkEntity = service.RetrieveMultiple(query)?.Entities.FirstOrDefault();
 9             return checkEntity;
10         }

项目实战拓展:QueryExpression多选项集条件查询与多条件组合查询方式:

 1  public List<Entity> GetOldFundsAccountByBusinessId(string businessEntityName, Guid businessId)
 2         {
 3             QueryExpression query = new QueryExpression(FundsAccount.new_funds_account);
 4             switch (businessEntityName)
 5             {
 6                 case FundsAccount.new_order:
 7                     query.Criteria.AddCondition(FundsAccount.new_order_field, ConditionOperator.Equal, businessId);
 8                     break;
 9                 case FundsAccount.new_salesorder:
10                     query.Criteria.AddCondition(FundsAccount.new_partssalesorder_field, ConditionOperator.Equal, businessId);
11                     break;
12                 default:
13                     break;
14             }
15             int[] typeArray = { FundsAccountType.BALANCE.GetHashCode(), FundsAccountType.BEGINBALANCE.GetHashCode(), FundsAccountType.LC.GetHashCode(), FundsAccountType.LINECREDIT_REMOVE_OUT.GetHashCode(), FundsAccountType.LINECREDIT_REMOVE_INNER.GetHashCode() };
16             ConditionExpression condition = new ConditionExpression("new_type", ConditionOperator.In, typeArray);
17             FilterExpression filter = new FilterExpression();
18             filter.AddCondition(condition);
19             query.ColumnSet = new ColumnSet(true);
20             query.NoLock = true;
21             query.Criteria.AddFilter(filter);
22             return crmOrg.RetrieveMultiple(query)?.Entities.ToList();
23         }

 

二、FetchXML查询方式

FetchXML查询方式个人使用最多的场景是多表联动查询

1.简单单表查询

 1 private static Entity GetPurchasingInfoRecord(string infnr,Guid otdmaterialId, Guid providerId)
 2         {
 3             Entity entity = null;
 4             string getOtdmaterialByCodeFetchXML = @"
 5                 <fetch mapping='logical'>
 6                     <entity name='foton_otdpurchasinginforecord'> 
 7                         <all-attributes /> 
 8                         <filter type='and'>
 9                              <condition attribute='foton_infnr' operator='eq' value='{0}' />
10                              <condition attribute='foton_otdmaterialid' operator='eq' value='{1}' />
11                              <condition attribute='foton_providerid' operator='eq' value='{2}' />
12                         </filter>
13                     </entity> 
14                 </fetch>";
15             string fetchXml = string.Format(getOtdmaterialByCodeFetchXML, infnr, otdmaterialId.ToString().Replace("{","").Replace("}", ""), providerId.ToString().Replace("{", "").Replace("}", ""));
16             EntityCollection entities = ExecFetchXML(fetchXml);
17             if (entities.Entities.Count > 0)
18             {
19                 entity = entities[0];
20             }
21             return entity;
22         }
23 
24      /// <summary>
25         /// 根据FetchXML查询对应的Entity列表
26         /// </summary>
27         /// <param name="fetchXml">fetchxml查询语句</param>
28         /// <create>ading</create>
29         /// <returns>结果集</returns>
30         public static EntityCollection ExecFetchXML(string fetchXml)
31         {
32             try
33             {
34                 return crmOrg.RetrieveMultiple(new FetchExpression(fetchXml));
35             }
36             catch (Exception ex)
37             {
38                 if (ex is MessageSecurityException)
39                 {
40                     crmOrg = OrgServiceUtil.Client;
41                     return crmOrg.RetrieveMultiple(new FetchExpression(fetchXml));
42                 }
43                 throw ex;
44             }
45         }

2.多表查询

(简单的插叙你语句可以通过在线sql语句转换成fetchxml的工具转化, 也可以通过相关的实体窗体右上方导航按钮中有个高级查询 自定义一个查询视图然后导出fetchxml,较为复杂的语句建议自己写)
 1         /// <summary>
 2         /// 税率查询FetchXML
 3         /// </summary>
 4         private string RateQueryFetchXML = @"
 5             <fetch mapping='logical'>
 6               <entity name = 'new_product_fourth'>
 7                 <link-entity name='new_return_analysis_detail' from='new_product_fourthid' to='new_product_fourthid' alias='analydetail' link-type='inner'>
 8                     <filter>
 9                         <condition attribute = 'new_return_analysis_detailid' operator='eq' value='{0}' />
10                     </filter>
11                 </link-entity>
12                 <link-entity name='new_brand' from='new_brandid' to='new_brandid' link-type='outer' alias='brand'>
13                   <attribute name='new_taxrate' />
14                 </link-entity>
15               </entity>
16             </fetch>";
17 要取出连接表中的new_taxrate字段的值 需要注意要用AliasedValue先承接连接表的查询结果
18 private decimal GetRateByDetail(IOrganizationService service, Entity detailEntity)
19         {
20             decimal rate = 0.00m;
21             Entity rateEntity = service.RetrieveMultiple(new FetchExpression(string.Format(RateQueryFetchXML, detailEntity.Id)))?.Entities.FirstOrDefault();
22             AliasedValue rateAliasedValue = rateEntity.GetAttributeValue<AliasedValue>("brand.new_taxrate");
23             rate = rateEntity == null ? 0.00m : (rateAliasedValue == null ? 0.00m: Convert.ToDecimal(rateAliasedValue.Value));
24             return rate;
25         }
26 brand.new_taxrate取值,实际上也可以通过设置连接表要查的字段一个别名的方式,rateEntity.GetAttributeValue<AliasedValue>("taxrate");:
27 <attribute name='new_taxrate' alias='taxrate' />    

拓展:

1)FetchXML中的关联表查询时,过滤条件为link表的字段是,既可以写在link表里,也可以放到最外层统一过滤 下面为js中拼接的fetchXML语句
 1 function getInvoiceDetailByOrderIdFetchXML(value) {
 2     var fetchXML = '<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">'
 3         + '<entity name="new_relation">'
 4         + '<all-attributes />'
 5         + '<link-entity name="crm_vehicle" from="crm_vehicleid" to="crm_vin" link-type="outer" />'
 6         + '<link-entity name="crm_ordermanage" from="crm_ordermanageid" to="crm_ordermanage" link-type="outer" />'
 7         + '<filter type="and">'
 8         + '<condition entityname="crm_ordermanage" attribute="crm_ordermanageid"  value="' + value + '" operator="eq" />'
 9         + '<condition entityname="crm_vehicle" attribute="crm_invoiceapplicationdetail"  operator="not-null"/>'
10         + '<condition attribute="crm_orderlinestatus"  value="171060003" operator="ne" />'
11         + '</filter>'
12         + '</entity >'
13         + '</fetch >';
14     return fetchXML;
15 }

 

三、QueryByAttribute方式:

QueryByAttribute queryo = new QueryByAttribute("opportunityproduct");
queryo.AddAttributeValue("foton_otddemandorerid", opportunityid.Id);
queryo.ColumnSet = new ColumnSet(true);
//queryinvoiceaccount.AddAttributeValue("statecode", 0);//属性名
EntityCollection ecinvoiceo = service.RetrieveMultiple(queryo);

 

 

 

标签:return,string,Dynamic,查询,query,new,Entity,CRM
From: https://www.cnblogs.com/adingfirstlove/p/16598133.html

相关文章

  • 简述一条查询语句的执行过程
    大体来说,MySQL可以分为Server层和存储引擎层两部分。Server层包括连接器、查询缓存、分析器、优化器、执行器等存储引擎层负责数据的存储和提取。 一个查询语句......
  • Dynamic CRM插件程序集中引入第三方dll合并打包
    有时候在插件程序集不可避免的需要使用第三方的dlll但crm插件平台注册时只能注册一个dll即项目自身的dll第三方的dll无法正常在注册后使用查找官方资料找到如下方法......
  • 16 Django-extra查询
    知识点补充:如何只拿出,dt字段中的年月字段?知识点补充:extra有些时候复杂的sql语句,Django无法去对应。extra(select=None,where=None,params=None,tables=None,ord......
  • sql语句查询
    xxxImpl.java 继承IDao<xxx>以qqUser为例publicclassqqDaoImplimplementsIDao<qqUser>{@Overridepublicintinsert(qqUserqqUser)throwsSQLExcep......
  • 复杂条件查询功能
    复杂条件查询功能分析       总记录数统计的代码实现UserDao接口:/***查询总记录数*@return*@paramcondition*/i......
  • SAP BW怎么查询传输请求号内容与所有请求号存放的表
    一:打开gui,输入事务码-SE11     在数据库表输入:E070(这个表就是存放所有传输请求号的)---点击显示  二:进入后点击下图红框处,查看表的数据 三:输入请求号,点......
  • 使用Fiddler劫持网络资源为前端开发助力(示例:Dynamic CRM 表单开发 也能热更新? )
    背景:使用过vue开发的童鞋应该都知道,在开发vue项目的过程中,有个叫"热更新"的功能特别爽,在传统html开发到初次接触vue时,才发现原来前端开发可以这么香。热更新的表现形......
  • 讲一讲加密数据如何进行模糊查询
    在上一篇讲一讲数据安全,如何有效预防脱库中我们提到了加密后的数据对模糊查询不是很友好,本篇就针对加密数据模糊查询这个问题来展开讲一讲实现的思路。为了数据安全我们......
  • mysql/表sql语句补充/关键字查询
    操作表的SQL语句补充alter1.修改表名 altertable表名rename新表名;2.新增字段 altertableadd字段名字段类型(数字)约束条件3.新增指定字段排在第一位 ......
  • MySQL查询关键数据方法
    MySQL查询关键数据方法操作表的SQL语句补充1.修改表名 altertable表名reame新表名;2.新增字段名 altertable表名add字段名字段类型(数字)约束条件;#默认队尾......