Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Error Using setCustomFieldValue

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?

 

1 answer

1 accepted

Hi @Uday Kiran Raparthy,

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:-

parent.png

2) The child issue without any value for the custom field:-

child_issue.png

3) Executing code in on the script console:-

script_console.png

4) After executing, the child issue is updated with the same value of the custom field as the parent issue:-

updated_child.png

 

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.

blocks_link.png

2) Below is a print screen of the script console:-script_console2.png

3) After executing the script on the script console below is the updated custom field on the linked issue:-

linked_issue_update.png

 

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.

Hi @Uday Kiran Raparthy,

Great to hear the solution worked for you. :)

Could you please accept the answer?

Thank you and Kind Regards,
Ram

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you