Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Jira Server CreateSubTaskIssueLink Error

Shachar Ovadia August 30, 2021

Hey guys, im creating subtask issue from a workflow transition, the issue is being created but it won't link the subtask to the parent issue, here is my code

def summary = getUser(members[currentMember])
def memberCapacityIssue = ComponentAccessor.issueFactory.getIssue();

memberCapacityIssue.projectObject = project;
memberCapacityIssue.issueTypeId = subTaskId;
memberCapacityIssue.summary = summary;
memberCapacityIssue.setParentId(issue.getId())
memberCapacityIssue.assignee = issue.getAssignee() == null ? user : issue.getAssignee();
memberCapacityIssue.reporter = issue.getReporter() == null ? user : issue.getReporter();
memberCapacityIssue = issueManager.createIssueObject(user, memberCapacityIssue);
subTaskManager.createSubTaskIssueLink(issue, memberCapacityIssue, user); this line is errored
// 
issue.store()

2021-08-29 22:11:34,635 ERROR [workflow.AbstractScriptWorkflowFunction]: Workflow script has failed  java.lang.NullPointerException

at com.atlassian.jira.config.SubTaskManager$createSubTaskIssueLink.call(Unknown Source) at Script8444.run(Script8444.groovy:51)

1 answer

1 accepted

0 votes
Answer accepted
Juan José Marchal Gómez
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.
August 30, 2021

Hello @Shachar Ovadia ,

issue store is deprecated, try to not use.

This code works for me. I've recommend to check the code in console before in transition.

For checking in console only it's needed that you uncomment the line that I've added with "def issue" and set the issue key of the issue that you are transitioning.

 

Best regards!

 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.config.SubTaskManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.user.ApplicationUser

def issueManager = ComponentAccessor.getIssueManager()
def issueService = ComponentAccessor.getIssueService()
def constantsManager = COmponentAccessor.getConstantManager()
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
//def issue = issueManager.getIssueObject("ISUEKEYINCONSOLE")
def parentIssue = issue //

// the issue type for the new issue - should be of type subtask
final issueTypeName = 'Release documentation' //set here your subtaks type name

def subtaskIssueTypes = constantsManager.allIssueTypeObjects.findAll { it.subTask }
def subTaskIssueType = subtaskIssueTypes.findByName(issueTypeName)
def reporter = parentIssue.getReporter()
final summary = parentIssue.getSummary()
final description = parentIssue.getDescription()
def assignee = parentIssue.getAssignee()

SubTaskManager subTaskManager = ComponentAccessor.getSubTaskManager();

def issueInputParameters = issueService.newIssueInputParameters().with
{
setProjectId(parentIssue.projectObject.id)
setIssueTypeId(subTaskIssueType.id)
setReporterId(reporter.username)
setSummary(summary)
setAssignee(assignee)
setDescription(description)
}


def validationResult = issueService.validateSubTaskCreate(loggedInUser, parentIssue.id, issueInputParameters)
assert validationResult.valid : validationResult.errorCollection

def issueResult = issueService.create(loggedInUser, validationResult)
assert issueResult.valid : issueResult.errorCollection

def subtask = issueResult.issue

if (subtask){
subTaskManager.createSubTaskIssueLink(parentIssue, subtask, loggedInUser)
}

Shachar Ovadia August 30, 2021

Hi !

Thank you for your response.

Im still Getting Errors,

This is my new code => 

// Managers
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def userSearchService = ComponentAccessor.getComponent(UserSearchService);
def constantsManager = ComponentAccessor.getConstantsManager()
def userManager = ComponentAccessor.getUserManager();
def issueService = ComponentAccessor.getIssueService();
SubTaskManager subTaskManager = ComponentAccessor.getSubTaskManager();

Issue issue = issueManager.getIssueObject("CAP-238")

// Finding the wanted issue Type (Sub)
final issueTypeName = 'Team Member Capacity' //set here your subtaks type name
def subtaskIssueTypes = constantsManager.allIssueTypeObjects.findAll { it.subTask }
def subTaskIssueType = subtaskIssueTypes.findByName(issueTypeName)

def project = ComponentAccessor.projectManager.getProjectObjByKey("CAP");
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser;
def currentEpicStatus = issue.getStatus();

// issue fields
def epicSummary = issue.summary;
def epicDescription = issue.description;

def relatedScrumTeam = customFieldManager.getCustomFieldObject("customfield_14053");
def membersInScrumTeam = customFieldManager.getCustomFieldObject("customfield_14007");
MutableIssue scrumTeamIssue = issueManager.getIssueObject(issue.getCustomFieldValue(relatedScrumTeam).toString());
def members = scrumTeamIssue.getCustomFieldValue(membersInScrumTeam) as Collection;

for(def currentMember = 0; currentMember < members.size(); currentMember ++) {

def summary = getUser(members[currentMember])
def memberCapacityIssue = ComponentAccessor.issueFactory.getIssue();

def issueInputParameters = issueService.newIssueInputParameters().with
{
setProjectId(issue.projectObject.id)
setIssueTypeId(subTaskIssueType.id)
setSummary(summary);
setReporterId(getUserId(members[currentMember]))
}

def validationResult = issueService.validateSubTaskCreate(user, issue.id, issueInputParameters)
assert validationResult.valid : validationResult.errorCollection

def issueResult = issueService.create(user, validationResult)
assert issueResult.valid : issueResult.errorCollection

def subtask = issueResult.issue

if (subtask){
Issue subtaskIssue = issueManager.getIssueObject(subtask.toString());
Issue parent = issueManager.getIssueObject(issue.toString())
log.warn(subtaskIssue.getIssueType())
log.warn(parent.issueType)
subTaskManager.createSubTaskIssueLink(parent, subtaskIssue, user)
}
}

Now, intresting part, the parent issuetype style is null, while the created issuetype style is jira_subtask.

i guess the errors (Same as before) coming from this ^

Juan José Marchal Gómez
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.
August 30, 2021

Hello,

could you log before "subTaskManager.createSubTask" 

subtaskIssue.getKey()

parent.getKey()

and tell us the result, please?

Best regards.

Shachar Ovadia August 30, 2021

Hi,

As expected i got the Created subtask key, and the parent issue key

2021-08-30 18:04:36,508 WARN [runner.ScriptBindingsManager]: CAP-238 (Parent)

2021-08-30 18:04:36,509 WARN [runner.ScriptBindingsManager]: CAP-543 (sub task)

Juan José Marchal Gómez
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.
August 31, 2021

Hi,

Fine.

There is other solution.

 

Could you use the following?

 

Other solution is using linkManager:

linkManager.createIssueLink(parent.getId(), subtaskIssue.getId(),(Long)10300,(Long)1,user)


Here 10300 is "subtask link type (in my instance), check yours".

 

Best regards.

Shachar Ovadia August 31, 2021

Hey,

i have tried this solution as well,

There is some thing strange while creating this sub-task - 

I'm Attaching an image, which displays two issues from the same issuetype-

The first one has been created from the WF Transition, 

The second one has been Created from the Create subtask jira button from the parent issue itself, and this one is linked to the subtask, the first one is not.

ללא שם.pngI appriciate all of your help ! 

Juan José Marchal Gómez
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.
August 31, 2021

Hello,

ok...I know what is the problem now.

Null object appears because you have "other field" with null information during this action and the system is failing.

Actually is not failing this code, is failing other related code created in the past.

For example, imagine that you have a listener that assign a customFieldValue to other customFieldValue when the issue is updated.

When you are linking the task with the subtask you are "updating" the issue. If the first customField is without information, you can not assign null information.

So, my recomendation is that you check all the automations that you have related to this issuetypes (mainly listeners or other related creations) and try to focus the problem disabling all that you can to check it again.

Make sense?

Best regards!

Like Shachar Ovadia likes this
Shachar Ovadia August 31, 2021

Amazing, 

I had a behaviour and a listener that was calcuating some values,

i moved them to a scripted field and it solved, you were right,

Much appreciated !!

Juan José Marchal Gómez
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.
August 31, 2021

Please, could you set as "resolved".

Thanks!

Suggest an answer

Log in or Sign up to answer