CRM Client Programming Tips

 前段时间一直在做CRM4.0的相关开发,其中大多数都是在做Customization和工作流,其实CRM的开发本来大多都是Customization,而在做Customization时更多的我们通常又会选择用Javascript来实现一些逻辑,包括有关界面样式的更改,动态生成一些控件,调用Web Service,强制提交隐藏字段,客户端事件通知等等。这里简单的将一些常用的Javascript操作做一总结,当然,这只是一部分,大家一起慢慢增加。至于复杂一些的像动态生成控件/字段,这里先不提及。客户端操作Web Service已经在另外一篇文章中提及。

1.       Lookup的操作

Lookup类型在CRM里是个很常用而且有些略显怪异的类型。根据生成的HTML来看,其实它本身也就是一个Input和一个Image而已,至于它的玄机在别的blog里也有过很多的解释,这里不尽阐述,但需要明白的是其实一个lookup就类似于Array,里便包含了多个Item(Object).如此理解之后我们可以很轻易的给lookup赋初始值:

 

//Create an array to set as the DataValue for the lookup control.

   var lookupData = new Array();

//Create an Object add to the array.

   var lookupItem= new Object();

 

//Set the id, typename, and name properties to the object.

   lookupItem.id = '{EEEA32C6-7430-DD11-BBDE-00155D012229}';

   lookupItem.typename = 'systemuser';

   lookupItem.name = 'Tony Park';

 

// Add the object to the array.

   lookupData[0] = lookupItem;

 

// Set the value of the lookup field to the value of the array.

   crmForm.all.new_financeofficerid.DataValue = lookupData;

 

在获取lookup的值时,需要分清获取的是value还是text,这是根据nameid来分的:

 

var lookupItem = new Array;

 

// Get the lookup for the primarycontactid attribute on the account form.

lookupItem = crmForm.all.primarycontactid.DataValue;

 

// If there is data in the field, show it in a series of alerts.

if (lookupItem[0] != null)

{

    // Display the text value of the lookup.

    alert(lookupItem[0].name);

 

    // Display the entity type name.

    alert(lookupItem[0].typename);

 

    // Display the GUID of the lookup.

    alert(lookupItem[0].id);

 

    // Display the entity type code of the lookup. A value of 1 equals account, and a value of 2 equals contact.

    alert(lookupItem[0].type);

}

 

2.       给客户端控件添加事件

除了在定制的时候我们可以选择一个控件的onChange事件,我们仍然可以在onLoad事件里通过Javascript给控件添加自己的事件处理程序。例如一下代码片段给一个checkBox控件添加了onClick事件。

var clickEventHandler = function {}

clickEventHandler.prototype =

{

    click:function(checked)

    {

        var intCount = parseInt(crmForm.all.numberofdaysrequested.DataValue);

        if (intCount == null) intCount = 0;

 

        if (checked)

        {

            intCount += 1;

        }

        else

        {

            if (intCount > 0) intCount -= 1;

        }

 

        crmForm.all.numberofdaysrequested.DataValue = intCount;

    }

}

 

crmForm.all.new_otherreasons.disabled = true;

 

crmForm.all.new_monday.onclick = function()

{

     clickEventHandler.click(crmForm.all.new_monday.DataValue);

}

 

3.       隐藏字段的强制提交ForceSubmit

Form上没有的字段(但实体对象上有)或者隐藏的字段,默认情况下CRM是不会提交这个值,只有在设置了ForceSubmit=true之后才会提交。如一下语句强制将new_name属性设置并提交。

//Force submit the disabled field

crmForm.all.new_name.ForceSubmit = true;

 

//Set Name(Primary Attribute) to be "[ChildName]-[NumberOfDaysRequested] days";

crmForm.all.new_name = childName + " - " + intCount + " days";

一个字段可以设置的属性还包括:

·         {Field}.DataValue

·         {Field}.Disabled

·         {Field}.RequiredLevel

·         {Field}.IsDirty

还有两个公共方法可以调用:

·         {Field}.FireOnChange()

·         {Field}.SetFocus()

4.       Boolean的空值

CrmBoolean类型的值是可以包含null值的,所以很多时候我们直接将field.DataValue转换为bool是错误的。在转换之前需要先判断是否为null.

var value = crmForm.all.my_bool.DataValue;

//Default to true if no value is set
if (value == null) {
    value = true;
}

if (value == true)

 {
    //do something
}
else

{
    //do something else
}

5.       判断是运行在CRM 3.0还是CRM4.0

CRM4.0 提供了一个新的global的方法,我们可以利用它来判断当前运行的环境:

if (typeof(GenerateAuthenticationHeader) == "undefined") {
    alert("
CRM 3.0");
}
else {
    alert("CRM 4.0");
}

6.       控制tab的显示与隐藏

CRM4.0中的tab页默认都是按tab1Tab…tabNTab来命名的,隐藏或更改其中的某个tab跟隐藏和显示普通的对象相同,你只需要提供tab的名字即可。(通过view page source来查看生成的源代码便可以得到,你也可以通过IE Developer ToolsFireBug轻松得到)

if (crmForm.FormType = 1)

 {
    var value = crmForm.all.new_
FileType.DataValue;

    crmForm.all.tab1Tab.style.display = (value == "1") ? "none" : "";
   crmForm.all.tab1Tab.style.backgroundColor = 'red';
}

7.       用正则表达式格式化

var originalPhoneNumber = "+49 (89) 12345678";
var formattedPhoneNumber = originalPhoneNumber.replace(/[^0-9,+]/g, "-");
formattedPhoneNumber = formattedPhoneNumber.replace(/-+/g, "-");
alert(formattedPhoneNumber);

8.       隐藏表格的行

其实在CRM中,除了在crmForm中的对象我们可以用crmForm.all.对象名来简单获取外,其它的位于crmForm外的对象仍然可以像平时一样用document.getElementById来获取(其实crmForm内的也可以),并通过获取的对象来设置属性。如以下代码演示了如何获取一个行并将其隐藏:

//the field you want to hide

var field = crmForm.all.name;

 

//search the enclosing table row

while ((field.parentNode != null) && (field.tagName.toLowerCase() != "tr")) {

    field = field.parentNode;

}

 

//if we found a row, disable it

if (field.tagName.toLowerCase() == "tr") {

    field.style.display = "none";

}

9.       如上的操作其实也可以用现在流行的jQuery来操作,并且更为简单。在CRM中引用jQuery来操作客户端对象,你会发现其优雅的方式让你的开发工作更为舒服。

利用jQuery来做客户端开发,你只需要做两件事即可完成:

1). jQuery的类库置入CRM并引用进页面模型。将jQuery-1.2.6.js文件放于服务器的"ISV"Scripts目录下,并在onLoad事件内加入将js文件加入文件引用的代码:

var script = document.createElement('script');
script.language = 'javascript';
script.src = '/ISV/scripts/jquery-1.2.
6.js';
script.onreadystatechange = function()
{
   if (event.srcElement.readyState == "complete" ||

      event.srcElement.readyState == "loaded")
         onReady();
};
document.getElementsByTagName('head')[0].appendChild(script);

function onReady()
{

// custom code goes here

}

2). jQuery来执行客户端操作

$("#name").parents("tr:first").hide();

$("*[RequiredLevel=2]").css("border","1px solid red");

 

posted on 2008-10-07 19:42  Allan.  阅读(3884)  评论(10编辑  收藏  举报