Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,458,912
Community Members
 
Community Events
176
Community Groups

How do I programmatically set values for custom fields of type select when creating an issue?

I have tried using:

IssueInputParameters.addCustomFieldValue(customField.getIdAsLong(), issue.getCustomFieldValue(customField).toString())

But this is causing errors in my CreateValidationResult

6 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

3 votes
Answer accepted

This is how I've been doing it, it works well enough for me:

CustomField customField = customFieldManager.getCustomFieldObject(customFieldId);

Options options = WebAppsCf.getOptions(null, customField.getRelevantConfig(issue), null);	

Option newOption = options.getOptionById(newOptionId);

ModifiedValue mVal = new ModifiedValue(issue.getCustomFieldValue(customField), newOption );

customField.updateValue(null, issue, mVal, new DefaultIssueChangeHolder());

do I have to use customfield.updateValue? Will a direct implementation of Issue.setCustomFieldValue not work?

By the way I'm guessing that this method will work for setting values of any custom field type that consists of options?

I don't see Issue.setCustomFieldValue in the api's which is probably why i didn't use it But if that method does exist, I think it would work, the important thing is you set the new value as a proper Option object.

Kyle I used your method and it worked! Thanks =)))

It hasn't not worked for me yet (:

Yes, select, multi-select, radio and check boxes will work. You might need to tweak it for cascading-select. The same principle will work for other field types too - for dates, use a timestamp, for user fields, a user object, versions for version type fields etc.

However, that code looks like it overrides the current value with a single value. For multi fields, I think you need to pass it a list of options. If you want to update, rather than overwrite, you'll need to fetch the current list and add to it (obviously, for "create", that's irrelevant)

def myCustomField = getCustomFieldObjects(issue).find { 
it.name == "My multi-select field"
}

// just passing a values as List/Map.
issue.setCustomFieldValue(myCustomField, [value])

/*
// e. g.
def values = issue.getCustomFieldValue(myCustomField)
values.each
{
Option it ->
...
}
*/

Reading an setting values is documented well in this book linked below.

 

It also includes very good examples of how to work with Custom Fields of the "Checkboxes" type - which is hard to find documentation for (see page 107-ish).

 

https://books.google.no/books?id=ioZcDgAAQBAJ&pg=PA85&lpg=PA85&dq=com.atlassian.jira.issue.context.GlobalIssueContext+not+found+by&source=bl&ots=-hMv8Mhr-r&sig=ACfU3U1DNn5_VvdNBGQa4t7mssvvMo8-hQ&hl=no&sa=X&ved=2ahUKEwjl9KKl0cDjAhW-xMQBHSnaDQkQ6AEwDHoECAkQAQ#v=onepage&q=Option&f=false

This is my variant of doing this but I'm not really happy yet. This also enables you to set disabled options so beware of this. 

// Get the project the issue should be created in.
def curPrj = projectManager.getProjectObjByKey("Project I want to set");

// Set the issuetype that should be used.
def issueType = issueTypeSchemeManager.getIssueTypesForProject(curPrj).find{it.name=="Issue Type I want to set"};
def issueContext = new IssueContextImpl(curPrj, issueType);
def fieldConfig = customfield.getRelevantConfig(issueContext);
def options = optionsManager.getOptions(fieldConfig); 
def optionToSelect = options.find { it.value == "Select Option I want to set" }; 
return optionToSelect.getOptionId();


Then in issue input parameters remember to treat it as a string.

Sorry if this is a very dumb question but how to I declare the optionsManager?  

ah i got it:

 

import com.atlassian.jira.component.ComponentAccessor
def optionsManager = ComponentAccessor.getOptionsManager()

My example of code here

Your code is not related to the select list options!

Niclas, yes but it may help you with an example of modern code that is not from 2013. It also represents Validation technique which is needed if you want your code to be safe.

But there's not a lot of point in giving code that doesn't have anything to do with the question.

mutableIssueParent.setCustomFieldValue(fieldIssue, value);

FieldLayoutItem fieldLayoutItem = ComponentAccessor.getFieldLayoutManager().getFieldLayout(mutableIssueParent).getFieldLayoutItem(fieldIssue);

DefaultIssueChangeHolder issueChangeHolder = new DefaultIssueChangeHolder();

Map<String, ModifiedValue> modifiedFields = mutableIssueParent.getModifiedFields();

final ModifiedValue modifiedValue = (ModifiedValue) modifiedFields.get(fieldIssue.getId());

fieldIssue.updateValue(fieldLayoutItem, mutableIssueParent, modifiedValue, issueChangeHolder);
        
// Dont forget this to search the latest elements updated
IssueIndexingService is=ComponentAccessor.getComponent(IssueIndexingService.class);
try {
is.reIndex(issue);
} catch (IndexException e) {
log.error("Erreur reIndexIssue ",e);
}

0 votes

Well, it would - you seem to be using strings, when select list fields expect "option" objects.

Unfortunately the only options I have when using the addCustomFieldValue function is (Long , String) or (String, String) , I dont have a choice of (Long, Option). Is there some other way I can get these custom fields populated in my IssueInputParameters variable?

If not what's the correct way to populate these fields after the issue has been created? I've tried using

IssueResult.getIssue().setCustomFieldValue(customField, Option ) but I dont know what to put to get the correct option for the field.

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS

Atlassian Community Events