Set customfield value (Free text) based on cascading select

Hello, 

Using in post function, i would like to use the cascading field, and if both is matching, i would like to set a customfield value?

is there a way to do this?

i tried by the following code: 

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.view.CustomFieldParams
import com.atlassian.jira.issue.fields.CustomField
ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
MutableIssue issue = componentManager.getIssueManager().getIssueObject('TP-7')
def stringval = "123"
if (cfValues['Select Email Template']?.values()*.value == ['Privacy', 'ID Check'])
		issue.setCustomFieldValue(customFieldManager.getCustomFieldObject(10806), stringval)
		else null

 

But doesnt seem to work, any solution?

Many thanks for your help, 

Pon

 

1 answer

1 accepted

1 vote
Vasiliy Zverev Community Champion Dec 08, 2016

For cascade fields HashMap<String, String> is returned. Try this code

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue

MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject('TP-7')
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

HashMap&lt;String, String &gt; value = (HashMap&lt;Object, Object&gt;) issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Select Email Template"));

if((value.get(null).equals("Privacy") &amp;&amp; (value.get("1").equals("ID Check")))){
    issue.setCustomFieldValue(customFieldManager.getCustomFieldObject(10806), stringval)
}

@Vasiliy Zverev Thanks you so much!

 

I placed the below code into postfunction but the customfield 10806 is not populating, am i doing something wrong?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
HashMap&lt;String, String &gt; value = (HashMap&lt;Object, Object&gt;) issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Select Email Template"));
if((value.get(null).equals("Privacy") &amp;&amp; (value.get("1").equals("ID Check")))){
    issue.setCustomFieldValue(customFieldManager.getCustomFieldObject(10806), "123")
}

 

using JIRA 6.3.3

 

Many thanks, 

Pon

Vasiliy Zverev Community Champion Dec 08, 2016

Try to debug it using Script console.

Thanks @Vasiliy Zverev. i ran it by scrip console, seems to be fine (no errors) but when i run it in post function, comment // on mutable issue, email body isnt populate, is there a way i should try to debug this?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
 
//MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject('TEST-422')
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
 
HashMap&lt;String, String &gt; value = (HashMap&lt;Object, Object&gt;) issue.getCustomFieldValue(customFieldManager.getCustomFieldObject(11401));
def stringval = "123"
if((value.get(null).equals("Privacy") &amp;&amp; (value.get("1").equals("ID Check")))){
    issue.setCustomFieldValue(customFieldManager.getCustomFieldObject(10806), stringval)
}

Not sure if its how im declaring my string?

Should i be debugging this somehow?

Any suggestion please, many many thanks.

Pon

Steven Behnke Community Champion Dec 08, 2016

What type of field is customField_10806?

Hey @Steven Behnke,

Thanks for your interests, customfield_10806 is a free text (Multi-line) field.

Really not sure whats causing it not to apply the value string

Many thanks,

Pon

Vasiliy Zverev Community Champion Dec 09, 2016

To store changes into a database it is requred to call IssueManager.updateIssue() method.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.UpdateIssueRequest

//MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject('TEST-422')
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

HashMap&lt;String, String &gt; value = (HashMap&lt;Object, Object&gt;) issue.getCustomFieldValue(customFieldManager.getCustomFieldObject(11401));
def stringval = "123"
if((value.get(null).equals("Privacy") &amp;&amp; (value.get("1").equals("ID Check")))){
    issue.setCustomFieldValue(customFieldManager.getCustomFieldObject(10806), stringval)
    ComponentAccessor.getIssueManager().updateIssue(
            ComponentAccessor.getJiraAuthenticationContext().getUser()
            , issue
            , UpdateIssueRequest.builder().eventDispatchOption(EventDispatchOption.ISSUE_UPDATED).sendMail(false).build()
    )
}

Thank you so much for your reply @Vasiliy Zverev

Unfortunately no luck, customfield (11401) "Email Body" still not updating to the string value

I've basically copied the code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.UpdateIssueRequest

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
 
HashMap&lt;String, String &gt; value = (HashMap&lt;Object, Object&gt;) issue.getCustomFieldValue(customFieldManager.getCustomFieldObject(11401));
def stringval = "Hello test"
if((value.get(null).equals("Privacy") &amp;&amp; (value.get("1").equals("ID Check")))){
    issue.setCustomFieldValue(customFieldManager.getCustomFieldObject(10806), stringval)
    ComponentAccessor.getIssueManager().updateIssue(
            ComponentAccessor.getJiraAuthenticationContext().getUser()
            , issue
            , UpdateIssueRequest.builder().eventDispatchOption(EventDispatchOption.ISSUE_UPDATED).sendMail(false).build()
    )
}

and placed it as the last operation also (I've tried changes the orders as well) still no luck.

Not sure if its me, but this is the cascading field below; 

select_template.JPG

I really dont know where its going wrong. 

I thank you for helping me this far so much. 

 

Regards, 

Pon

Vasiliy Zverev Community Champion Dec 10, 2016

It seems that wrong customfild id into 

 issue.setCustomFieldValue(customFieldManager.getCustomFieldObject(10806), stringval)

Replace it with correct one

Hey @Vasiliy Zverev

I've tried another customfield id, and it doesn't seem to work with that also.

I've also tried with:

customFieldManager.getCustomFieldObjectByName("Email Body"))also doesn't work.

Do you think there is a alternate solution to this?

I ran it by the scriptconsole with no errors found

Many many thanks

Pon

 

 

Vasiliy Zverev Community Champion Dec 11, 2016

There are two options:

  1. If clause if equal to false and update is not called
  2. Wrong update statement

To check it try to execute code into a script console. Before update add 

return "before update".

Then will go further.

 

Hey Vasiliy, 

 

Thank you so much, 

 

I've just tested the condition, and its returning false?

HashMap&lt;String, String &gt; value = (HashMap&lt;Object, Object&gt;) issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Select Email Template"));

def condition = value.get(null).equals("Privacy") &amp;&amp; (value.get("1").equals("ID Check"))

return condition

Also check with only: 

def condition = value.get(null).equals("Privacy")

and its returning false aswell?

 

Any solution?

Many Many thanks, 

Pon

I GOT IT WORKING smile 

 

Thanks Vasiliy.

Below code works for me, but only checks for second value,it can be amended to check for first value aswell:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.UpdateIssueRequest
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject('TEST-422')
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
//HashMap&lt;String,String &gt; value = (HashMap&lt; String, String&gt;) issue.getCustomFieldValue(customFieldManager.getCustomFieldObject(11401));
value = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject(11401));
def stringval = "ID Check"
def cascval = (String)value.get("1")
if (cascval == "ID Check")
    issue.setCustomFieldValue(customFieldManager.getCustomFieldObject(10806), stringval)
 else if (cascval == "Closing")
             issue.setCustomFieldValue(customFieldManager.getCustomFieldObject(10806), "closing")
  ComponentAccessor.getIssueManager().updateIssue(
            ComponentAccessor.getJiraAuthenticationContext().getUser()
            , issue
            , UpdateIssueRequest.builder().eventDispatchOption(EventDispatchOption.ISSUE_UPDATED).sendMail(false).build()
    )

Thanks, 
Pon 

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

80 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