Get EPIC Issue key on create

Hello All,

 

I tried searching this solution and have yet to come across one,.

 

Currently what im trying to do is on create transition of a NON Epic issue, i create an EPIC case if a tick box is checked. 

I would like to link this issue to the newly created EPIC but is not sure how to get the new EPIC issue key when its created. 

 

Here is the code i have so far:

 

package IssueCreation
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.issue.index.IssueIndexManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.util.ErrorCollection
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.CustomFieldManager

MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject('TEST-462')
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

IssueService issueService = ComponentAccessor.getIssueService();
IssueInputParameters inputParameters = issueService.newIssueInputParameters();
UserManager userManager = ComponentAccessor.getUserManager();

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
String firstName = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject(10004));
String lastName = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject(10005));
String caseVal = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject(13013));

if (caseVal == "[Raise New]") {
    
            inputParameters
                  .setProjectId(11700) // Case
                  .setIssueTypeId("12301") // Epic
                  .setReporterId(currentUser.name)
                  .setSummary("TEST CASE")
                  .setAssigneeId(currentUser.name)
                  .addCustomFieldValue("customfield_10000", "test ID")
    			  .addCustomFieldValue("customfield_13004", "test Case")
                  //.addCustomFieldValue("customfield_13002", issue.key)
                  .addCustomFieldValue("customfield_10004", firstName)
                  .addCustomFieldValue("customfield_10005", lastName)
                  .addCustomFieldValue("customfield_13012", '13712');

          IssueService.CreateValidationResult createValidationResult = issueService.validateCreate(currentUser, inputParameters)

          if(!createValidationResult.isValid()){
              ErrorCollection errorCollection = createValidationResult.getErrorCollection();
              return errorCollection.toString()
          }
          else{
              IssueService.IssueResult createResult = issueService.create(currentUser, createValidationResult)
              IssueIndexManager issueIndexManager = ComponentAccessor.getIssueIndexManager();
              issueIndexManager.reIndex(createResult.getIssue())
          }
    

    
    }







Many thanks for the help.

 

Cheers, 

Pon

 

 

 

1 answer

Here is the solution for anyone who needs it:

 

package IssueCreation
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.issue.index.IssueIndexManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.util.ErrorCollection
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.CustomFieldManager

MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject('TEST-462')
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

IssueService issueService = ComponentAccessor.getIssueService();
IssueInputParameters inputParameters = issueService.newIssueInputParameters();
UserManager userManager = ComponentAccessor.getUserManager();

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
String firstName = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject(10004));
String lastName = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject(10005));
String caseVal = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject(13013));

if (caseVal == "[Raise New]") {
    
            inputParameters
                  .setProjectId(11700) // Case
                  .setIssueTypeId("12301")
                  .setReporterId(currentUser.name)
                  .setSummary("TEST CASE")
                  .setAssigneeId(currentUser.name)
                  .addCustomFieldValue("customfield_10000", "test ID")
         .addCustomFieldValue("customfield_13004", "test Case")
                  //.addCustomFieldValue("customfield_13002", issue.key)
                  .addCustomFieldValue("customfield_10004", firstName)
                  .addCustomFieldValue("customfield_10005", lastName)
                  .addCustomFieldValue("customfield_13012", '13712');

          IssueService.CreateValidationResult createValidationResult = issueService.validateCreate(currentUser, inputParameters)

          if(!createValidationResult.isValid()){
              ErrorCollection errorCollection = createValidationResult.getErrorCollection();
              return errorCollection.toString()
          }
          else{
              IssueService.IssueResult createResult = issueService.create(currentUser, createValidationResult)
              IssueIndexManager issueIndexManager = ComponentAccessor.getIssueIndexManager();
              issueIndexManager.reIndex(createResult.getIssue())
      
     def newIssue = createResult.getIssue()
              return newIssue
             

          }


    }







Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Tuesday in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

118 views 0 4
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