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

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

tommy guo
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 10, 2013

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
kgood July 10, 2013

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());

tommy guo
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 10, 2013

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

tommy guo
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 10, 2013

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

kgood July 10, 2013

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.

tommy guo
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 10, 2013

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

kgood July 10, 2013

It hasn't not worked for me yet (:

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 11, 2013

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)

ioann god June 25, 2018
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 ->
...
}
*/
1 vote
Tormod Haugene July 19, 2019

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

1 vote
Niclas Sandstroem
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 28, 2017

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.

daniel siliski June 21, 2018

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

daniel siliski June 21, 2018

ah i got it:

 

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

1 vote
Roman Samorodov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 18, 2017

My example of code here

Niclas Sandstroem
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 27, 2017

Your code is not related to the select list options!

Roman Samorodov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 28, 2017

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.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 28, 2017

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

0 votes
yassine kaci July 26, 2017

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
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 10, 2013

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

tommy guo
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 10, 2013

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?

tommy guo
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 10, 2013

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
AUG Leaders

Atlassian Community Events