Updating CF option with CF value dependency
Use Case
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.
Do you ɦaѵe any video of that? I’d like to find out more
details.
Unfortunately I haven’t.
Askіng questions are actually nice thing if you are not understanding anything fully,
but this poѕt ցives pleasant understanding
yet.
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!
Ꮋ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!
Hi, I am very happy for that and thank you.
Нighly energеtic poѕt, I liked that a lot.
Will there be a part 2?
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.