Setting Component Field value in JIRA groovy code

jaideep srivastava March 9, 2014

Hi,

I need to set the System field values in JIRA through Groovy code (I have Component fields as "JIRA" and "Confluence" defined). I executed the below code to set the value as "JIRA" (event set for "issue updated"). I do not get any error in the logs but the value is not getting set. can anyone help for the same ? thanks

Code---

@Override
void workflowEvent(IssueEvent event) {
Issue issue = event.getIssue();

ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
IssueManager issueManager = componentManager.getIssueManager()

Project project = issue.getProjectObject()
ProjectComponent component = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(),"JIRA")
issue.setComponentObjects([component])

2 answers

1 accepted

1 vote
Answer accepted
Henning Tietgens
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.
March 9, 2014

issue should be a MutableIssue and you have to store your changes. See my answer here for details. Additionally you could add log messages like

log "component: $component"

to see if everthing is working like you expect.

JamieA
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.
March 9, 2014

log.warn or log.debug etc, you probably meant.

Henning Tietgens
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.
March 10, 2014

Oh.. yes :-)

0 votes
jaideep srivastava March 10, 2014

Thanks Henning for your pointers, it was mutable issue and storething. By logs i meant that everything was working otherwise a big exception comes. i had removed the log messages before posting on the forum. below is the code which worked for me. once again a big thanks.

Category log = Category.getInstance(test.class)

@Override
void issueUpdated (IssueEvent event) {
log.setLevel(org.apache.log4j.Level.DEBUG)


def groupManager = ComponentAccessor.getGroupManager()

ComponentManager componentManager = ComponentManager.getInstance()
def customFieldManager = componentManager.getCustomFieldManager()
//def watcherManager = ComponentAccessor.getWatcherManager()


MutableIssue issue = event.issue
Project project = issue.getProjectObject()
IssueManager issueManager = componentManager.getIssueManager()

def componentJIRA = componentManager.getProjectComponentManager().findByComponentName(project.getId(),'JIRA')
def componentConfluence = componentManager.getProjectComponentManager().findByComponentName(project.getId(),'Confluence')

def Agroup=issue.getCustomFieldValue( customFieldManager.getCustomFieldObjectByName("Assignment Group"))
try
{
if(Agroup.name.contains("Blocker"))
{
issue.setComponents([componentJIRA.getGenericValue()])
issueManager.updateIssue(null, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
issue.store()
log.debug "Blocker issue"
}



catch(e)
{
issue.setComponents(null)
issueManager.updateIssue(null, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
issue.store()
log.debug "Exception Occured, Blank value is set for Group"
}


}

}

Henning Tietgens
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.
March 10, 2014

Fine :-) One remark, if you call issueManager.updateIssue() you don't have to call issue.store().

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events