AXForum  
Вернуться   AXForum > Microsoft Dynamics CRM > Dynamics CRM: Blogs
All
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 02.12.2010, 19:11   #1  
Blog bot is offline
Blog bot
Участник
 
25,475 / 846 (79) +++++++
Регистрация: 28.10.2006
ukcrm: CRM 2011 Dependent Option Set Example
Источник: http://blogs.msdn.com/b/ukcrm/archiv...t-example.aspx
==============

Bu2b…

With the introduction of Option Sets in CRM 2011 (formerly known as picklists), it is now possible to create common drop-down lists across multiple entities and forms. On a recent project I had a specific requirement to build a solution that used two related option sets. If I selected a particular country from the first drop-down, the region would automatically selected from the second drop-down.

That doesn’t sound too tricky, and indeed writing a small amount of JScript I was able to achieve this fairly quickly. The problem came about when I wanted to use these option sets multiple times on a single entity. In this example, I needed to track “Country of Birth”, “Country of Residence” and “Tax Residency” as well as the associated regions. Now I have three variations of the same JScript code, with the only difference being the CRM schema names of the various country and region fields.

This got me thinking. Could I build a JScript library that would allow me to reuse the dependent option set logic, without having to hard-code specific field names? A quick search through the CRM 2011 SDK revealed several major enhancements to the client-side programming model, and of specific interest to me were:
  1. The ability to access the form execution context. When you associate a JScript library function with a CRM form event (such as a field OnChange event), you can now pass the execution context as a parameter to your function. The execution context object has a method called getEventSource(), which returns a reference to the field that triggered the event. In this example, I no longer have to hard-code the name of the country field that triggered the OnChange event.
  2. The ability to pass additional parameters to a JScript library function. In this example I specify the name of the region field as an additional parameter, instead of using a hard-coded value in my code.
So what does my code look like? Below, I have a simple JScript function called attributeOnChange(), which takes the both execution context and the dependent attribute name as input parameters. After some some simple error handling, the function then calls a second function getDependentAttributeValue(), which actually does the work of selecting the correct region, based on which country was selected. I’ve chosen to use the names attribute and dependentAttribute instead of country and region, so I can reuse this code for any dependent option sets.

1: function attributeOnChange(context, dependentAttributeName) { 2: // Check the context object exists and that it has a method getEventSource() 3: if ((context != "undefined") && (context != null) && (context.getEventSource)) { 4: 5: // Get the attribute that triggered this event 6: var attribute = context.getEventSource(); 7: 8: // Check the attribute type is an OptionSet 9: if (attribute.getAttributeType() == "optionset") { 10: 11: // Check the dependent attribute name has been passed as a parameter, and that it is a string 12: if ((dependentAttributeName != "undefined") && (dependentAttributeName != null) && (typeof (dependentAttributeName) == "string")) { 13: 14: // Get the dependent attribute object from the CRM page 15: var dependentAttribute = Xrm.Page.data.entity.attributes.get(dependentAttributeName); 16: 17: // Check the dependent attribute object exists 18: if ((dependentAttribute != "undefined") && (dependentAttribute != null)) { 19: 20: // Check the dependent attribute type is an OptionSet 21: if (dependentAttribute.getAttributeType() == "optionset") { 22: 23: // Get the selected attribute OptionSet value 24: var attributeValue = attribute.getValue(); 25: 26: // Lookup the dependent attribute OptionSet value 27: var dependentAttributeValue = this.getDependentAttributeValue(attributeValue); 28: 29: // Update the dependent attribute OptionSet 30: dependentAttribute.setValue(dependentAttributeValue); 31: } 32: else { 33: alert("The second parameter of this function should be the name of an OptionSet attribute. The attribute \"" + dependentAttributeName + "\" is not an OptionSet."); 34: } 35: } 36: else { 37: alert("The second parameter of this function \"" + dependentAttributeName + "\" is not a valid attribute name."); 38: } 39: } 40: else { 41: alert("This function requires you to pass the name of the dependent attribute as the second parameter e.g. \"crm_attributename\"."); 42: } 43: } 44: else { 45: alert("This function should only be used with an OptionSet attribute. The attribute \"" + attribute.getName() + "\" is not an OptionSet."); 46: } 47: } 48: else { 49: alert("This function requires you to pass the execution context as the first parameter."); 50: } 51: }
So now I have the necessary option sets and JScript library to solve my original problem, all that’s left is to add the fields to a form, and wire-up the OnChange() event as shown.



As you can see, I have selected the “Pass execution context as first parameter” checkbox, and specified the name of the region field “srh_region”, not forgetting to put quotes around the name. If you want to try this out in your own CRM 2011 environment, I have exported my solution as an unmanaged package, which you may download here.

This posting is provided "AS IS" with no warranties, and confers no rights.

Laughing Boy Chestnuts Pre-School Chain Gang




Источник: http://blogs.msdn.com/b/ukcrm/archiv...t-example.aspx
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
 

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
ukcrm: CRM 2011 Videos On Channel 9 (Part 3) Blog bot Dynamics CRM: Blogs 0 09.11.2010 19:05
ukcrm: CRM 2011 Videos On Channel 9 (Part 4) Blog bot Dynamics CRM: Blogs 0 09.11.2010 19:04
ukcrm: CRM 2011 Videos On Channel 9 (Part 3) Blog bot Dynamics CRM: Blogs 0 09.11.2010 19:04
Все о Microsoft Dynamics CRM: Как установить Microsoft Dynamics CRM 2011 Beta Blog bot Dynamics CRM: Blogs 0 31.10.2010 15:08
CRM DE LA CREME! Configuring Microsoft Dynamics CRM 4.0 for Internet-facing deployment Blog bot Dynamics CRM: Blogs 0 18.08.2009 11:05

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 14:18.
Powered by vBulletin® v3.8.5. Перевод: zCarot
Контактная информация, Реклама.