查找字段的筛选-使用addCustomView

关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复231或者20161031可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong.me 。

我这里有个简单的需求,就是选择了查找字段的值以后对地区字段做筛选,查找字段查找的是客户实体,客户实体上面有个所属地区字段,显示了这个客户所属的省份,然后地区就只能选择这个省份下面的城市。
对查找字段做筛选大概有三个方法,方法一:
1. 这种从我了解到的CRM 2011开始就支持了。制作一个公共视图,然后在表单上设置查找字段的属性,将默认视图设置为制作的这个公共视图,并将视图选择器关闭,如下图所示。
 
这种方式比较简单,效果如下,只能选择这个公共视图中的记录,配置简单功能也就简单。
 
方法2. 这种筛选方法从2013开始才提供,主要是使用 addPreSearch和addCustomFilter  来做,优点是页面OnLoad的时候不会执行代码,而且所加的筛选条件会应用到所有视图,缺点是它只能使用查找的实体做筛选,不能用关联的实体做筛选,这个问题 Using Linked-Entity in addCustomFilter 也有提到这个限制。我的文章 微软Dynamics CRM 2013介绍系列之三十:筛选查找控件,so easy。有介绍,这里不赘述了。 
方法3. 这种方法从CRM 2011(或许更早)就开始提供了,主要是使用 addCustomView 来做,这是今天博文要讲解的。先看看SDK中的介绍 Xrm.Page.getControl(arg).addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, isDefault) ,参数含义我这里不解释了。首先是要构造一个fetchXml,使用高级查询最方便了,这里值得一提的是很多人构造的时候不加上状态等于可用这个条件,很多情况下应该加上的。
一般还需要调整列,我这里调整如下:
 
 配置好后当然要点击 结果 查看结果是不是对的,我这里如下,是对的:
查找字段的筛选 - 罗勇 - 微软MVP罗勇的博客
 
在这个结果页面点击F12按钮,打开Developer Tools这个工具,我这用的Chrome浏览器,再按Ctrl + F,输入 layoutxml 进行查找,查找到的第一个如下,刚好就是我要找的。
 
 
可以分别复制出来fetchxml如下:
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">
    <entity name="ly_district">
        <attribute name="ly_districtid" />
        <attribute name="ly_name" />
        <attribute name="ly_districtno" />
        <attribute name="ly_parentdistrict" />
        <order attribute="ly_name" descending="false" />
        <link-entity name="ly_district" from="ly_districtid" to="ly_parentdistrict" alias="bg">
            <link-entity name="account" from="ly_district" to="ly_districtid" alias="bh">
                <filter type="and">
                    <condition attribute="accountid" operator="eq" uiname="Adventure Works (示例)" uitype="account" value="{C223165A-3AA3-E511-80C7-000D3A807EC7}" />
                </filter>
            </link-entity>
        </link-entity>
    </entity>
</fetch>
 
复制出来的 LayoutXml 如下:
<grid name="resultset" object="10067" jump="ly_name" select="1" icon="1" preview="1">
    <row name="result" id="ly_districtid">
        <cell name="ly_name" width="125" />
        <cell name="ly_parentdistrict" width="125" />
        <cell name="ly_districtno" width="125" />
    </row>
</grid>
然后就可以写代码了,注意:
1. FetchXML中查找字段筛选条件中的 uiname='湖南省' 和 uitype='ly_district' 部分可以去掉,我就把这两个属性去掉。
2. LayoutXml 中的属性preview在写代码的时候请设置为0,这样选择框好看点。
3. 复制出来的fetchxml和layoutxml中双引号替换为单引号比较好。
4. 若只能从这个筛选出来的记录选择,请在addCustomView时设置为默认视图,并且禁用选择其他视图,如下图所示:还需要在addCustomView 后执行 setDefaultView 。
 
5. 一般窗体的onload事件和影响这个筛选条件的字段的onchange事件都要执行相同的代码。
 
最后我所用的关键代码如下,在窗体的OnLoad事件和查找字段的onchange事件中执行如下代码:

Xrm.Page.getControl("ly_district").addCustomView("{6753B0C9-0B8C-431B-8E96-44D1D4EF6A06}", "ly_district", '地区筛选视图', "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'><entity name='ly_district'><attribute name='ly_districtid'/><attribute name='ly_name'/><attribute name='ly_districtno'/><attribute name='ly_parentdistrict'/><order attribute='ly_name' descending='false'/><link-entity name='ly_district' from='ly_districtid' to='ly_parentdistrict' alias='bg'><link-entity name='account' from='ly_district' to='ly_districtid' alias='bh'><filter type='and'><condition attribute='accountid' operator='eq' value='" + (Xrm.Page.getAttribute("ly_lookup").getValue() ? Xrm.Page.getAttribute("ly_lookup").getValue()[0].id : "00000000-0000-0000-0000-000000000000") + "'/></filter></link-entity></link-entity></entity></fetch>", "<grid name='resultset' object='10067' jump='ly_name' select='1' icon='1' preview='0'><row name='result' id='ly_districtid'><cell name='ly_name' width='125'/><cell name='ly_parentdistrict' width='125'/><cell name='ly_districtno' width='125'/></row></grid>", true);
Xrm.Page.getControl("ly_district").setDefaultView("{6753B0C9-0B8C-431B-8E96-44D1D4EF6A06}");

效果如下,不能选择到其他视图,只能在限定的范围内选择。
 
 
 
原文地址:https://www.cnblogs.com/luoyong0201/p/Dynamics_365_Filter_Lookup_Field_Use_addCustomView.html