Monday, 16 July 2012

How to save Custom Field values?

Today, we will see, the ways to save values in Jira Custom Fields. Firstly, let us understand what is Custom Field in Jira platform.

Custom Fields is the additional feature given by the Jira platform to tailor Jira's platform for your organization needs. There are nearly 20 types of Custom Field Types available in Jira platform and saving values in custom fields depends on custom field's type.

Ex:-1
Let's suppose, there is a requirement to save a "Free Text" type custom field of name "Description", then

"Description" is the custom field name
"ABCD....XYZ" is the description value to be stored.


CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField customField =  getCustomFieldObjectByName("Description");
issue.setCustomFieldValue(customField, "ABCD....XYZ");


Ex:- 2
Let's suppose, there is a requirement to save a "Select Type" custom field type.

"Severity" is the custom field name and has options as "Critical", "High", "Medium" and "Low"
"High" is the custom field value to be stored.


public void updateCustomFieldValue(MutableIssue issue, String customFieldName, Object customFieldValue) {

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField customField =  getCustomFieldObjectByName("Description");
String customFieldType = customField.getCustomFieldType().getName();
if (customFieldType.equalsIgnoreCase("Select Type")) {
List<Option> options = getOptionsManager().findByOptionValue((String)customFieldValue);
            if (options == null || options.isEmpty()) {
                logger.error("Can't set custom field value to " + customFieldValue + " because it doesn't correspond to a valid custom field option of the custom field : " + customField.getName());
            } else {
                issue.setCustomFieldValue(customField, options.get(0));
            }
}

}

private OptionsManager getOptionsManager() {
        return  ((OptionsManager) ComponentManager.getInstance().getComponentInstanceOfType(OptionsManager.class));
    }


Above mentioned scenario is valid for custom field types such as "Multi Checkboxes" and "Select Type"

Ex:- 3
Let's suppose, there is a requirement to save a "Cascading Select" custom field type.

State/District are the cascading select drop down list, based on the selection of state value, districts drop-down will be populated.

State/District is the custom field name;


/**
 * This method is to populate custom field value from "Cascading Select" type custom fields.
 * 
 **/
public static Map<String, String> populateStateDistrictMap(
            MutableIssue issue) {
        Map<String, String> stateDistrictmap = new HashMap<String, String>();
        CustomField stateDistrictCF=  getStateDistrictCF();

        Object stateDistrictCFValue= issue
                .getCustomFieldValue(stateDistrictCF);
        CustomFieldParams params = new CustomFieldParamsImpl(stateDistrictCF, stateDistrictCFValue);
        String stateCFValue = null;
        String districtCFValue = null;
        if (params != null && !params.isEmpty()) {
            stateCFValue = params.getFirstValueForNullKey().toString();
            districtCFValue = params.getFirstValueForKey("1").toString();
        }
        stateDistrictmap.put("STATE", stateCFValue);
        stateDistrictmap.put("DISTRICT", districtCFValue);

         return stateDistrictmap;
}


public static boolean updateStateDistrictMap(MutableIssue issue, String stateStr, String districtStr) {
        List<Option> stateCFOption = getOptionsManager().findByOptionValue(stateStr);
        List<Option> districtCFOption = getOptionsManager().findByOptionValue(districtStr);
        Map<String, Option> map = new HashMap<String, Option>();
        map.put((String) null, stateCFOption .get(0));
        map.put("1", districtCFOption .get(0));
        CustomField stateDistrictCF=  getStateDistrictCF(); 
        issueToCreate.setCustomFieldValue(stateDistrictCF, map);
}

private CustomField getStateDistrictCF(){
        CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
        CustomField stateDistrictCF=  getCustomFieldObjectByName("State/District"); 
        return  stateDistrictCF;
}

private OptionsManager getOptionsManager() {
        return  ((OptionsManager) ComponentManager.getInstance().getComponentInstanceOfType(OptionsManager.class));
    }






3 comments:

  1. Hi Sateesh,

    Wat if two custom fields are there with same name.

    ReplyDelete
    Replies
    1. Yes, this case is possible as jira won't have any validation on creating a replica of already existing custom field. In this case, you need to access customfield based on it's id.

      Delete
  2. Hi,

    Nice post, this was pretty easy and straight forward information!

    Just a question, you said:
    "Above mentioned scenario is valid for custom field types such as "Multi Checkboxes" and "Select Type""

    Do you really mean Select Type? That is like "Select List" for me. I guess you mean Multi Checkbox and Multi Select works they same way?

    BR
    Chris

    ReplyDelete