You are here:
Populate the Options Programmatically (Managed Package)
For the managed package runtime, use Apex to populate options for a Select element.
This information is for Omnistudio for Managed Packages. For Omnistudio on standard runtime, see Omnistudio Help.
- Create a class and method that returns a map of options translated according to the language code in effect when the method is called. See the sample code below.
-
Add a Select element to your Omniscript, configured as follows:
- Option Source: Custom
- Source: The class and method that return options
Note For dependent Select elements that are populated programmatically (custom), configure a controlling field by specifying the name of an Omniscript element that contains the value that determines the options in the dependent Select element. (For example, a list of cities might depend on a state specified in another element.) For the dependent element, specify its CONTROLLING FIELD settings as follows:- Option Source: Custom
- Source: Omit
- Element: The Omniscript element containing the value that determines how this dependent element is populated.
The Apex method that returns the values that populate the Select element must contain logic that uses the value of the controlling element and the language code to determine what values to return to populate the dependent element.
global class PicklistPopulation implements VlocityOpenInterface { public Boolean invokeMethod(String methodName, Map<String, Object> input, Map<String, Object> outMap, Map<String, Object> options) { try { if (methodName.equals('PopulatePicklist')) { PopulatePicklist(input, outMap, options); } else if (methodName.equals('PopulateDependentPicklist')) { PopulateDependentPicklist(input, outMap, options); } } catch (Exception e) { System.debug(LoggingLevel.ERROR, 'Exception: ' + e.getMessage() + ' ' + e.getStackTraceString()); } return true; } // Get All Contacts Associated with the ContextId Account where the Omniscript is Launched public void PopulatePicklist(Map<String, Object> input, Map<String, Object> outMap, Map<String, Object> options) { List<Map<String,String>> plOptions = new List<Map<String, String>>(); String lang = (String)input.get('LanguageCode'); for(Integer i=0; i<2; i++) { Map<String, String> tempMap = new Map<String, String>(); tempMap.put('name', 'test'+ String.valueOf(i)); // Language Independent tempMap.put('value', 'testV'+ String.valueOf(i)); // Displayed in Picklist UI if(lang == 'zh_CN') tempMap.put('value', 'testV' + String.valueOf(i) + ' 中文'); plOptions.add(tempMap); } outMap.put('options',plOptions); } // Populate a Dependent Picklist based on the Contacts ReportsToId Field Selected in the Other Field this field depends on public void PopulateDependentPicklist(Map<String, Object> input, Map<String, Object> outMap, Map<String, Object> options) { // Map of List where the Key is the Potential Values in the Other Picklist Map<String, List<Map<String, String>>> dependency = new Map<String, List<Map<String, String>>>(); String lang = (String)input.get('LanguageCode'); String elementName = (String)input.get('controllingElement'); // itself Logger.debug('hello1 ' + lang); Logger.debug('hello2 ' + elementName); List<String> controlValList = new List<String>(); controlValList.add('Licensing & Permitting'); controlValList.add('Contract'); for(Integer i=0; i<2; i++) { List<Map<String, String>> optionList = new List<Map<String, String>>(); for(Integer j=0; j<2; j++) { Map<String, String> tempMap = new Map<String, String>(); tempMap.put('name', controlValList[i] + 'Child' + String.valueOf(j)); // Language Independent tempMap.put('value', controlValList[i] + 'ChildV' + String.valueOf(j)); // Displayed in Picklist UI if(lang == 'zh_CN') tempMap.put('value', controlValList[i] + 'ChildV' + String.valueOf(j) + ' 中文' ); optionList.add(tempMap); } dependency.put(controlValList[i], optionList); } outMap.put('dependency',dependency); } }

