Setting Component Field value in JIRA groovy code

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
Accepted answer
Henning Tietgens Community Champion Mar 09, 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.

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

Henning Tietgens Community Champion Mar 10, 2014

Oh.. yes :-)

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 Community Champion Mar 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
Community showcase
Published Nov 29, 2018 in Marketplace Apps

How to set up an incident workflow from the VP of Engineering at Sentry

Hey Atlassian community, I help lead engineering at Sentry, an open-source error-tracking and monitoring tool that integrates with Jira. We started using Jira Software Cloud internally last year, a...

1,144 views 0 8
Read article

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