Create Issue in project depending on custom field (ScriptRunner)

Simon Kegel //SEIBERT/MEDIA
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.
November 25, 2014

Hey there,

I want to create a scripted post function which creates issues in project X. Project is depending on a custom field "customer" on the transition - screen.

Has anyone experience with this and / or could help me with this?

This is the code a colleague hand me over but it doesn't work right:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.util.ImportUtils
import com.opensymphony.workflow.WorkflowContext
import org.apache.log4j.Category

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.crowd.embedded.api.CrowdService
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.bc.issue.IssueService;
import com.atlassian.jira.issue.IssueInputParameters

log = Category.getInstance("com.onresolve.jira.groovy.DelegateIssue")
log.error("Processing: " + issue.key);

CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();
IssueManager issueManager = ComponentAccessor.getIssueManager();
IssueService issueService = ComponentAccessor.getIssueService();

Object readLocalCustomField(issue, String fieldName) { // Read by NAME
	CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();
	def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == fieldName}
	return cf.getValue(issue)  //return issue.getCustomFieldValue(cf)
}

def project = readLocalCustomField(issue, "customer")
log.error("Customer: " + project);

issueMgr = ComponentManager.getInstance().getIssueManager()
projectMgr = ComponentManager.getInstance().getProjectManager()
 
String currentUserName = ((WorkflowContext) transientVars.get("context")).getCaller();
UserManager userManager = ComponentAccessor.getUserManager();
ApplicationUser currentApplicationUserObj = userManager.getUserByName(currentUserName);
User currentUserObj = currentApplicationUserObj.getDirectoryUser();

//User currentUserObj = CrowdService.getUser(currentUserName);
 
def wasIndexing = ImportUtils.indexIssues
ImportUtils.indexIssues = true

IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();

// Regular Fields
issueInputParameters.setProjectId(project.id);
issueInputParameters.setIssueTypeId(issue.getIssueTypeId());
issueInputParameters.setSummary(issue.summary);
issueInputParameters.setReporterId(issue.getReporterId());
issueInputParameters.setDescription(issue.description);
 	
// Custom Fields
def customField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "foo"};
issueInputParameters.addCustomFieldValue(customField.getIdAsLong(), "bar");


IssueService.CreateValidationResult result = issueService.validateCreate(currentUserObj, issueInputParameters);
if(!result.isValid()) {
    log.error("could not create issue: " +  result.getErrorCollection());
}

newissue = issueService.create(currentUserObj, result).getIssue()

// get the current list of outwards Blocks on links to get the sequence number
linkMgr = ComponentManager.getInstance().getIssueLinkManager()
def sequence = 0
for (IssueLink link in linkMgr.getInwardLinks(issue.id)) {
    if ("Blocks" == link.issueLinkType.name) {
        sequence++;
    }
}
 
linkMgr = ComponentManager.getInstance().getIssueLinkManager()
// TODO: Should check that 10000 is always the link id for Blocks
linkMgr.createIssueLink (newissue.id, issue.id, 10000, sequence, currentUserObj)
ImportUtils.indexIssues = wasIndexing

 

Any idea?  
I'm grateful for any hint.

Greets
Simon 

2 answers

1 accepted

1 vote
Answer accepted
Bob Swift OSS (Bob Swift Atlassian Apps)
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.
November 25, 2014

I can't help with the scripting problem, but, I can suggest a non-programming alternative:

Use the Create Issue post function from Create on Transition for JIRA. Then use %original_customer% substitution variable in the project field.

Simon Kegel //SEIBERT/MEDIA
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.
November 26, 2014

Thanks! I thought I can just set a fix value in the project field when Create issues with the post function. Makes much more sense than a script with less scripting knowledge :D

Simon Kegel //SEIBERT/MEDIA
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.
November 26, 2014

Then I just have a short question what exactly I have to fill in the Issue Key field when setting up a Link? The only thing I can't get to work now. I appreciate your reply.

Bob Swift OSS (Bob Swift Atlassian Apps)
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.
November 26, 2014

Are you trying to link the new issue to the original issue as a Blocks link? Then use *%original_key%* and *Blocks* for the fields. The pick the link direction you need.

Bob Swift OSS (Bob Swift Atlassian Apps)
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.
November 26, 2014

Ignore the *s, I don't know why we don't have wiki markup support in the comment editor.

0 votes
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.
November 25, 2014

Can you give us more info on how it's not working?

An easier alternative might be to use the built-in script, "Clone and link" with some "additional code" for setting the project. I think this has been done before.

Simon Kegel //SEIBERT/MEDIA
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.
November 26, 2014

Ìt's not working like it does nothing .. But got another solution so it's ok. Thank you anyway :)

Suggest an answer

Log in or Sign up to answer