I have a groovy script that performs the following in a workflow post function:
The script works however I now want to copy the value of two custom fields into the linked issues on create. One is a Select List (Single Choice) and the other is a Date Picker. I am trying to use the addCustomFieldValue method but it seems to be configured for a String. Any help would be much appreciated.
Code (I have marked up the lines causing the problem with /* comments):
// Required library imports
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.jira.issue.context.IssueContext
import com.atlassian.jira.issue.context.IssueContextImpl
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.config.manager.PrioritySchemeManager
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.riadalabs.jira.plugins.insight.services.model.ObjectBean
import customRiadaLibraries.insightmanager.InsightManagerForScriptrunner
import org.apache.log4j.Level
import org.apache.log4j.Logger
// This annotation allows the import of com.riadalabs classes
@WithPlugin('com.riadalabs.jira.plugins.insight') insightPlugin
issue = issue as MutableIssue //this helps with IDE autocomplete and Script Editor status type checking
def mylog = Logger.getLogger("com.acme.workflows")
mylog.setLevel(Level.DEBUG)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customFieldInsightA = customFieldManager.getCustomFieldObject(12001)
def CusValueA = issue.getCustomFieldValue(customFieldInsightA) as List<ObjectBean>
def customFieldInsightB = customFieldManager.getCustomFieldObject(11801)
def cfDataRequired = customFieldManager.getCustomFieldObject(12100)
def cfDataRequiredValue = issue.getCustomFieldValue(cfDataRequired)
def cfExitDate = customFieldManager.getCustomFieldObject(12002)
def cfExitDateValue = issue.getCustomFieldValue(cfExitDate)
def issueService = ComponentAccessor.issueService
def constantsManager = ComponentAccessor.constantsManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def prioritySchemeManager = ComponentAccessor.getComponent(PrioritySchemeManager)
//eachWithIndex iterates through following code for all Custom Field values creating the linked issues
CusValueA.eachWithIndex { item, index ->
mylog.info("item="+item)
mylog.info("index="+index)
// the project key under which the issue will get created
final projectKey = 'ABC'
// the issue type for the new issue
final issueTypeName = 'Task'
// the priority of the new issue
final priorityName = 'Medium'
def project = ComponentAccessor.projectManager.getProjectObjByKey(projectKey)
assert project : "Could not find project with key $projectKey"
def issueType = constantsManager.allIssueTypeObjects.findByName(issueTypeName)
assert issueType : "Could not find issue type with name $issueTypeName"
// get the reporter of the current issue
def reporter = issue.getReporter()
// get the current user
def currentUser = loggedInUser
// if we cannot find the priority with the given name or if this is null, then set the default priority
def issueContext = new IssueContextImpl(project, issueType) as IssueContext
def priorityId = constantsManager.priorities.findByName(priorityName)?.id ?: prioritySchemeManager.getDefaultOption(issueContext)
def issueInputParameters = issueService.newIssueInputParameters().with {
setProjectId(project.id)
setIssueTypeId(issueType.id)
setReporterId(loggedInUser.username) // set the reporter of the sub-task as the current user
setSummary("Customer Decommissioning: " + item.label)
setPriorityId(priorityId)
addCustomFieldValue(customFieldInsightB.idAsLong, item.objectKey) // add the current item (insight object) to the field in the issue
addCustomFieldValue(cfDataRequired.getId(), cfDataRequiredValue) /* This line of code causes the script to become invalid, I want to set the custom field value for Select List Single Choice in child same as parent*/
addCustomFieldValue(cfExitDate.getId(), cfExitDateValue) /* This line of code causes the script to become invalid, I want to set the custom field value for Date Picker in child same as parent*/
}
def validationResult = issueService.validateCreate(loggedInUser, issueInputParameters)
assert validationResult.valid : validationResult.errorCollection
def result = issueService.create(loggedInUser, validationResult)
assert result.valid : result.errorCollection
// the name of the issue link
final String issueLinkName = "Relates"
// the sequence of the link
final Long sequence = 1L
def issueLinkTypeManager = ComponentAccessor.getComponent(IssueLinkTypeManager)
def availableIssueLinkTypes = issueLinkTypeManager.issueLinkTypes
def linkType = availableIssueLinkTypes.findByName(issueLinkName)
assert linkType : "Could not find link type with name $issueLinkName. Available issue link types are ${availableIssueLinkTypes*.name.join(", ")}"
def childTask = result.issue
ComponentAccessor.issueLinkManager.createIssueLink(issue.id, childTask.id, linkType.id, sequence, loggedInUser)
}
mylog.info("Field Value= "+ CusValueA)
For anyone interested in this, as a temporary fix I have used two scriptrunner post functions (copy field) positioned after the above custom script post function to reach the same end. Although I would find it preferable for all the actions associated with the create issue to be contained in the script :)
@Tom Brown, @Amr Hamza (Legacy) I solved the problem by using the following code snipped
issueInputParameters.addCustomFieldValue( cfOption.getId(), String.valueOf(option.getOptionId()) )
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Tom Brown I have been struggling with the same problem for the past couple of hours and I was about to give up when I did a last attempt which worked to my surprise.
So even though the error message, when setting a select list custom field, indicates that allowed options should be optionId[optionName], the only way it is passing the optionId only as String
Just wanted to share in case you and/or anyone else are still interested.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.