subtasks fields need to block parent transition

using scriptrunner, could someone share with me how i would create a validator where i have three custom fields in a subtask (unit price, quote number, part number) that must be filled out in order to transition the parent? And if not will return a message that says " unit price, quote number, part number on each line item must be entered to proceed"

1 answer

1 accepted

1 vote

This should do the trick

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

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def unit_price = customFieldManager.getCustomFieldObjectByName('Unit Price');
def quote_number = customFieldManager.getCustomFieldObjectByName('Quote number');
def part_number = customFieldManager.getCustomFieldObjectByName('Part Number');

def subTasks = issue.getSubTaskObjects();
if(subTasks) {
subTasks = (Collection<Issue>)subTasks;
for(Issue subTask : subTasks) {
if(!(subTask.getCustomFieldValue(unit_price) && subTask.getCustomFieldValue(quote_number) && subTask.getCustomFieldValue(part_number))) {
throw new InvalidInputException("Unit price, part number, and quote number are required on subtasks")
}
}
}

Thank you Tarun. It worked as long as those fields were filled out on one of the sub-tasks. However, i need to ensure it validates those fields are inputted on all subtasks. 

it did throw this error

Time (on server): Tue Dec 12 2017 10:23:20 GMT-0600 (Central Standard Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2017-12-12 09:23:20,193 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2017-12-12 09:23:20,194 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: OPS-577, actionId: 31, file: <inline script>
java.lang.NullPointerException
 at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896)
 at com.atlassian.jira.issue.Issue$getCustomFieldValue$4.call(Unknown Source)
 at Script8.run(Script8.groovy:14)

Did you put proper custom field name in the above code?

Like in

customFieldManager.getCustomFieldObjectByName("here you have to enter the exact name of the custom field"

it looked like a case sensitive issue. thank you so much for that.

do you have time for one more? its a bit more complex. :)

Tarun Sapra Community Champion Dec 13, 2017

Hi @Scott Federman, sure tell me, but I would suggest to create another question and share the link here as it's always better not to mix two questions.

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,287 views 14 20
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot