retrieve custom field values with array

Junaid Shah
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.
January 14, 2016

Hi,

I have the script below

 

CustomField request_type1 = customFieldManager.getCustomFieldObjectByName('Cabling')
CustomField request_type2 = customFieldManager.getCustomFieldObjectByName('Airport')
CustomField request_type3 = customFieldManager.getCustomFieldObjectByName('ID')

String[] list1 = [request_type1, request_type2, request_type3]
String[] list2

for(int i = 0; i < list1.length; i++){
list2[i] = targetIssue.getCustomFieldValue(list1[i])
}

for(int i = 0; i < list1.length; i++){
issue.setCustomFieldValue(list1[i], list2[i])
}

But I am getting the error..

groovy.lang.MissingMethodException: No signature of method: 
com.atlassian.jira.issue.IssueImp1.getCustomFieldValue() is applicable 
for argument types: (java.lang.String) values: [Cabling]

 

This is being run as a post function using scripted post function. Create a subtask.

Any help greatly appreciated...

4 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
Jeff Louwerse
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.
January 14, 2016

IMO you would be far better off creating a list of field names and then looping over that.  I didn't test this code but I use something similar all the time. 

def myFieldList = ["Cabling","Airport","Id"];
for (myFieldName in myFieldList )
{   def tempCF = customFieldManager.getCustomFieldObjectByName(myFieldName );
	def tempVAL = targetIssue.getCustomFieldValue(tempCF );
	issue.setCustomFieldValue(tempCF , tempVAL )
}

 

 

Junaid Shah
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.
January 14, 2016

Thanks! I'll give this a try.

Junaid Shah
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.
January 15, 2016

I can confirm with a bit of tweaking this works smile Thank you.

Jeff Louwerse
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.
January 15, 2016

Great.  I was about to comment that you mentioned bringing values over from a different issue and different projects... this can get troublesome for select lists unless both projects use the same field context.  So just keep that in mind should you start to copy drop down lists.  You might have to start retrieving the option from from one issue, pulling out the text value (hope that both contexts have that text value!) and then looking up that text value in the other project to get the new option.

Junaid Shah
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.
January 15, 2016

Luckily they're using the same field contexts so not having an issue, but I did realise that field configurations have to be set to "Global", even if both projects you're working on are defined. I'll keep your comment in mind though however! Thanks.

 

1 vote
Thanos Batagiannis [Adaptavist]
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.
January 14, 2016

Sy,

Assuming that the Cabling is a custom field of type Select List (which return to you a list) the way to get the values are

def request_type1 = customFieldManager.getCustomFieldObjectByName('Cabling')
def listOfValues = targetIssue.getCustomFieldValue(request_type1)

or

cfValues['Cabling']?.value
Junaid Shah
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.
January 14, 2016

Hi, Cabling is a text field, and not a select list, all I am trying to accomplish is moving the data of Field A from one issue to another.

Thanos Batagiannis [Adaptavist]
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.
January 14, 2016

Can I ask how you retrieve the targetIssue ?

Thanos Batagiannis [Adaptavist]
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.
January 14, 2016

Also try to replace the String[] with def

Junaid Shah
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.
January 14, 2016

I have the target issue stored in a text field which is def cf = cfValues['Target Issue'] then.. //More Definitions def issueManager = ComponentAccessor.getIssueManager() //Retrieving Target Issue def targetIssue = issueManager.getIssueObject(cf)

Thanos Batagiannis [Adaptavist]
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.
January 14, 2016

As both Nic and Gabrielle mentioned the issue is with the definition, try to replace with def instead of types declaration.

Junaid Shah
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.
January 15, 2016

thanks thanos this helps with the select list retrieval.

0 votes
GabrielleJ
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.
January 14, 2016

As I see it list1 is an array of "Custom Field", which is not a String hence the error.

You should get the CustomField name (which is a String) first.

Reference: (https://docs.atlassian.com/jira/latest/com/atlassian/jira/issue/fields/CustomField.html#getFieldName--)

Junaid Shah
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.
January 14, 2016

Hi, Do you mean something like String getFieldName('Cabling')? etc?

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 14, 2016

I suspect that your fields are select list type fields?  They take/return "options", not strings, so you need to convert the string into an option

Junaid Shah
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.
January 14, 2016

Hi, The issue isn't because of a select list type (even though its a text box), this is because if I do something like: CustomField request_type1 = customFieldManager.getCustomFieldObjectByName('Cabling') def request1_value = targetIssue.getCustomFieldValue(request1) issue.setCustomFieldValue(request_type1, request_value1) then all works fine, whether it is a text box.. multi check box.. etc. I assume it is something to do with my array declaration more of, but not sure

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 14, 2016

oh, hang on, your original code is taking a customfield object and wedging it into an array of strings. That's never going to work. This new code works fine because you're getting the value of the string held in the Cabling field and then setting it back. Which seems somewhat pointless, but it's using the right objects, so it's a start. I'm confused as what you're trying to do now!

Junaid Shah
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.
January 14, 2016

Basically I'm trying to create a better performing code, the above works fine for me to add everything line by line, but I'm trying to reduce the code so that instead of having multiple: targetIssue.getcustomFieldValue issue.setcustomFieldValue I can instead have two loops that do this instead. My coding isn't the greatest, so would you know how I could get around this issue? I'm not sure as to what to do next, as I've tried declaring it as a Object: Object[] list1 = new list1 [......] But doesn't like it. Thanks.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 14, 2016

Sorry, I meant the goal of the script - at the moment, it looks like the aim is to take a value from a custom field and put the same value back into the same custom field. So the optimal code would really be "do nothing"

Junaid Shah
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.
January 14, 2016

Haha.. Yes if only "Do nothing" was an option - Its basically taking field values from a different issue in a different project and bringing it into this subtask.

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events