神秘的 CRM Lookup (I)


1. 大家都知道CRM 里面的Lookup 保存了相关实体的GUID,让我们深入的了解一下CRM Lookup。当我们在2个实体间建立关系的时候,CRM自动生成了一些attributes来保存相关实体的信息,虽然我们在CRM定制界面只能看到一个 attribute,也就是保存GUID的那个,其实CRM还创建了一些隐含的attributes来保存其他信息,来看一个例子:

crmForm.all.regardingobjectid.DataValue[0].id;  // The GUID of the lookup.
crmForm.all.regardingobjectid.DataValue[0].name;  // The text value of the lookup.
crmForm.all.regardingobjectid.DataValue[0].typename;  // The entity type name.


这也就是为什么我们可以引用除了GUID的其他相关信息,这些隐含的attributes可以通过导出实体的customisation.xml 来看到。
也许有人注意到了,在CRM里,所有user-owned entity(比如 account, contact, case, email etc) 都有一个系统attribute叫做 ownerbusinessunit,这也是个Lookup,但却是系统的Lookup。这个attribute用来记录记录所有者(the owner)所在BusinessUnit的GUID。CRM论坛里一个常见的问题就是怎样得到当前用户的BusinessUnit,最常用的方法是通过AJAX来得到,见我以前的Blog有提。其实我们可以利用ownerbusinessunit来做,方法是导出customisation.xml 进行修改然后导入,这样才可以把ownerbusinessunit加到窗体上。注意这种方法只能得到GUID,不能得到BU's Name,原因上面已经说了,因为是系统的Lookup,CRM没有创建Name这个attribute。到此为止这种方法是比AJAX来的简单,但如果要得到BU's Name就不如食用AJAX了。


2. 我们来看看regardingobjectid这个特殊的Lookup,当用户选择Regarding的时候,也许那个下拉菜单会有很多选项:Account, Contact, Opportunity, Lead.... ,如果我只想显示Account 和Contact,并且把默认值设定为Contact怎么办呢? 在onLoad() 里面写如下语句:

crmForm.all.regardingobjectid.setAttribute("lookuptypes""1,2");  //only show account and contact
crmForm.all.regardingobjectid.setAttribute("lookuptypeIcons""/_imgs/ico_16_1.gif :/_imgs/ico_16_2.gif");  //set the icons
crmForm.all.regardingobjectid.setAttribute("defaulttype""2");  //default to contact


除了使用setAttribute 方法,还可以使用CRM自己的方法:

crmForm.all.regardingobjectid.lookuptypes = "1,2";    
  crmForm.all.regardingobjectid.lookuptypeIcons 
= "/_imgs/ico_16_1.gif:/_imgs/ico_16_2.gif";
crmForm.all.regardingobjectid.defaulttype 
= "2";  



3. 如果你注意一下Lookup的地址URL,你会发现CRM是这样调用的:
/lookupsingle.aspx?class=ActivityRegarding&objecttypes=1,2,3,4&browse=0&ShowNewButton=1&ShowPropButton=1&DefaultType=0

lookupsingle.aspx有一些参数可以被我们所用(ISV, IFRAME):

Objecttypes 实体代码, e.g. Objecttypes = "1, 2" //show account and contact
      DefaultType 默认实体代码, e.g. DefaultType = "2" //default to contact
      Browse  布尔值,  0 = 显示"查找" 栏; 1 = 浏览模式, 隐藏 "查找" 栏.
      ShowNewButton 布尔值,  0 = 隐藏 "新建" 按钮; 1 = 显示 "新建" 按钮.
      ShowPropButton 布尔值,  0 =  隐藏 "属性" 按钮; 1 = 显示 "属性" 按钮.

在IFRAME或者ISV里,如果我们不想显示"新建" 按钮,那么可以把URL调用写成:
/lookupsingle.aspx?class=ActivityRegarding&objecttypes=1,2,3,4&browse=0&ShowNewButton=0&ShowPropButton=1&DefaultType=0

但是如果我想在CRM里面隐藏 "新建" 按钮怎么办呢?你不能直接使用 crmForm.all.regardingobjectid.ShowNewButton = 0; 但是可以在onLoad()里使用下面的方法:

/*
   显示/隐藏 "新建" 按钮
   bShow = 0 : 隐藏 "新建" 按钮
   bShow = 1 :  显示 "新建" 按钮
*/

function NewButton(bShow)
{
    
return function()
    
{
        crmForm.all.regardingobjectid.AddParam(
"ShowNewButton", bShow);
    }

}

crmForm.all.regardingobjectid.attachEvent(
"setadditionalparams",NewButton(0));





 

posted @ 2008-05-05 21:30  MicrosoftCRM  阅读(2814)  评论(1编辑  收藏  举报