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

Meher HAJJI June 9, 2022

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
Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 9, 2022

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