Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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,559,686
Community Members
 
Community Events
185
Community Groups

How to require certain fields based on a JQL condition

Edited

My requirement is this: Need to require 3 fields to be populated for major incidents. Major incidents are determined by the following JQL:

project = ITIM AND priority in ("0 - (P1) Emergency", "1 - (P2) Critical") OR "P2 Alert" = Yes

"P2 Alert" is a custom field.

So when users resolve the major incident, we want to make sure that three fields are filled out: Start Time, End Time, and Root Cause Owner which are also 3 custom fields.

We do have Adaptavist ScriptRunner for JIRA installed. Thanks in advance.

1 answer

1 accepted

1 vote
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.
Dec 29, 2017

Hi Suzanne,

A scripted validator seems to be the solution here.  But before you begin make sure that:

1) You have a screen attached to your "Resolve issue" transition.

2) That screen has your Start Time, End Time, and Root Cause Owner fields in it.

Next, create a scripted validator using the below code and add it to your "Resolve issue" transition. I'm going to assume that you know how to look up your custom field IDs because you'll need them for the code:

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

def cfMgr = ComponentAccessor.getCustomFieldManager()

def projectKey = issue.getProjectObject().getKey()
def priority = issue.getPriority().getName()
def p2Alert = issue.getCustomFieldValue(cfMgr.getCustomFieldObject(11111 as Long)) as String //field id in parenthesis

def startTime = cfMgr.getCustomFieldObject(22222 as Long)
def endTime = cfMgr.getCustomFieldObject(33333 as Long)
def rootCauseOwner = cfMgr.getCustomFieldObject(44444 as Long)

def reqFieldsList = new ArrayList()
reqFieldsList.addAll(startTime, endTime, rootCauseOwner)

if ((projectKey == "ITIM" && (priority == "0 - (P1) Emergency" || priority == "1 - (P2) Critical")) || p2Alert == "Yes"){
for (int i = 0; i < reqFieldsList.size(); i++){
if (issue.getCustomFieldValue(reqFieldsList[i]) == null){
throw new InvalidInputException("'Start Time', 'End Time' and 'Root Cause Owner' are required!")
}
}
}

Hi Ivan, thank you! I do know how to look up the custom field IDs. I'll give it a shot and let you know how it works. Thanks!

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.
Dec 29, 2017

I suppose a more flexible solution would be to run a JQL search inside your script and then check the current issue against the results.

Please note that if you want to use quotes ("") inside a String definition, make sure that you place a backslash (\) before them, like so:

String sampleString = "project = ITIM AND priority in (\"0 - (P1) Emergency\", \"1 - (P2) Critical\") OR \"P2 Alert\" = Yes"

import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def jqlQuery = jqlQueryParser.parseQuery("project = ITIM AND priority in (\"0 - (P1) Emergency\", \"1 - (P2) Critical\") OR \"P2 Alert\" = Yes")
def resultIssues = searchProvider.search(jqlQuery, currentUser, PagerFilter.getUnlimitedFilter()).getIssues()

for (int i; i < resultIssues.size(); i++){
if (issue.getKey() == resultIssues[i].getKey()){
def cfMgr = ComponentAccessor.getCustomFieldManager()
def startTime = cfMgr.getCustomFieldObject(22222 as Long)
def endTime = cfMgr.getCustomFieldObject(33333 as Long)
def rootCauseOwner = cfMgr.getCustomFieldObject(44444 as Long)
def reqFieldsList = new ArrayList()
reqFieldsList.addAll(startTime, endTime, rootCauseOwner)

for(int v; v < reqFieldsList.size(); v++){
if (issue.getCustomFieldValue(reqFieldsList[v]) == null){
throw new InvalidInputException("'Start Time', 'End Time' and 'Root Cause Owner' are required!")
}
}
}
}

Hi Ivan, I was finally able to try this today and I got the following error. Any ideas?

Script33.groovy:24 [Static type checking] - Cannot find matching method com.atlassian.jira.issue.Issue#getCustomFieldValue(E). Please check if the declared type is right and if the method exists. @ line 24, column 11.

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.
Jan 03, 2018 • edited

This is odd, it should work as is. But ok, let's change something.

Try this:

import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.fields.CustomField

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def jqlQuery = jqlQueryParser.parseQuery("project = ITIM AND priority in (\"0 - (P1) Emergency\", \"1 - (P2) Critical\") OR \"P2 Alert\" = Yes")
def resultIssues = searchProvider.search(jqlQuery, currentUser, PagerFilter.getUnlimitedFilter()).getIssues()

for (int i = 0; i < resultIssues.size(); i++){
if (issue.getKey() == resultIssues[i].getKey()){
def cfMgr = ComponentAccessor.getCustomFieldManager()
def startTime = cfMgr.getCustomFieldObject(22222 as Long)
def endTime = cfMgr.getCustomFieldObject(33333 as Long)
def rootCauseOwner = cfMgr.getCustomFieldObject(44444 as Long)
def reqFieldsList = new ArrayList()
reqFieldsList.addAll(startTime, endTime, rootCauseOwner)

for(int v = 0; v < reqFieldsList.size(); v++){
if (issue.getCustomFieldValue(reqFieldsList[v] as CustomField) == null){
throw new InvalidInputException("'Start Time', 'End Time' and 'Root Cause Owner' are required!")
}
}
}
}

Incidentally, what version of Jira are you using? 

Thanks, we'll give it a shot! We're using 7.3.8.

That worked! Thanks so much Ivan. Perfect. :)

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.
Jan 05, 2018

Hi Suzanne,

Glad I could help.

A piece of advice if I may. For performance sake you might want to add a resolution check to your JQL to make the list of returned issues as short as possible so that the script doesn't go through a ton of issues while checking the current issue against them.

Ah, that makes perfect sense. Okay, I'll figure out how to do that on Monday. We are still just testing this in our testing instance. Thanks again!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events