Set customfield value (Free text) based on cascading select

Deleted user December 8, 2016

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
Answer accepted
Vasiliy Zverev
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.
December 8, 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)
}
Deleted user December 8, 2016

@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
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.
December 8, 2016

Try to debug it using Script console.

Deleted user December 8, 2016

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 F Behnke
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.
December 8, 2016

What type of field is customField_10806?

Deleted user December 8, 2016

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
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.
December 9, 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()
    )
}
Deleted user December 10, 2016

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
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.
December 10, 2016

It seems that wrong customfild id into 

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

Replace it with correct one

Deleted user December 11, 2016

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
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.
December 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.

 

Deleted user December 12, 2016

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

Deleted user December 12, 2016

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
TAGS
AUG Leaders

Atlassian Community Events