Search code examples
nullpointerexceptionjirajira-pluginjira-rest-java-apiscriptrunner-for-jira

Create a new linked issue but new issue ID is null in Jira


I would like to create a new linked issue of type feature for a super feature issue. Here is my code: 

void createFeature(Issue issue, List<String> componentList){

    // Issue issue

//def issueManager = ComponentAccessor.issueManager

//def issueFactory = ComponentAccessor.issueFactory

def issueLinkManager = ComponentAccessor.issueLinkManager

//def userManager = ComponentAccessor.userManager

def authenticationContext = ComponentAccessor.jiraAuthenticationContext

def issueFactory = ComponentAccessor.getIssueFactory()

def issueManager = ComponentAccessor.getIssueManager()

def userManager = ComponentAccessor.getUserUtil()

 def currentUserObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();

MutableIssue newIssue= ComponentAccessor.issueFactory.issue

newIssue.setProjectObject(issue.getProjectObject())

newIssue.setSummary("MOUNA CAMELIA")

newIssue.setIssueTypeId("19")

int issueLinkType= 10070

long sequence =1

long newissueTypeID= 19

long myissueID= issue.getId()

 MutableIssue newissue

newissue = issueFactory.getIssue()

//set values

newissue.setProjectObject(issue.projectObject)

newissue.setIssueTypeId("19") 

newissue.setSummary("MOUNA CAMELIA")

newissue.setDescription("MOUNA CAMELIA")

newissue.reporter = issue.getReporter()

def newIssueCreated = issueManager.createIssueObject(currentUserObj, newissue)

newIssue.setDescription(issue.getDescription())

                for(String component: componentList){

                    log.warn("MOUNA CAMELIA component"+ component +" NEW ISSUE "+ newIssue)

                     log.warn("MOUNA CAMELIA Component ====="+ component+"===== myissueID ====="+ myissueID+" =====newissueTypeID ====="+newissueTypeID +" =====issueLinkType ====="+issueLinkType 

                     +" =====sequence ====="+ sequence +" =====authenticationContext.getLoggedInUser() ====="+authenticationContext.getLoggedInUser()+" newIssue.getId() "+newIssue.getId())                

                    issueLinkManager.createIssueLink(myissueID, newIssue.getId(), issueLinkType, sequence, authenticationContext.getLoggedInUser())

        }

}

I am using this code block to create a new issue 

MutableIssue newIssue= ComponentAccessor.issueFactory.issue

newIssue.setProjectObject(issue.getProjectObject())

newIssue.setSummary("MOUNA CAMELIA")

newIssue.setIssueTypeId("19")

int issueLinkType= 10070

long sequence =1

long newissueTypeID= 19

long myissueID= issue.getId()

then I am using this linke to assign the new issue as a linkedissue:

 issueLinkManager.createIssueLink(myissueID, newIssue.getId(), issueLinkType, sequence, authenticationContext.getLoggedInUser())

The function CreateIssueLink is helping assign the new issue as a linked issue. Here is the API I am using enter image description here The problem is that I get a null pointer exception because newIssue.getId() is null but I cannot assign an ID myselt to the new issue. How can I get the ID and have it automatically generated by Jira so I can avoid the nullpointerexception?


Solution

  • The following line of code was missing

    def newIssueCreated = issueManager.createIssueObject(currentUserObj, newissue)
    

    Then, we would need to use the variable newIssueCreated as a parameter for the function CreateIssueLink

    long newIssueCreatedID= newIssueCreated.getId()
                     for(String component: componentList){
    
                         log.warn("MOUNA CAMELIA ===== myissueID ====="+ myissueID+" =====newissueTypeID ====="+newIssueCreated.getId()
                          +" =====issueLinkType ====="+issueLinkType 
                         +" =====sequence ====="+ sequence +" =====authenticationContext.getLoggedInUser() ====="+authenticationContext.getLoggedInUser()
                         +"issuelinkmanager===="+issueLinkManager)                
                         issueLinkManager.createIssueLink(myissueID, newIssueCreatedID, issueLinkType, sequence, authenticationContext.getLoggedInUser())
    
             }
    }