In the ScriptRunner console, I am writing a script to copy a custom field value from parent to child issues. This is my code:
import com.atlassian.jira.util.ComponentLocator
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.UpdateIssueRequest
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.issue.link.IssueLinkManager;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
ChangeHistoryManager changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def issue = event.issue
def changeHistory = changeHistoryManager.getAllChangeItems(issue).each{
}
def chHistory = changeHistory[-1].field.toString()
final cf = 'customfieldname'
def change = event.changeLog.getRelated('ChildChangeItem').find { it.field == cf }
if (!change) {
return
}
def linkedIssues = ComponentAccessor.issueLinkManager.getOutwardLinks(issue.id).findAll {it.issueLinkType.name in ['Epic-Story Link'] }
def customField = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue)?.find {it.name == cf}
def nV = change.newstring
linkedIssues.each{
def linkedIssue = it.destinationObject
def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
linkedIssue.setCustomFieldValue(customField, user)
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
}
I get an error when calling setCustomFieldValue(). How do I fix this code to make it work?
Are you trying to do this for child issues or linked issues?
If you want to copy the value of a custom field from the parent issue to the child issue, you could try something like this:-
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
def issueManager = ComponentAccessor.issueManager
def customFieldManager = ComponentAccessor.customFieldManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def issue = issueManager.getIssueByCurrentKey("MOCK-1")
def subTasks = issue.subTaskObjects
def sampleText = customFieldManager.getCustomFieldObjectsByName("Sample Text")[0]
subTasks.each {
def subTask = it as MutableIssue
subTask.setCustomFieldValue(sampleText, issue.getCustomFieldValue(sampleText))
issueManager.updateIssue(loggedInUser,subTask, EventDispatchOption.DO_NOT_DISPATCH,false)
}
Please note, the sample code provided is not 100% exact to your environment. Hence, you will need to make the required modifications.
Below are some print screens of the test:-
1) The parent issue with the value for the custom field:-
2) The child issue without any value for the custom field:-
3) Executing code in on the script console:-
4) After executing, the child issue is updated with the same value of the custom field as the parent issue:-
Alternatively, if you want to copy the value to linked issues, below is a sample code you can try:-
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
def issueManager = ComponentAccessor.issueManager
def customFieldManager = ComponentAccessor.customFieldManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def issueLinkManager = ComponentAccessor.issueLinkManager
def issue = issueManager.getIssueByCurrentKey("MOCK-1")
def sampleText = customFieldManager.getCustomFieldObjectsByName("Sample Text")[0]
def linkedIssue = issueLinkManager.getInwardLinks(issue.id)
linkedIssue.each {
def sourceIssue = it.sourceObject as MutableIssue
sourceIssue.setCustomFieldValue(sampleText, issue.getCustomFieldValue(sampleText))
issueManager.updateIssue(loggedInUser,sourceIssue, EventDispatchOption.DO_NOT_DISPATCH,false)
}
Please note, this sample code is not 100% exact to your environment. Hence, you will need to make the required modifications.
In this case, the source issue (MOCK-1) is using the is blocked by link type. The destination issue i.e. MOCK-3 is using the blocks link type.
Below are a few test steps.
1) In issue MOCK-3 which blocks issue MOCK-1, no custom field value is added. MOCK-1 is the same as per the example above and already has value for the custom field.
2) Below is a print screen of the script console:-
3) After executing the script on the script console below is the updated custom field on the linked issue:-
I hope this helps to answer your question. :)
Thank you and Kind Regards,
Ram
Hi Ram,
Thanks for your response, I tried the changes and it worked.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Great to hear the solution worked for you. :)
Could you please accept the answer?
Thank you and Kind Regards,
Ram
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.