Forums

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

ScriptRunner create sub-task with custom field failed

Priska Aprilia
Contributor
October 23, 2019

Hi,

I am trying to create a sub-task with custom fields.
I can create sub-task with system fields with the following code. However, whenever I add the line of code for setting custom field, an error is returned like below. Note that Cluster is a textfield.

No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.setCustomFieldValue() is applicable for argument types: (com.atlassian.jira.issue.fields.ImmutableCustomField, java.lang.String) values: [Cluster, aaaa]

And this is my code. I have tried to look online to find a solution. You can see, I did try another method in the commented codes but to no avail. Please help. Thanks

import com.atlassian.jira.component.ComponentAccessor
import groovy.json.JsonSlurper
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParameters.addCustomFieldValue.*
import com.atlassian.jira.issue.fields.CustomField;

def cfm = ComponentAccessor.customFieldManager
def issueKey = "CSDA-47"
def issueManager = ComponentAccessor.getIssueManager()
def issueObject = issueManager.getIssueByCurrentKey("CSDA-47")
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def authenticationContext = ComponentAccessor.getJiraAuthenticationContext();

import com.atlassian.jira.component.ComponentAccessor

// the issue key of the parent issue
final String parentIssueKey = "CSDA-47"

// the issue type for the new issue - should be of type subtask
final String issueTypeName = "VM Sub-task"

// user with that user key will be the reporter of the issue
final String reporterKey = "anuser"

// the summary of the new issue
final String summary = "Groovy Friday"

def issueService = ComponentAccessor.issueService
def constantsManager = ComponentAccessor.constantsManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def parentIssue = ComponentAccessor.issueManager.getIssueByCurrentKey(parentIssueKey)
assert parentIssue : "Could not find parent issue with key $parentIssueKey"

def cVMDetails = customFieldManager.getCustomFieldObject("customfield_10401")
assert parentIssue : "Could not find parent issue with id $customfield_10401"
def cVMDetailsValue = parentIssue.getCustomFieldValue(cVMDetails)

def subtaskIssueTypes = constantsManager.allIssueTypeObjects.findAll { it.subTask }
def subTaskIssueType = subtaskIssueTypes.findByName(issueTypeName)
assert subTaskIssueType : "Could not find subtask issue type with name $issueTypeName. Avaliable subtask issue types are ${subtaskIssueTypes*.name.join(", ")}"

def jsonSlurper = new JsonSlurper()
def json_object = jsonSlurper.parseText(cVMDetailsValue.toString())
assert json_object instanceof Map : "Not a JSON"
int size = json_object.rows.size()
// if we cannot find user with the specified key or this is null, then set as a reporter the logged in user
def reporter = ComponentAccessor.userManager.getUserByKey(reporterKey) ?: loggedInUser
for(int i;i<size;i++){
def clusterObject = customFieldManager.getCustomFieldObjectByName("Cluster")
def issueInputParameters = issueService.newIssueInputParameters().with {
setProjectId(parentIssue.projectObject.id)
setIssueTypeId(subTaskIssueType.id)
setReporterId(reporter.key)
setSummary(summary)
setCustomFieldValue(clusterObject,"aaaa")
}

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
ComponentAccessor.subTaskManager.createSubTaskIssueLink(parentIssue, subtask, loggedInUser)

def user = authenticationContext.getLoggedInUser()
def issueFactory = ComponentAccessor.getIssueFactory()

/*
def test=""


test+=i
//create new issue as sub task
def newSubtask = issueFactory.getIssue()
newSubtask.summary = "new subtask"+i;
def network_tier = json_object.rows[i].columns.network_tier
//def network_tier = json_object.rows[i].columns.network_tier
newSubtask.description = "Network Tier: " + network_tier
//cluster
def clusterObject = customFieldManager.getCustomFieldObject("customfield_10501")
newSubtask.setCustomFieldValue(clusterObject,"aaaa")
newSubtask.setProjectId(issueObject.getProjectId())
newSubtask.setIssueTypeId("10300") //issue type id must be subtask type's id
newSubtask.setParentId(issueObject.getId())
//newSubtask.setAssigneeId("user1")
newSubtask.setReporterId(issueObject.getReporterId())
newSubtask = issueManager.createIssueObject(user, newSubtask)

//link subtask to parent task
def subTaskManager = ComponentAccessor.getSubTaskManager()
subTaskManager.createSubTaskIssueLink(issueObject, newSubtask, user)

*/
}

 

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Antoine Berry
Community Champion
October 24, 2019

Hi @Priska Aprilia , 

Looking at your logs and at the javadoc, it seems that you should use addCustomFieldValue instead of setCustomFieldValue.

Antoine

TAGS
AUG Leaders

Atlassian Community Events