Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Custom Fields /Dynamic Forms/ Condition on Custom field

Frank May 9, 2018

Hi.

I have two custom fields on the screen. If I select Yes in one Custom Field It needs to be Yes in another custom field and it should pop an error. How to achieve this.?????

3 answers

0 votes
Frank May 16, 2018

@Peter Bengov

I tried something like this

 

import org.apache.log4j.Category;
import com.atlassian.jira.ComponentManager;
import com.opensymphony.workflow.InvalidInputException;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.comments.CommentManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.util.ImportUtils;
import com.opensymphony.util.TextUtils
import com.atlassian.jira.component.ComponentAccessor;

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField customField_1 = customFieldManager.getCustomFieldObject("customfield_18900");
CustomField customField_2 = customFieldManager.getCustomFieldObject("customfield_18509");

if (issue.getCustomFieldValue( customField_1 ) == "Yes") { issue.getCustomFieldValue( customField_2 ) == "Yes"}
else {
throw new InvalidInputException("If Approval Required? is Yes, Approval Attached needs to be Yes to proceed")
}

Eventhough, I set the two fields to Yes, I still see the error. 

0 votes
Frank May 16, 2018

@Peter Bengov

I tried something like this 

import org.apache.log4j.Category;
import com.atlassian.jira.ComponentManager;
import com.opensymphony.workflow.InvalidInputException;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.comments.CommentManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.util.ImportUtils;
import com.opensymphony.util.TextUtils
import com.atlassian.jira.component.ComponentAccessor;

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();

CustomField customField_1 = customFieldManager.getCustomFieldObject("customfield_18900");
CustomField customField_2 = customFieldManager.getCustomFieldObject("customfield_18509");

if (issue.getCustomFieldValue( customField_1 ) == "Yes") { issue.getCustomFieldValue( customField_2 ) == "Yes"}
else {
throw new InvalidInputException("If Approval? is Yes, Approval Attached needs to be Yes to proceed")
}

 

Even though I set two custom fields to Yes, I am still getting the error

0 votes
Peter Bengov
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 10, 2018

Hi Frank,

If you have Behaviours (Script Runner) installed - this is possible using this plugin. 

Frank May 11, 2018

@Peter Bengov I do have behavior plugins. Please guide me

Peter Bengov
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 12, 2018

Try something like this:

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager();

String customField1Name = "XXX";
String customField2Name = "YYY";

String cf1ValueToCheck = "Yes";

String errorToShow;

def cf1;
def cf1FieldValue;

cf1 = getFieldByName(customField1Name);
cf1FieldValue = cf1.getValue();

if ((cf1FieldValue != null) && (cf1FieldValue.toString() == cf1ValueToCheck)) {
cf2.setRequired(true);
}

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, mindful member, mindful member badge, atlassian community, community kudos, community giveaway, atlassian swag, community values, empathy and kindness, badge challenge, atlassian learning, community engagement, swag giveaway

Earn a Badge, Win a Prize 🎁

Earning the Mindful Member badge proves you know how to lead with kindness, plus it enters you into a giveaway for exclusive Atlassian swag. Take the quiz, grab the badge, and comment on our announcement article to spread the good vibes!

Start here
AUG Leaders

Atlassian Community Events