Java API:Enumerated Fields

From AgileApps Support Wiki

About Enumerated Fields

There are multiple types of enumerated fields. The type is specified using FieldMetaDataBean.setType(String type). These are the possible values. (They are passed within quotes, as strings):

  • PICK_LIST
  • GLOBAL_PICK_LIST
  • DEPENDENT_PICK_LIST
  • MULTI_PICK_LIST
  • RADIO_BUTTON
  • MULTI_CHECK_BOX

The standard field methods are used to work with enumerated fields:

  • Functions.addFieldMetaData(FieldMetaDataBean fmdb);
  • Functions.updateFieldMetaData(FieldMetaDataBean fmdb);
  • Functions.deleteFieldMetaData(FieldMetaDataBean fmdb);
Considerations
  • Add/update/delete is not supported for global picklists. All other enumeration types are supported.

Retrieving Field Metadata

import com.platform.beans.EnumerationDetailsBean.*;

//Retrieving the FieldMetaData
FieldMetaDataBean fmdb = getFieldMetaData("myObject", "picklistField");
StringBuilder sb = new StringBuilder();

//Get the field name
String fieldName = fmdb.getTableColumn();

//Get the field type
// (PICK_LIST, DEPENDENT_PICK_LIST, RADIO_BUTTON, MULTI_CHECK_BOX, GLOBAL_PICKLIST)
String fieldType = fmdb.getType();

//String object Id
String objectId = fmdb.getObjectId();

//Get the field label
String fieldLabel = fmdb.getTitle();

// Echo the attributes
sb.append("Field Name -> " + fieldName + "\n") ;
sb.append("Field Type -> " + fieldType + "\n");
sb.append("Object Id -> " + objectId + "\n");
sb.append("Field Label -> " + fieldLabel + "\n");

//Enumeration Details
EnumerationDetailsBean  enumerationDetails = fmdb.getEnumerationDetails();

//Internal Id of the Enumerated Type field
String name =  enumerationDetails.getName();
sb.append("Internal Picklist Id -> " + name + "\n");

//Sort flag
Boolean sortFlag = enumerationDetails.getSortFlag();
sb.append("Sort flag -> " + sortFlag + "\n");

//Show first value as default flag
Boolean showFirstValAsDefaultFlag = enumerationDetails.getShowFirstValueAsDefault();

//Get the list of Enumeration Items
List<EnumerationItemBean> enumerationItems = enumerationDetails.getEnumerationItems();

//Extracting the Item details
for(EnumerationItemBean item : enumerationItems)
{
    //Enumeration Item Id
    String id = item.getId();	
    sb.append("Enumeration Item Id -> " + id + "\n");

    //Enumeration Item label
    String enumeratedItemLabel = item.getPicklistLabel();
    sb.append("Enumeration Item Label -> " + enumeratedItemLabel + "\n");

    //Enumeration Item Value
    String enumeratedItemValue = item.getPicklistValue();	
    sb.append("Enumeration Item Value -> " + enumeratedItemValue + "\n");
	
    //Text color of the Enumeration Item Label
    String textColor = item.getTextColor();
    sb.append("Enuemration Label text color -> " + textColor + "\n");

    //Text background color of the Enumeration Item label
    String textBackgroundColor = item.getTextBgColor();	
    sb.append("Enumeration Label text background color -> " + textBackgroundColor + "\n");

    //Show only image flag
    Boolean showOnlyImageFlag = item.getShowOnlyImage();	
    sb.append("Show only image flag -> " + showOnlyImageFlag + "\n");

    //Indicates if the item is an option group laabel
    Boolean flagIsOptGroup = item.getFlagIsOptgroup();	
    sb.append("Is options group flag -> " + flagIsOptGroup + "\n");
		
    //Order of the Item
    Integer itemOrder = item.getItemOrder();	
    sb.append("Item order -> " + itemOrder + "\n");
}

Logger.info(sb.toString(), "Enum");

Adding a Picklist Field

To create an enumerated field:

  1. Add the new field to the object using Functions.addFieldMetaData().
  2. Add the list of items, using the Functions.updateFieldMetaData().
//Instantiate FieldMetaDataBean
FieldMetaDataBean fmdb = new FieldMetaDataBean();

//Setting the objectId
fmdb.setObjectId("myObject");

//Setting the field name
fmdb.setTableColumn("priority_picklist");

//Setting the field type
fmdb.setType("PICK_LIST");

//Setting the field label
fmdb.setTitle("Priorities");

//Setting enumeration details
EnumerationDetailsBean enumDetails = new EnumerationDetailsBean();
String[] items = {"priority", "urgent", "rush", "second-day delivery"};  
List<String> itemList = Arrays.asList(items);
enumDetails.setEnumerationItems(itemList);

//Sorting the enumerated values 
enumDetails.setSortFlag(true);

fmdb.setEnumerationDetails(enumDetails);

//On successful addition of the field, the field name name is returned
String fieldName = Functions.addFieldMetaData(fmdb);

Logger.info("Added field " + fieldName, "Enum");

Updating a Picklist

To update a specific item in an enumeration, it's ID must be specified.

To update the entire field:

  • The new list of items replaces the old one. All existing enumeration items must be included in the list, or they are not preserved.
  • For dependent picklists, all existing items plus the ParentToChildMapping beans must be provided.
import com.platform.beans.EnumerationDetailsBean.*;

//Instantiate FieldMetaDataBean
FieldMetaDataBean fmdb = getFieldMetaData("myObject", "picklistField");

//Setting enumeration details
EnumerationDetailsBean enumDetails = new EnumerationDetailsBean();
String[] items = {"priority", "rush", "second-day delivery"};  
List<String> itemList = Arrays.asList(items);
enumDetails.setEnumerationItems(itemList);

//Sorting the enuerated values 
enumDetails.setSortFlag(true);

//Creating a list of Enumeration items
List<EnumerationItemBean> listEnumItems = new ArrayList<EnumerationItemBean>();

//Setting up an enumeration item
EnumerationItemBean enumItemBean = new EnumerationItemBean();

//Specify the ID of the enumeration item to be update, 
//where ENUMERATION_ITEM_ID is retrieved using getFieldMetaData()
//enumItemBean.setId(ENUMERATION_ITEM_ID)

//setting the label of the Enumerated item
enumItemBean.setPicklistLabel("Label 1");

//setting the value of the Enumerated item
enumItemBean.setPicklistValue("value1");

//Setting the text color of the Enumerated item label
enumItemBean.setTextColor("#000000");

//Setting the Back ground color of the Enumerated item label
enumItemBean.setTextBgColor("#FFFFFF");

//Adding enumeration item to the list. In a similar way we can add more items
listEnumItems.add(enumItemBean);

//Setting the list of Enumeration Items in the EnumerationDetailsBean
enumDetails.setEnumerationItems(listEnumItems);

//Setting in the Enumeration details in the FieldMetaDataBean
fmdb.setEnumerationDetails(enumDetails);

//On successful addition of the field, the field name is returned
String fieldName = Functions.updateFieldMetaData(fmdb);

Define Mapping for a Dependent Picklist

A Dependent Picklist is one where the list of choices depends on a selection made in a parent field (another field of type enumerated). This sample code maps each choice in a parent picklist to the list of values in the dependent picklist.

import com.platform.beans.EnumerationDetailsBean.*;

//Retrieve the  metadata for the dependent picklist 
FieldMetaDataBean dependentPicklistMeta = getFieldMetaData("myObject", "dependentField");

String parentFieldName = dependentPicklistMeta.getParentPicklistTableColumn();
FieldMetaDataBean parentEnumeratedFieldMeta = getFieldMetaData("myObject", parentFieldName);
EnumerationDetailsBean parentEnumerationDetails = 
    parentEnumeratedFieldMeta.getEnumerationDetails();
List<EnumerationItemBean> parentEnumerationItemList = 
    parentEnumerationDetails.getEnumerationItems();
Map<String,String> parentItemValToItemIdMap = new HashMap<String,String>();

for(EnumerationItemBean item : parentEnumerationItemList)
{
    String itemValue = item.getPicklistValue();
    String itemLabel = item.getPicklistLabel();
    String itemId = item.getId();
    parentItemValToItemIdMap.put(itemValue,itemId);
}

EnumerationDetailsBean dependentEnumerationDetails = 
    dependentPicklistMeta.getEnumerationDetails();
List<EnumerationItemBean> dependentEnumerationItemList =
    dependentEnumerationDetails.getEnumerationItems();
Map<String,String> dependentItemValToItemIdMap = new HashMap<String,String>();

for(EnumerationItemBean item : dependentEnumerationItemList)
{
    String itemValue = item.getPicklistValue();
    String itemLabel = item.getPicklistLabel();
    String itemId = item.getId();
    dependentItemValToItemIdMap.put(itemValue,itemId);
}

List<ParentToChildMapping> pTc = new ArrayList<ParentToChildMapping>();
String dependentItemId = dependentItemValToItemIdMap.get("dep1");
String parentItemId = parentItemValToItemIdMap.get("value1");
ParentToChildMapping tmpPTCMap = new ParentToChildMapping();
tmpPTCMap.setDependentPicklistItemId(dependentItemId);
tmpPTCMap.setParentPicklistItemId(parentItemId);
pTc.add(tmpPTCMap);
dependentEnumerationDetails.setParentToChildMapping(pTc);

String response = Functions.updateFieldMetaData(dependentPicklistMeta);