Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Script runner behaviours - How set field mandatory on edit screen while editing

i want to make a custom field based on another custom field mandatory. The code works but while editing the edit screen form the issue the field is not requierd until i submit the changes.

What I want is dynamically to change the field to mandory  while the user ist editing the issue in the edit screen. 

The second example works exactly how I want it but only with a systemfiled. 

My Code: 

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




// different ways of getting field values
def checkbox = getFieldById("customfield_10230")
def begruendung = getFieldById("customfield_10231")
def checkboxInhalt = checkbox.getValue()
log.warn("Test1:" + checkboxInhalt.toString() )


// get the options avaliable for that custom field
def customField = customFieldManager.getCustomFieldObject(checkbox.getFieldId())
log.warn("Test2: " + customField.getValue(underlyingIssue).toString().equalsIgnoreCase("ja"))
// def getOptionById = ComponentAccessor.getOptionsManager().getOptions(config).find {it.optionId == checkbox.fieldOptions.size()}


if(customField.getValue(underlyingIssue) != null && customField.getValue(underlyingIssue).toString().equalsIgnoreCase("ja") )
begruendung.setRequired(true)
else if(customField.getValue(underlyingIssue) == null || customField.getValue(underlyingIssue).toString().equalsIgnoreCase("nein") )
begruendung.setRequired(false)

 

Code from an example with issuelink, which works dynamically:

/**
* Make the links field required when there are no existing link and
* the link type is "is part of"
* Video link: https://youtu.be/q4KfHSTdVeA
*/

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink

def links = getFieldById("issuelinks")
def linksValue = links.getValue()
def linkType = getFieldById("issuelinks-linktype")
def linkTypeValue = linkType.getValue()
def linkField = getFieldById("issuelinks-issues")
def linkFieldValue = linkField.getValue()

log.debug("Links Value: "+ linksValue)
log.debug("Link Type Value: "+ linkTypeValue)
log.debug("Links Value: "+ linkFieldValue)

def existingLinks = ComponentAccessor.getIssueLinkManager().getInwardLinks(underlyingIssue?.getId())

if (existingLinks?.size() == 0 && linkTypeValue == "is blocked by") {

links.setRequired(true)

} else {

links.setRequired(false)

}

 

1 answer

1 accepted

Hi @Laith Ata ,

 

I think your issue is that you are getting the value of customfield in the issue instead of using the value of the edit form.

When you use customField.getValue(underlyingIssue) you are getting the field's value of the issue and not from your edit form. In behaviour in order to get the field value's in the edit form you should use the Field.getValue() method, see below a suggestion modification of your first code:

Just a notice that checkbox can have both String and ArrayList types, it depends if the user select one or more options in the form. For this reason we have an condition to check the value type and I get this piece from the below documentation 

https://library.adaptavist.com/entity/control-form-field-based-on-selected-checkbox-value

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


// different ways of getting field values
def checkbox = getFieldById("customfield_10230")
def begruendung = getFieldById("customfield_10231")

def checkboxInhalt = checkbox.getValue()
def begruendungInhalt = begruendung.getValue()

log.warn("checkboxInhalt:" + checkboxInhalt)
log.warn("begruendungInhalt:" + begruendungInhalt)

// seee https://library.adaptavist.com/entity/control-form-field-based-on-selected-checkbox-value
// Collect the selected checkbox values
// If a single item is selected, the checkbox value type will be String
// If more than one option is selected, the checkbox value will be an ArrayList
log.debug("The checkbox field value: '${checkBoxFieldValue}' of type '${checkBoxFieldValue.class.simpleName}'")
def chosenValuesList = []
if (checkboxInhalt in String) {
chosenValuesList.add(checkboxInhalt)
} else if (checkboxInhalt in ArrayList) {
chosenValuesList.addAll(checkboxInhalt)
}


if("ja" in chosenValuesList){
begruendung.setRequired(true)
}
else if("nein" in chosenValuesList){
begruendung.setRequired(false)
}

 

 

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

🍂📹 Apptoberfest demo contest roundup: vote for your favorite demos!

Hi Community! The submissions are in (and listed below) for the 🍻🍂Apptoberfest🍂🍻 Demo Competition and it’s time for you to place your votes for the best: Analytics & reporting app demo ...

71 views 2 10
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