Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,298,560
Community Members
 
Community Events
165
Community Groups

make a field mandatory based on the content of two other fields

Hello,

 

We need to make a custom field required based on two other fields contents : if one of them is not empty, it will be required

All fields are not in the screen of the transition, we have only the Field 1 who need to be required.

Field 1 : Text Field (single line) 

Field 2 & 3 : Number Field 

thoses Field (2 & 3) are calculated using this script :

##################

def Some = getFieldById("customfield_19059");
String test1 = getFieldById("customfield_19060").getValue();
String test2 = getFieldById("customfield_19062").getValue();
String test3 = getFieldById("customfield_19063").getValue();
String test4 = getFieldById("customfield_19064").getValue();

int cfHC = test1.findAll( /\d+/ )*.toInteger()[0];
int cfFC = test2.findAll( /\d+/ )*.toInteger()[0];
int cfSLOC = test3.findAll( /\d+/ )*.toInteger()[0];
int cfECC = test4.findAll( /\d+/ )*.toInteger()[0];
int total = cfHC+cfFC+cfSLOC+cfECC;

if (total!=null && total>0)
{
Some.setFormValue(total);}

else {Some.setFormValue('0')
}

##################

 

We have test a lot of different methods for this setting but it's not work.

here is a few : 

Script 1 : added in a behaviour mapped to the correct project/issue type and for field who we need to set required ("customfield_19081"). 

##################

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.jira.groovy.user.FormField

def checkvalue1 = getFieldById("customfield_19059")

def checkvalue2 = getFieldById("customfield_19066")


def source = getFieldById("customfield_19081")


if ((checkvalue1 != "0") || (checkvalue2 != "0"))
{source.setRequired(true)}
else 
{source.setRequired(false)}

##################

 

Script 2 : script added to the field with ID : 19081

##################

import com.atlassian.jira.component.ComponentAccessor

def String DK = getFieldById("customfield_19081").getValue().toString();
def String OOC = getFieldById("customfield_22919").getValue().toString();
def String RC = getFieldById("customfield_19066").getValue().toString();


if ((OOC != "0")||(RC != "0"))
getFieldById("customfield_19081").setRequired(true);
else getFieldById("customfield_19081").setRequired(false);

##################

 

thank you in advance for your help and assistance 

1 answer

1 accepted

1 vote
Answer accepted

Hi @Meher HAJJI ,

in order to make your code working, you need to add both fields on the screen (otherwise their values will be not retrieved by your script).

You make add them and make readonly so that none can edit them during transition.

Another solution is to retrieve them through api issue.getCustomFieldValue()

 

Hope this helps,

Fabio

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
Community showcase
Published in Jira Software

Upcoming changes to epic fields in company-managed projects

👋 Hi there Jira Community! A few months ago we shared with you plans around renaming epics in your company-managed projects. As part of these changes, we highlighted upcoming changes to epics on...

14,877 views 37 49
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you