Microsoft Dynamics CRM 2011 & Business Productivity - Jim Wang's blog [MVP]

Welcome to my blog: http://mscrm.cn [Chinese] & http://jianwang.blogspot.com [English]

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  23 随笔 :: 52 文章 :: 103 评论 :: 5 引用

 
5.       现在我们来看一个普遍应用的例子:我只想在regardingobjectidLookup里列出OpenCase

 

a.       CRM 3.0

 

 

/* set the regarding to open case : Form.onLoad() */
crmForm.all.regardingobjectid.lookuptypes 
= "112";
crmForm.all.regardingobjectid.lookuptypeIcons 
= "/_imgs/ico_16_112.gif";
 
/* only show the active cases : Form.onLoad() */
if (crmForm.ObjectId != null)
{
    crmForm.all.regardingobjectid.lookupbrowse 
= 1;
    crmForm.all.regardingobjectid.additionalparams 
= "fetchXml="
    
+ "<fetch mapping='logical'><entity name='incident'><all-attributes /><filter>"
    
+ "<condition attribute='statecode' operator='eq' value='0' />"
    
+ "</filter></entity></fetch>";
}


 

b.      CRM 4.0

 

4.0里,上述功能(additionalparams)不再可以使用,但是我们仍然可以使用上一篇文章提到的技术:打开Case Lookup View, 点击'添加新的搜索栏'( Add Find Column),  选择statecode,保存并发布。然后在Form.onLoad()里面输入如下代码:

 

/* set the regarding to open case : Form.onLoad() */
crmForm.all.regardingobjectid.lookuptypes 
= "112";
crmForm.all.regardingobjectid.lookuptypeIcons 
= "/_imgs/ico_16_112.gif";
crmForm.all.regardingobjectid.additionalparams 
= 'search=Active';

 

大家已经看到了,像这种简单的Filter功能用这个技术就可以实现。但是如果想实现复杂一点的呢?
比如我们现在的要求变成了:只列出当前用户的
Open Case!


像这样复杂一些的
Filter功能我们一般先用高级查找(Advanced Find)Build FetchXml,然后再进行下一步。具体做法在Ronald Lemmen Blog里有讲:就是在搜索结果的IE地址栏里输入如下代码:javascript:prompt("", resultRender.FetchXml.value);


这样会弹出一个
JavaScript.pormpt窗口,具体到我们的这个例子弹出的FetchXml语句是:


<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false"><entity name="incident"><attribute name="title"/><attribute name="ticketnumber"/><attribute name="createdon"/><attribute name="incidentid"/><order attribute="title" descending="false"/><filter type="and"><condition attribute="statecode" operator="eq" value="0"/><condition attribute="ownerid" operator="eq-userid"/></filter></entity></fetch>


下面介绍的在
CRM 4.0里面的FilteredView功能最先是Adi Katz开发的,然后网友George进行了一些改进。我拿改进好的版本给大家演示:(注:下面的定制方法属于非常Unsupported的那种Customizationidea是重载了code-behind 的程序,加一个Filter进去。CRM 4.0 目前只有一种基于Filtered Lookup Supported Customization,是Michael Höhne开发的收费插件


你需要修改这个文件:
CRMWeb\\_controls\\lookup\\lookupsingle.aspx,加入下面的代码:


<script runat="server">
protected override void OnLoad(EventArgs e)
{
    
base.OnLoad(e);
    crmGrid.PreRender 
+= new EventHandler(crmGrid_PreRender);
}

void crmGrid_PreRender(object sender, EventArgs e)
{
    
if (crmGrid.Parameters["search"!= null && crmGrid.Parameters["search"].StartsWith("<fetch")) 
    

        crmGrid.Parameters.Add(
"fetchxml", crmGrid.Parameters["search"]);  
        crmGrid.Parameters.Remove(
"searchvalue");  
        
this._showNewButton = false
    }
 
}

</script>



然后在Form.onLoad()里面写入下面的代码(和 CRM3.0相似),注意fetchStr的值就是我们在高级搜索里得到的值:


/* set the regarding to open case which owned by current user : Form.onLoad() */
crmForm.all.regardingobjectid.lookuptypes 
= "112";
crmForm.all.regardingobjectid.lookuptypeIcons 
= "/_imgs/ico_16_112.gif";
 
var fetchStr = "<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false"><entity name="incident"><attribute name="title"/><attribute name="ticketnumber"/><attribute name="createdon"/><attribute name="incidentid"/><order attribute="title" descending="false"/><filter type="and"><condition attribute="statecode" operator="eq" value="0"/><condition attribute="ownerid" operator="eq-userid"/></filter></entity></fetch>";
crmForm.all.regardingobjectid.lookupbrowse 
= 1;
crmForm.all.regardingobjectid.additionalparams 
= "search=" + fetchStr;



到目前为止,这个方法已经应用了3个月,没有什么状况。所以介绍给大家 :)



posted on 2008-05-19 19:26 Jim Wang 阅读(1199) 评论(8)  编辑 收藏

评论

#1楼 2008-06-12 10:37 jonas[未注册用户]
可以实现一个表单针对同一个实体的 多个lookup么?比如产品lookup,建立n/1关系后只有一个。
能不能通过某个方法,增加多个呢?
 回复 引用   

#2楼[楼主] 2008-06-22 20:41 Jim Wang      
可以呀,比如你想在Account.Form上新增加引用2个Contacts
那么可以在Account.Relationship里添加2个新的和Contact的N:1 Relationship,注意名字不要想同。然后就可以在Account.Form上引用了。
 回复 引用 查看   

#3楼 2008-07-16 15:48 1w23e[未注册用户]
那为什么我在建新的N:1的relationship时,主要实体那个下拉列表里面看不到,已经关联过的实体呢?比如已经有一个account和contact的,列表里面就看不到contact了啊
 回复 引用   

#4楼[楼主] 2008-07-17 00:16 Jim Wang      
你用的是3.0吧?4.0可以的。
 回复 引用 查看   

#5楼 2008-11-10 15:26 dlmiszy[未注册用户]
按照楼主的方法,确实表面上实现了按条件选择,可是如果在输入画面
按下crtl+shift+F 打开输入助手窗口的话,所有的数据又都显示出来了,
过滤的目的就达不到了,请问楼主有什么好办法解决么?
 回复 引用   

#6楼 2009-02-10 16:04 GavinGan      
“在4.0里,上述功能(additionalparams)不再可以使用”但你在4.0里面又用了additionalparams,什么意思?
 回复 引用 查看   

#7楼 2009-10-10 14:01 tuberosa      
请问,代码中的lookuptypes和lookuptypeIcons 属性是在哪里可以查到,我在sdk中没找到啊。
 回复 引用 查看   

#8楼 2009-10-27 14:37 Watt[未注册用户]
请问, 如何在Entity里新增一个像Task里的Regarding 这样的属性。
例如说我有个审核Entity. 有属性
1.审核结果Result (Picklist: pass, not pass)
2.相关资料Regarding(lookup, N: 1 , 连接的Entity不只一个 可能是Contact, Account, Product.....就像是Task里的Regarding一样。)
 回复 引用