JIRA Update Issue Custom Field from cascading select

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

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.

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

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)

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.

 

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

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.  

 

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]

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)
}

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)

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.

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)

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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

82 views 0 5
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you