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

Groovy Script post function - Adding custom fields to multiple cloned issues

Deborah Hansen January 26, 2012

I do not have the custom field aspect working at all. I think I'm just missing some definition or import that is probably obvious (just not to me)? The intersect CF management you use looks great, but bombs for me not knowing what newissuecfs is. Also tried to simply get rid of the "$it" per your example, but I'm not sure how to define projectName (no such property). Groovy manuals, forums all around me, just not enough practice yet... :(

So - at current state is below: Added the import for customfield manager, changed the newissue to cloneissue rather than getissue, still have the old for loop for the cf options and I get the following in my logs...

groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.For() is applicable for argument types: (java.util.ArrayList, Script124$_run_closure1_closure2) values: [[...all my custom fields...], Script124$_run_closure1_closure2@61774e]
Possible solutions: any(), wait(), every(), dump(), get(java.lang.String), put(java.lang.String, java.lang.Object)

A little more assistance would be greatly appreciated... TIA! Ran out of room on previous question: https://answers.atlassian.com/questions/29674/groovy-script-modification-clone-w-dependency-need-project-array

import com.atlassian.core.user.UserUtils
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.util.ImportUtils
import com.opensymphony.user.User
import com.opensymphony.workflow.WorkflowContext
import org.apache.log4j.Category
import com.atlassian.jira.issue.CustomFieldManager //NEW

issueMgr = componentManager.getIssueManager()
issueFactory = componentManager.getIssueFactory()
projectMgr = ComponentManager.getInstance().getProjectManager()
versionManager = componentManager.getVersionManager()
customFieldManager = componentManager.getCustomFieldManager()
String currentUser = ((WorkflowContext) transientVars.get("context")).getCaller();
User currentUserObj = UserUtils.getUser(currentUser);
issue = issue

def wasIndexing = ImportUtils.indexIssues
ImportUtils.indexIssues = true

log = Category.getInstance("com.onresolve.jira.groovy.CreateDependentIssue")

// Configurable section
def ProjectList = ["Project A", "Project B"] //list of projects

ProjectList.each() {

// for other customisations change the code below

newissue = issueFactory.cloneIssue(issue)
newissue.setSummary ("Common - $issue.summary")
newissue.setProject (projectMgr.getProjectByName("${it}"))

def cf = customFieldManager.getCustomFieldObjects(issue) //???
For (cf) {
if (issue.getCustomFieldValue(cf)) {
newissue.setCustomFieldValue(cf, issue.getCustomFieldValue(cf))
}
}

issueMgr.createIssue(currentUserObj, newissue)

// get the current list of outwards depends on links to get the sequence number
linkMgr = ComponentManager.getInstance().getIssueLinkManager()
def sequence = 0
for (IssueLink link in linkMgr.getInwardLinks(issue.id)) {
if ("Depends" == link.issueLinkType.name) {
sequence++;
}
}

linkMgr = ComponentManager.getInstance().getIssueLinkManager()
linkMgr.createIssueLink (newissue.id, issue.id, 10001, sequence, currentUserObj)
ImportUtils.indexIssues = wasIndexing
}

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
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.
January 27, 2012

As Jon says it's for not For, but even if you change that it's still wrong. Given:

def cfList = customFieldManager.getCustomFieldObjects(issue) 

cfList is a collection, so you can either of the following:

for (CustomField cf in cfList)

or

cfList.each {cf ->

You can remove the it by naming the parameter for the closure, like I did above. So in this case you would use cf instead of it.

Check out: http://groovy.codehaus.org/Looping

Deborah Hansen January 29, 2012

Perfect!!! Thanks so much - Drinks are on me if you're going to Summit this year. :)

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.
January 29, 2012

Cheers, mine's a "Doom Bar" ;-)

Deborah Hansen February 9, 2012

Jamie - One more thing on this. Can you help me add it so the cloned issue is assigned to the project lead?

I know this will mean I'll be booking a flight over the pond to get you a 2nd, but I'm due for a holiday soon anyway ;) Thanks in advance....

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.
February 9, 2012

You can post it to me ;-)

Replace this line:

newissue.setProject (projectMgr.getProjectByName("${it}"))

with:

def project = projectMgr.getProjectByName(it)
newIssue.setProject(project)
newIssue.setAssigneeId(project.get("lead"))

Deborah Hansen February 20, 2012

And... Currently only the first item is indexing? is there a magical trick to index everything?

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.
February 20, 2012

You need indexManager.reIndex(newIssue)

Deborah Hansen February 21, 2012

thanks!!! Again! :) Ended up having to code it like this but it's working!

ImportUtils.setIndexIssues(true);
indexManager.reIndex(newIssue);
ImportUtils.setIndexIssues(false);

0 votes
Jon Sword
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 27, 2012

While I was suggesting some debugging code over here I noticed a bug. The "For" used in your custom field loop should be lower-case.

Enjoy...

TAGS
AUG Leaders

Atlassian Community Events