How to check for a Custom Field value in a Post Function?

Moon May 8, 2015

I have created a Single Select Single Choice custom field and named it "CF Name". It can have several values one of which is "CF Value". I have the following code in a Post Function to validate "CF Name" and assign the ticket to a particular team member called user@company.com,

Here is my code. When I remove the If statement my post function works and ticket gets assigned to user@company.com. But when I put the validation in it does not make an assignment. Can anyone help please.

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
  
CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();
CustomField customField_name = customFieldManager.getCustomFieldObjectByName('CF Name');

issue.summary = ('For your Review: ' + issue.summary);
if(customField_name == "CF Value") 
{
    issue.assigneeId = 'user@company.com';
}

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
JamieA
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 8, 2015

I don't think cfValues is available in normal scripts, it's just conditions, although I could be totally wrong. But you don't want the spread-dot operator (asterisk), that's for multi selects. This should work:

if(issue.getCustomFieldValue(customField_name)?.value == "CF Value") {...}

 

 

Udo Brand
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 8, 2015

Learned someting new ;-)

Moon May 11, 2015

It works! thanks @Jamie Echlin [Adaptavist]

0 votes
Moon May 11, 2015

Hi @Jamie Echlin [Adaptavist]
Is there any way I can use your function with "contains" to validate against a string like what @Udo Brand has done in his code. As an example I have used this before to validate a component against a value. Thanks!

if issue.components.name*.contains('VALUE') { ... }
JamieA
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 11, 2015

Do you want to check for a single select having any of a list of values, or a multiselect containing a particular value, or a multiselect containing any of a list of values?

Moon May 12, 2015

@Jamie Echlin [Adaptavist] Thanks for your quick response! It is for a single select/single choice Custom Field that can contain any of a list of values. List of values may be prefixed by a common string. For example the custom field may have these values: Customer_aaa Customer_bbb Customer_ccc I'd like to validate the custom field value against "Customer_". Hope that makes sense, Thanks!

0 votes
Udo Brand
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 8, 2015

try

if(cfValues['CF Name']*.value.contains("CF Value")) {...}
Moon May 11, 2015

Thanks for your response. I had tried your function already and it was not successful. As @Jamie Echlin [Adaptavist] says it does not appear that cfValue is available in generic scripts. It is available in built-in scripts though.

TAGS
AUG Leaders

Atlassian Community Events