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

This widget could not be displayed.

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 Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

158 views 2 0
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