Forums

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

Want to make custom field required based on another field value

jaya chandra reddy nellore May 9, 2022

Hi Folks,

 

I have Custom field Source ID(Text field) values contains like KB-*,IN-*,TE-*..etc, Now i have to create behavior script If Source ID values starts with KB* i want to make RCA Custom field(text field) Required.

Tried below script but not working. Script-1

-------------------------------------------------

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def Source= getFieldById('Source ID')
def RCAfield= getFieldByName('RCA')
RCAfield.required = false

if(Source.toString().contains('KB') ) {
    RCAfield.required = true
}
Script-2
----------------
def value=(String)getFieldByName('Source ID').value
if(value== 'KB-2209920')
{
getFieldByName('RCA').setRequired(true)
}
else
{
  getFieldByName('RCA').setRequired(false)  
}

   

4 answers

1 accepted

0 votes
Answer accepted
Andrea Pannitti
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 9, 2022

Hi @jaya chandra reddy nellore

you could try changing the first script as follow:

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours

def Source = getFieldByName('Source ID').formValue
def RCAfield = getFieldByName('RCA')

if(Source.toString().contains('KB') ) {
RCAfield.setRequired(true)
} else {
RCAfield.setRequired(false)
}
jayachandrajira May 9, 2022

@Andrea Pannitti It's not working.

1 vote
Fabio Racobaldo _Herzum_
Community Champion
May 9, 2022

Hi @jaya chandra reddy nellore ,

welcome to the Atlassian community!

Here a correct behaviour code to associate to your field Source ID.

def sourcefield= getFieldByName('Source ID')
def RCAfield= getFieldByName('RCA')

String sourceValue = sourcefield.getValue()

if(sourceValue.startsWith("KB")){
RCAfield.setRequired(true);
} else {
RCAfield.setRequired(false);
}

Please let me know if it works,

Fabio

jayachandrajira May 9, 2022

@Fabio Racobaldo _Herzum_  This is not working.

def sourcefield= getFieldByName('Source ID')
def RCAfield= getFieldByName('RCA Notes')

String sourceValue = sourcefield.getValue()

if(sourceValue.startsWith("IN")){
RCAfield.setRequired(true);
} else {
RCAfield.setRequired(false);
}
Do i need to add custom field RCA notes in behavior? 
Fabio Racobaldo _Herzum_
Community Champion
May 9, 2022

you need to add that code in a behaviour mapped to your project/issue type and for field "Source ID". It should work

jayachandrajira May 9, 2022

@Fabio Racobaldo _Herzum_  Please check below screen shorts, i am also following same but no use.

behavior script: Screen shorts 1 & 2

Checking behavior applied or not: Screen shorts 3 & 4Screenshot 1.pngScreenshot 2.pngScreenshot 3.pngScreenshot 4.png

Fabio Racobaldo _Herzum_
Community Champion
May 9, 2022

@jaya chandra reddy nellore , is Source ID in the same screen of RCA Notes? It seems no.

jayachandrajira May 9, 2022

@Fabio Racobaldo _Herzum_ Bothe are in the same screen, but both are in different tabs only.

Fabio Racobaldo _Herzum_
Community Champion
May 9, 2022

please could you share me a screenshot of transition screen with both values?

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
May 9, 2022

Hi @jaya chandra reddy nellore,

If you are using this for a Server-Side Behaviour, the correct approach to ensure that the Behaviour will be triggered is to use the fieldChanged to initialise the field the Behaviour is configured for, as shown below:-

import com.onresolve.jira.groovy.user.FieldBehaviours

import groovy.transform.BaseScript


@BaseScript FieldBehaviours behaviours

def Source= getFieldById(fieldChanged)
def sourceValue = Source.toString()

def RCAfield= getFieldByName('RCA')

RCAfield.required = false

if(sourceValue.contains('KB') || sourceValue.startsWith('KB') ) {
  RCAfield.required = true
}

Please visit this ScriptRunner Training Video for more information.
I hope this helps to answer your question. :)
Thank you and Kind regards,
Ram
0 votes
Dirk Ronsmans
Community Champion
May 9, 2022

Without going in to details already, I notice you use different methods for setting required.

   
RCAfield.required = true
and
getFieldByName('RCA').setRequired(true)
Whats the idea here?
Also to get the value you need to use  formField.getValue() and not just .value
maybe a good idea to look thru the api quick reference first?

Suggest an answer

Log in or Sign up to answer