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

sharma-shweta October 8, 2013

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

1 vote
Bharadwaj Jannu
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.
October 8, 2013

you add the statement

ComponentAccessor.getIssueIndexManager().reIndex(issue);

at the end and check.

sharma-shweta October 8, 2013

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..
Bharadwaj Jannu
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.
October 8, 2013

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

RambanamP
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.
October 8, 2013

Did you tried by adding reindex code ?

sharma-shweta October 8, 2013

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

Bharadwaj Jannu
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.
October 8, 2013

where you are adding this code?

0 votes
RambanamP
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.
October 8, 2013
0 votes
DanielM
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.
October 8, 2013

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