JIRA Update Issue Custom Field from cascading select

Robert G. Nadon
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.
May 24, 2016

Hi all,

I hope someone can help.  Here is what we are trying to accomplish.   I was asked to make the field Components selectable based upon a custom field value called product.  

Here was my proposal:

  1. Add a new cascading select field called product-component  with product as the first choice and then the various component choices
  2. In a post function copy the second of the cascading list component to components
  3. in another post function copy the top value of the cascading list to product

Everything works except 3.    I have tried a basic copy, then I tried Update Issue Custom Field with the following values:

Try2: ${issue.customfield_13502.toString()}

Try3: ${issue.cfValues['product-component']*.value} or ${issue.cfValues['customfield_13502']*.value}

Try4: ${issue.getCustomFieldValue(getCustomFieldObjectByName("customfield_13502"))}

Try5: ${issue.getCustomFieldValue("customfield_13502")}

try 6: ${issue.getCustomFieldValue(customfield_13502)} or $issue.getCustomFieldValue(product-component)

try 7: ${issue.product-component} or ${issue.customfield_13502:1}

None of which worked

Does anyone know how I could get the value from the top part of a cascading select list called product-component (customfield_13502) into another select list custom field?  I will ensure that the first part of product-component has exact values of the customer single select list called product.

Thanks!!!

Robert

3 answers

1 accepted

2 votes
Answer accepted
David _old account_
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.
June 3, 2016

You can instead use JMWE's "Set Field Value to Constant or Groovy Expression" post-function with a simple Groovy script:

import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;
CustomField customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("<name of your cascading field>");
def value = issueObject.getCustomFieldValue(customField);
return value?.get(null)?.getValue();

I didn't test this code so there might be an error in it but you get the gist.

Note that the script uses the "issueObject" variable, which is an instance of JIRA's Issue interface.

Robert G. Nadon
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.
June 3, 2016

Wow that did it!!!   THANK YOU!!!   We are good the exact script above (with the obvious replacement of the name of the cascading field worked perfectly.  Case closed

Sandra Meessen February 21, 2019

Hi @David _old account_ , I want to use your code (I'm completely new to Groovy) for solving my problem:

I have cascading custom field and 2 single custom fields. I want to copy the parent part of the cascading to one custom field and the child part to the other custom field. I followed the steps of initiator Robert and succeeded in copying the child part to the other custom field. 

For the parent I want to use your code, but I don't understand where to put the name of the cascading (parent) and where the name of the custom field copying to. Can you help me please? Thanks! 

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 21, 2019

Hi Sandra,

 which app are you using (JMWE or ScriptRuner)?

Sandra Meessen March 19, 2019

Hi @David Fischer, We have scriptrunner, but I have no experience in coding (groovy?). We don't have JMWE. Thank you.

Sandra Meessen April 25, 2019

Hi @David Fischer [Innovalog], could you help me on my question? I want to copy the parent part of a cascading field to a custom field. I don't have JMWE, but use the Scriptrunner post function and copied your piece of coding you mentioned earlier and filled in the name of my cascading field:

import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;
CustomField customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Projectcode for Hosting");
def value = issueObject.getCustomFieldValue(customField);
return value?.get(null)?.getValue();

 It gives me an error on the 4th and 5th line of coding saying "The variable [issueObject] is undeclared". My fieldname is correct. It would help me a lot.

Thank you and kind regards,

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 25, 2019

@Sandra Meessen unfortunately, I have zero experience with ScriptRunner. But my guess is that you just need to replace "issueObject" with "issue".

1 vote
Vijay Khacharia
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.
May 25, 2016

Each option in JIRA customfield has an optionID. You have two fields product-component and product, both are select lists. So you to get the correct option ID for product field and set the value to that. Here you are trying to set the same value which wont work, as it is option for product-component field.

For getting the option for the field you can use 

customField.getRelevantConfig(issue)).getOptionForValue("option", null)
Robert G. Nadon
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.
May 26, 2016

Thanks Vijay! I appreciate the help... What post function should I use with the above?  

  • Set Field Value to constant or Groovy expression
  • Update Issue Custom Field
  • Copy Value From Field to Field

Actually product-component is a cascading select and product is a regular select. The above "option" is the product-component ID.  IE product-component is also customfield_13502, going into each option also give me an ID, but that is what I am looking for. so here is what product-component returns: linux-gui, linux-installation, windows-interface, windows-installation, windows-bsod, etc.  What I am looking put in the product field is linux or windows. As product is a single select list with Linux and Windows as the options.

So is the "option" above the option ID from the customfield (13502) or the option ID (the ID of linux-interface for example)?

Excuse my novelty, but I am trying to write a post function, not a plugin, though one day I hope to get there.

 

Vijay Khacharia
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.
May 26, 2016

I would use a script post function and put a groovy script to do this.

  • Get the value for "Product-component" field.
  • Check if it is "Linux-interface" or "Linux-Installation"
    • Look for the optionID for value "Linux" for the field "Product"

      cfProduct.getRelevantConfig(issue)).getOptionForValue("Linux", null)
  • If it is "Windows-Interface" or "Windows-installation", etc.
    • Look for optionID for value "Windows" for the field "Product"
  • Set the value of Product field
  • Update the issue

Below answer will be helpful if you are new to groovy.

https://answers.atlassian.com/questions/189262

Robert G. Nadon
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.
May 26, 2016

I believe I understand the confusion here.  I am first just trying to return the value of Product-component to a text field for now, as I cannot even seem to get the value of the multiselect field.  If I can get the value of Product-component, setting the field, I think, will be the easy part.  

 

Robert G. Nadon
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.
May 26, 2016

What my question should be I guess is how do I print a string of the parent value of a cascading select list.  Here is my sample code I have tried many different samples found on the web/answers none work.

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.customfields.view.CustomFieldParams
import com.atlassian.jira.issue.customfields.option.Option

Issue issue = issue;

ComponentManager componentManager = ComponentManager.getInstance();

CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();

CustomField customField = componentManager.getCustomFieldManager().getCustomFieldObjectByName("product-component");

Object productValue = issue.getCustomFieldValue(customField)?.getValue();

CustomFieldParams params = (CustomFieldParams) productValue;
if (params != null) {
Object parent = params.getFirstValueForNullKey();
Object child = params.getFirstValueForKey("1");
}

String retVal = parent.toString();
return retVal

Here is the error I usually get:

Error while executing SetFieldValueFunction: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'com.innovalog.jmwe.IssueProxy@19c983b3' with class 'com.innovalog.jmwe.IssueProxy' to class 'com.atlassian.jira.issue.Issue'

or

/secure/QuickCreateIssue.jspa [jmwe.plugins.functions.SetFieldValueFunction] Error while executing SetFieldValueFunction: groovy.lang.MissingMethodException: No signature of method: com.innovalog.jmwe.IssueProxy.getCustomFieldValue() is applicable for argument types: (com.atlassian.jira.issue.fields.CustomFieldImpl) values: [product-component]

Vijay Khacharia
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.
May 26, 2016

Below script works. It gives you parent and child as option. You can compare the parent option and then get the valid optionId for the "Product" field

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.impl.CascadingSelectCFType
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
Issue issue = ComponentAccessor.getIssueManager().getIssueObject("DELDEFECT-3922");
CustomField customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Cascade");
Map<String, Option> params = (HashMap<String,Option>) issue.getCustomFieldValue(customField)
if (params != null) {
    Option parent = params.get(CascadingSelectCFType.PARENT_KEY)
    Option child = params.get(CascadingSelectCFType.CHILD_KEY)
}
Robert G. Nadon
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.
May 27, 2016

Thank you so much for your help this is really got me beating my head.  It does not seem to like getCustomFieldValue for product-component.  This was in the JIRA logs:

/secure/CreateIssueDetails.jspa [jmwe.plugins.functions.SetFieldValueFunction] Error while executing SetFieldValueFunction: groovy.lang.MissingMethodException: No signature of method: com.innovalog.jmwe.IssueProxy.getCustomFieldValue() is applicable for argument types: (com.atlassian.jira.issue.fields.CustomFieldImpl) values: [product-component]
groovy.lang.MissingMethodException: No signature of method: com.innovalog.jmwe.IssueProxy.getCustomFieldValue() is applicable for argument types: (com.atlassian.jira.issue.fields.CustomFieldImpl) values: [product-component]

Here is the code I put in:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.impl.CascadingSelectCFType
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
Issue issue = ComponentAccessor.getIssueManager().getIssueObject("SA-991");
CustomField customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("project-component");
Map<String, Option> params = (HashMap<String,Option>) issue.getCustomFieldValue(customField)
if (params != null) {
    Option parent = params.get(CascadingSelectCFType.PARENT_KEY)
    Option child = params.get(CascadingSelectCFType.CHILD_KEY)
}

I am running JIRA 6.4.7, hopefully I don't need to upgrade to 7 as that is scheduled for the summer of this year.  Should be running the latest of MISC Workflow extensions (4.0.4) and JIRA Misc Custom Fields (1.6.4)

Vijay Khacharia
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.
May 27, 2016

I dint try to use the Misc workflow extensions. I mostly use Script Runner from Adaptavist and use Script Postfunction as a postfunction. 

I see in documentation the actual issue on which you put the post function is available by name "issueObject". Issue is more a generic value. So if you use issue, you should use something like issue.get("product-component")

 

And yes you dont need to upgrade to 7 for it to work.

Robert G. Nadon
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.
May 27, 2016

I tried Script Runner use a script as a post function and get the identical error.  I will try to do this on the production server on a dummy project just to make sure it is not the sandbox.  On the production I get this error.  And it only has script runner installed....

/secure/CreateIssueDetails.jspa [scriptrunner.jira.workflow.ScriptWorkflowFunction] Script function failed on issue: null, actionId: 1, file: <inline script>
java.lang.NullPointerException
at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:1095)
at com.atlassian.jira.issue.Issue$getCustomFieldValue$4.call(Unknown Source)
at Script29.run(Script29.groovy:8)

0 votes
Robert G. Nadon
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.
June 1, 2016

Looks like the only answer I can get to work is this https://innovalog.atlassian.net/browse/JMWE-178 which allows me to create a Calculated Text field which will display the parent, and see that instead of a post function copy.    That answer is less than ideal as then the cascading select and the components can be out of sync if someone edits the component, but it is the best I can get working.  Thanks @Vijay Khacharia for all your help, but I get errors with all the recommended groovy scripts.  (possibly as I am still using the last free version of script runner)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events