jira

Groovy script in post function #1 Updating Custom Field values (option) dependent on Custom Field value

Updating CF option with CF value dependency

Use Case

We have a project, where is necessary to select custom field option according to value of another custom field. We have custom field with id 10101 and it can have a 2 values. According to this value, we need to set option of custom field with id 10102 to option with id 99998 or 99999.

Groovy script

import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.customfields.option.Options;
import com.atlassian.jira.issue.customfields.option.Option;
import com.atlassian.jira.issue.ModifiedValue;
import com.atlassian.jira.component.ComponentAccessor;

// Custom field ID's
int sourceCfID = 10101;
int targetCfID = 10102;

// Option ID's of target custom field
int optionFirst = 99998;
int optionSecond = 99999;

CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();

String sourceCfValue = customFieldManager.getCustomFieldObject(sourceCfID).getValue(issue).toString();

CustomField targetCf = customFieldManager.getCustomFieldObject(targetCfID);

// If value of source custom field is XXX, then select first option, if NOT select second option
// This can be replaced by switch statement if you need to check more options!!!!
int targetCfOption = (sourceCfValue == 'XXX') ? optionFirst : optionSecond;

Options targetCfOptions = ComponentAccessor.getOptionsManager().getOptions(targetCf.getRelevantConfig(issue));

Option ChoosenOption = targetCfOptions.getOptionById(targetCfOption);

targetCf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(targetCf), ChoosenOption), new DefaultIssueChangeHolder());

Pay attention to obtaining custom field object. We are using CustomFieldManager and getCustomFieldObject(Long id) method to get custom field object, if you need to write it with custom field names, you are able to use getCustomFieldObjectByName(String name).
Next important point in this script is option selecting. At first you have to get all Options via ComponentAccessor and then find option by selected ID. Updating value of custom field containing options is realized by updateValue(FieldLayoutItem fieldLayoutItem, Issue issue, ModifiedValue modifiedValue, IssueChangeHolder issueChangeHolder). We set FieldLayoutItem to null, use global variable issue to get current issue instance, new ModifiedValue where we get customfield and new selected option and as last create new DefaultIssueChangeHolder by default(empty) constructor.

Aleš Laňar
Senior Engineer Software ve společnosti CA Technologies

8 thoughts on “Groovy script in post function #1 Updating Custom Field values (option) dependent on Custom Field value

  1. Askіng questions are actually nice thing if you are not understanding anything fully,
    but this poѕt ցives pleasant understanding
    yet.

  2. Hi there! Thiѕ articlе couldn’t be written much better!
    Looking at this post reminds me of my previous rοommate!
    He always kept talking about this. I will send this infοrmation to him.
    Faiгly certain he will have a good read.
    Tɦank you for shaгing!

  3. Ꮋi theгe! I could have sworn I’ve been to this blog befоre but after checking throuɡҺ sοme of
    the post I realized it’s neᴡ tօ me. Nonetheless, I’m definitely ɗelighted I found it аnd I’ll be book-marking аnd
    checking back often!

    1. Thank you! I hope yes and very soon. I am also trying to find more time to create posts and examples for Confluence User Macros and currently I am planning Jira plugin developement to export only project configuration.

Leave a Reply

Your email address will not be published. Required fields are marked *