Forums

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

Copy parent custom field. Need help with this code

KRC
Contributor
March 7, 2018

I use the following code to copy parent description to a scripted field called Parent Description. I need the same thing but now i want to copy a user picker customfield_14468 to scripted filed called PM. How can i call/define parents customfield just like i called parents description in below code? Any thoughts?

I put this code in the scripted field.

Thanks

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;
if (issue.getProjectObject().getKey() == 'MCO')
{
def field =ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Parent description")
def newfield = issue.getParentObject().description;
return newfield;
}

1 answer

1 accepted

0 votes
Answer accepted
Ivan Tovbin
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.
March 7, 2018

You can do it like so:

def parentCf = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(14468)
return issue.getParentObject().getCustomFieldValue(parentCf)

Just make sure your scripted field is using a proper template and searcher if you want the data to be displayed correctly. 

KRC
Contributor
March 7, 2018

Hi @Ivan Tovbin

I replaced with your suggested code and i get the error as attached.

I chose the template as user picker single

When u say searcher, what are you referring to?

I want the code to work so that parents custom field value will be copied over to a scripted field. So whenever i update parent, the updates will flow thru the new scripted field. it was successful when i copied parent description and summary

Untitled.png

KRC
Contributor
March 7, 2018

I modified code a little to this. User picker from parent shows a user and this new scripted filed is suppose to show the same user. instead it shows as anonymous. please see attched screenshot123.jpg

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.user.ApplicationUsers
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;
if (issue.getProjectObject().getKey() == 'MCO')
{
def field =ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Program Manager.")
def parentCf = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_14468")
return parentCf
}

Ivan Tovbin
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.
March 7, 2018

Yeah, Scriptrunner can be very annoying at times with its Static Type Checking not always being accurate. In this case I'm quite certain you can pretty much ignore this error. Or you can change: 

getCustomFieldObject(14468)

 to

getCustomFieldObject(14468.toLong())

and this should make this error go away.

By searcher I mean a search template which is being used to to index and searth this field. For a user picker type custom field make sure the search template is set to 'User Picker & Group Searcher'

Ivan Tovbin
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.
March 7, 2018

Yep, your user picker data is rendered as 'Anonimous' because an incorrect search template is set for this field. Check the above comment to fix it.

Ivan Tovbin
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.
March 7, 2018

Also, your code is wrong, because it returns a CustomField object instead of its value for a given issue. Please try the code I provided and see if it works.

KRC
Contributor
March 7, 2018

Thank you for the inputs. Will try and let you know

KRC
Contributor
March 9, 2018

It works. Thank you for sharing your knowledge

Ivan Tovbin
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.
March 9, 2018

@KRC Great news! Please mark this solution as "Accepted" if it resolved your issue.

Suggest an answer

Log in or Sign up to answer