在CRM定制中常用的Javascript
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,這是根據name和id來分的:
?
| 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; ?{ { |
5.?????? 判斷是運行在CRM 3.0還是CRM4.0
CRM4.0 提供了一個新的global的方法,我們可以利用它來判斷當前運行的環境:
| if (typeof(GenerateAuthenticationHeader) == "undefined") { |
6.?????? 控制tab的顯示與隱藏
CRM4.0中的tab頁默認都是按tab1Tab…tabNTab來命名的,隱藏或更改其中的某個tab跟隱藏和顯示普通的對象相同,你只需要提供tab的名字即可。(通過view page source來查看生成的源代碼便可以得到,你也可以通過IE Developer Tools或FireBug輕松得到)
| if (crmForm.FormType = 1) ?{ |
7.?????? 用正則表達式格式化
| var originalPhoneNumber = "+49 (89) 12345678"; |
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'); ????? event.srcElement.readyState == "loaded") function onReady() // custom code goes here } |
2). 用jQuery來執行客戶端操作
| $("#name").parents("tr:first").hide(); $("*[RequiredLevel=2]").css("border","1px solid red"); |
?
轉載于:https://www.cnblogs.com/zlgcool/archive/2008/10/07/1305774.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的在CRM定制中常用的Javascript的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用XMLHTTP获取动态页生成的HTML
- 下一篇: jstl格式化日期