Change custom field ( select field type) value not working.

Hi,

I have written a script to assigna value to a custom field of select field type. I want to change the value of the field to 'Yes' option. I added a add comment statement to the code which would show the value of the field after I set it to 'yes'. The output comment is [Yes].

But when I search for issues with 'Yes' option for the custom field as the filter, the issue doesnt show up.

Can some one tell me what might be the issue?

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.manager.OptionsManager

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.index.IssueIndexManager
import com.opensymphony.workflow.WorkflowContext
import org.apache.log4j.Category

import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.user.ApplicationUsers

ComponentAccessor componentAccessor= ComponentAccessor.newInstance()
CustomFieldManager customFieldManager = componentAccessor.getCustomFieldManager()
IssueManager issueManager = componentAccessor.getIssueManager()

String currentUser = ((WorkflowContext) transientVars.get("context")).getCaller();

def tgtField = customFieldManager.getCustomFieldObjectByName("Test-GlobDefect")
def tgtFieldValue = issue.getCustomFieldValue(tgtField)

OptionsManager optionsManager = ComponentAccessor.getOptionsManager()
Option option = optionsManager.getOptions(tgtField.getRelevantConfig(issue)).getOptionForValue("No", null);

issue.setCustomFieldValue(tgtField ,[option])

issueManager.updateIssue(componentAccessor.jiraAuthenticationContext.getLoggedInUser(), issue, EventDispatchOption.DO_NOT_DISPATCH, false)

CustomField srcField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Test-GlobDefect"}
cfwt = issue.getCustomFieldValue(srcField).toString()



commmgr = (CommentManager) ComponentManager.getComponentInstanceOfType(CommentManager.class)
commmgr.create(issue, currentUser,cfwt , true)
issue.store()

3 answers

you add the statement

ComponentAccessor.getIssueIndexManager().reIndex(issue);

at the end and check.

Hi,

I realized there is some issue with this line:

issueManager.updateIssue(componentAccessor.jiraAuthenticationContext.getLoggedInUser(), issue, EventDispatchOption.DO_NOT_DISPATCH, false)

with this line, the script doesnt run..

Yes, I think componentAccessor.getJiraAuthenticationContext().getLoggedInUser() would be there.

Did you tried by adding reindex code ?

Yes I tried tried re-indexing.. didnt help.

where you are adding this code?

We've had a problem with multi-select fields (from what I gather that's the type of your field), where we couldn't filter the information by their values. The way I fixed it is, I made the multi-select field behave like the default Component field in Jira (i.e. add lables for each option). I did that by adding this JavaScript to the description of the multi-select field:

<script type="text/javascript">
(function($) {
  
 // "customfield_10400" is the number of the custom 
 // multiselect field you want to change as e.g. 
 // seen at the end of the "configure" page url of 
 // the field you want to change this way 
  
    AJS.$("#customfield_10400 option[value='-1']").remove(); //Removes the default value "None"
    function convertMulti(id){
        if (AJS.$('#'+id+"-textarea").length == 0){
            new AJS.MultiSelect({
                element: $("#"+id),
                itemAttrDisplayed: "label",
                errorMessage: AJS.params.multiselectComponentsError
            });
  
        }
    }
  
    AJS.toInit(function(){   
        convertMulti("customfield_10400");
    })
  
    JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e, context) {
        AJS.$("#customfield_10400 option[value='-1']").remove();
        convertMulti("customfield_10400");
    });
  
})(AJS.$);
</script>

After that everything worked like a charm.

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

533 views 1 18
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you