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,366,357
Community Members
 
Community Events
168
Community Groups

Make field mandatory on transition IF other field is NULL

Rob B Rising Star Jan 31, 2018

Is it possible to make a custom field mandatory ONLY when another customer field has not been populated (NULL) on a screen.
This will happen on the create transition and i have no idea were to start! 

2 answers

You should start with ScriptRunner behaviours. You can find more info here:

https://scriptrunner.adaptavist.com/5.3.0/jira/behaviours-overview.html

You would need to add the custom field which influences the required behaviour of another custom field and write a script for the field.

The script would look something like this

def field1 = getFieldById(getFieldChanged())

def field2 = getFieldByName("customFieldName")

if (field1.getValue() == "your value") {

  field2.setRequired(true)

} else {

field2.setRequired(false)

}

I did not check if the script works, but it s the idea.

In my opinion, it is better to use the validator

import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def CustomFieldValue1 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("name1"));

if
(CustomFieldValue1 == null) {
def CustomFieldValue2 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("name2"));

if(CustomFieldValue2 == null){
invalidInputException = new InvalidInputException("name2 is mandatory when name1 is null");
}

}

 

Rob B Rising Star Feb 01, 2018

Worked perfect thanks Neta. Problem im having now is it works when i create the case, but when i create another case it doesnt?

Once ive published the modified workflows its only lasting one creation if that makes sense?

You added another if case and it does not work? Can you send the script?

Rob B Rising Star Feb 01, 2018

So i did a validator on the create transition (Script Validator)....

import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def CustomFieldValue1 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Retailer ID List"));

if(CustomFieldValue1 == null) {
def CustomFieldValue2 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Retailer ID"));

if(CustomFieldValue2 == null){
invalidInputException = new InvalidInputException("Retailer ID is mandatory when Retailer ID List is not populated");
}

}

 

What is the problem can you explain ?

I don't understand what you mean when you said "case" I thought you meant you added another if statement..

 

Rob B Rising Star Feb 01, 2018

Sorry when i say case i mean issue (blame our users terminology)

I add script to create transition - Publish workflow - Create issue - Works perfect!

Then if i try to create another issue - script hasnt worked

The issue you created has the same WF as the first issue?

You added another condition to the script?

It's not supposed to happen..

Rob B Rising Star Feb 01, 2018

Yes the same WF. Its a weird one the fact it works the first time, but then doesnt even though im doing exactly the same thing?

Below are the validators and post functions...

validation_postfunction.jpg

1)Try to add logs to the script

2) Try this:

import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def CustomFieldValue1 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("name1")) as String;

if
(CustomFieldValue1 == null && CustomFieldValue1=="") {
def CustomFieldValue2 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("name2")) as String;

if(CustomFieldValue2 == null && CustomFieldValue2 ==""){
invalidInputException = new InvalidInputException("name2 is mandatory when name1 is null");
}

}

If it's doesn't work I have no idea why it happens .. I use this script all the time on the create transition this way and did not come across it ..

By adding logs to the script you can know exactly why it's happen.

Rob B Rising Star Feb 01, 2018

Is there a chance it could be because one of the fields is a 'JSON Request Custom Field'?

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events