Forums

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

Require custom field based on another

Tim C March 28, 2019

Im trying to create a workflow "simple script validator" for a transition. Trying to set this up for Original Estimate and Story Points. Basically require one or the other custom field. If one isnt filled out require the other and vise versus. Im not a developer but i have been able to get some insight from other examples ive read online. 

 

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


def CustomFieldManager = ComponentAccessor.getCustomFieldManager();
def CustomFieldValue1 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Original Estimate"));

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

if(CustomFieldValue2 == null){
def invalidInputException = new InvalidInputException("Original Estimate is required");
}

}

2 answers

1 accepted

0 votes
Answer accepted
Ivan Tovbin
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.
March 28, 2019

Hi Tim,

Something like this should do the trick:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.opensymphony.workflow.InvalidInputException

Issue issue = issue

if (!issue.getOriginalEstimate()) {
throw new InvalidInputException("Story Points is required!")
} else if (!getCustomFieldValue(issue, "Story Points")) {
throw new InvalidInputException("Original Estimate is required!")
}
return true

Object getCustomFieldValue(Issue issue, String fieldName) {
CustomField customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(fieldName)
return issue.getCustomFieldValue(customField)
}
Tim C April 1, 2019

Thank you so much Ivan. I tested the code out and it is now requiring both fields to be filled out before it allows it to be transitioned. I needed it to be one or the other though. Need it to require Story Points if the custom field Original Estimate isnt filled out. And the same the other direction make Original Estimate required if Story Points isnt filled in.

0 votes
Tim C April 17, 2019

Was finally able to get this to work with the following script. Thanks Ivan for getting me started. This actually works.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import com.opensymphony.workflow.InvalidInputException

def Object getCustomFieldValue(Issue issue, String fieldName) {
    CustomField customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(fieldName)
    return issue.getCustomFieldValue(customField)
}

def Object storyPoints = getCustomFieldValue(issue, "Story Points");
def Object originalEstimate = issue.originalEstimate;

return !(storyPoints == null && originalEstimate == null)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events