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)
}
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)
}
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.