Jira: Require field based on reporter
07/12/2018 Leave a comment
Our IT support team creates tickets in Jira for things submitted to the helpdesk that need to be handled by the development team. If the person that submitted the original ticket is a Jira user, they’ll change the reporter to that person. Otherwise it gets left as the shared account used by the helpdesk (username: helpdesk). In those cases, the helpdesk is supposed to enter the users information in the description. We’ve been having issues lately where they don’t do this. We wanted to add a required custom field Original Requestor, but making it required would be annoying for any other users entering tickets, as well as cases where the helpdesk was able to change the reporter.
Using the script runner plugin, I was able to create a custom validator that only requires the field be populated when the reporter is helpdesk. I had to piece this together from a few different forum posts and digging through the Jira class documentation. I figured I’d share my solution in case anyone else needs to do something similar.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.issue.fields.CustomField import com.opensymphony.workflow.InvalidInputException import org.apache.log4j.Category import com.atlassian.jira.user.ApplicationUser import com.atlassian.jira.component.ComponentAccessor Issue myIssue = issue ApplicationUser reporter = myIssue.getReporter(); CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager() Collection cfs = customFieldManager.getCustomFieldObjectsByName("Original Requestor") CustomField cf = cfs[0] Object cfValueObj = issue.getCustomFieldValue(cf) String cfValue = "" if(cfValueObj != null){ cfValue = cfValueObj.toString().trim() } if(reporter.getUsername().equals("helpdesk") && cfValue.equals('')){ throw new InvalidInputException("reporter","You must either change the reporter or specify the original requestor.") } |
This is what the error looks like if the helpdesk user does not provide a value for the ‘Original Requestor’ field
A few additional things
- I couldn’t figure out how to get the message to show under the Reporter field and have the label in the top part say “Reporter” instead of “reporter.”
- You can use
cf.getId()
as the first parameter in the InvalidInputException, and it will show up under the custom field – but the label was custom_field_10023 or something like that. Don’t know if there is a way to have it show the field name in the label. - You can show errors for multiple fields by creating an instance of InvalidInputException for the first error, and then calling
myException.addError(..)
for the remaining errors. - You can omit the first parameter to InvalidInputException, and just pass in the error message. However, nothing will show up under the field causing the error in this case.
- All of the examples I found used
customFieldManager.getCustomFieldObjectByName
, but according to the documentation, that method has been deprecated in favor ofcustomFieldManager.getCustomFieldObjectsByName
.
Recent Comments