Hi, I need help trying to find a way of stopping an issue being created if there is an open issue with a custom field value selected.
Example:
Issue 1 is Open and Custom Field = 123
User tries to create Issue 2 and populates Custom Field with '123'. I want the creation to be blocked/stoppped with maybe an error message?
I have script runner installed if that helps?
Thanks
Hi Robert,
I would suggest using a ScriptRunner custom validator, see this documentation: https://scriptrunner.adaptavist.com/latest/jira/custom-workflow-functions.html#_validators
You will want to do something like this (untested /example code)
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
Issue issue = issue
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField customField1 = customFieldManager.getCustomFieldObject("CF1")
def customField1Value = issue.getCustomFieldValue(customField1)
def otherIssue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("TEST-1")
def otherIssueCustomField1Value = otherIssue.getCustomFieldValue(customField1)
if(customField1Value == otherIssueCustomField1Value) throw new InvalidInputException("Some combination of multiple fields are in error")
Hope this help.
Thanks,
Johnson Howard (Adaptavist)
Hi Johnson, hope you are well! Thanks for this and thanks for the tshirts! ;)
Question about the TEST-1 ..... Should that be the issue type?
Rob (UU)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ah it's that Robert Bromley! Thought I recognised the name. Hope things are all good your end mate.
TEST-1 is the key for the issue that you want to compare the field with.
So where you have said: "Issue 1 is Open and Custom Field = 123"
TEST-1 is the key for that issue.
and
Issue issue = issue
is the Issue 2 in your respect.
Let me know if you need more help
Thanks,
Johnson
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Mike thinks my way of explaning the requirements is wrong. This is how he thinks it should be.......
'We need the ability to be able to block a user from creating a new issue (Issue type 1) if one or several issues (Issue type 2) are in an open state.
We would need one field within Issue Type 2 to be the validator for the creation of issue type 1.
Example:
o 123
o 234
o 345
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ok so to make sure I have the correct understand of this.
(for example):
Is that right?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Perfect! Why i couldnt explain it i do not know! haha
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
In that case, try something like this (again untested but correct concept):
package exampleScripts
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
//get user
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
//get the issues in project x of type Task that have a value in the CF field
def JQLQuery = jqlQueryParser.parseQuery("project = X AND issuetype = \"Task\" and CF is not EMPTY ")
def results = JQLQuery ? searchProvider.search(JQLQuery, user, PagerFilter.getUnlimitedFilter()) : null
def issues = results? results.issues : null
// get all the values of the CF field from the issues
def cF = customFieldManager.getCustomFieldObject("CF")
def values = []
issues.each {
values.add(it.getCustomFieldValue(cF))
}
//get the issue that is trying to be created, find the value of CF they have entered, check it isnt in the values list
// if it is then throw the error
Issue issue = issue
def cFValue = issue.getCustomFieldValue(cF)
if(cFValue in values) throw new InvalidInputException("Your error message")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
im getting 'no such property issue for class' on line 18
def issues = results? results.issues : null
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Can you log out what is in 'results'
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hold on, are you getting that error as a red line in the code area or in the logs when it runs?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
In the code
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You can ignore those errors. They are just type checking errors. It's basically because 'issues' is assigned to a dynamic variable so when you use dot notation on it like 'issues.results' the environment doesnt know what type of object it is so it throws an error.
Try running it.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I have issue SEW-16 (Notification Issue Type) open with 'Liverpool' in the district field.
I try to create SEW-17 (Logon/off issue type) with 'Liverpool' in the district field and it still allows me to do it and no error message appears.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What I would suggest is taking the JQL query you have and putting it into the issue navigator to see if any issues are returned by it.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Just remove the slashes. The the quotation marks if that still doesnt work.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sorry mate, I made a small mistake:
Change
def cF = customFieldManager.getCustomFieldObject("District council")
to
def cF = customFieldManager.getCustomFieldObjectByName("District council")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Did it work?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
No mate still lets me create it.
This is the logs exe info if thats helps?
Time (on server): Fri May 25 2018 10:52:28 GMT+0100 (GMT Daylight Time)
The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.
2018-05-25 10:52:28,788 ERROR [workflow.ScriptWorkflowFunction]: ************************************************************************************* 2018-05-25 10:52:28,792 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script> com.atlassian.jira.jql.parser.JqlParseException: com.atlassian.jira.jql.parser.antlr.RuntimeRecognitionException: NoViableAltException(59@[]) at com.atlassian.jira.jql.parser.DefaultJqlQueryParser.parseClause(DefaultJqlQueryParser.java:96) at com.atlassian.jira.jql.parser.DefaultJqlQueryParser.parseQuery(DefaultJqlQueryParser.java:32) at com.atlassian.jira.jql.parser.JqlQueryParser$parseQuery.call(Unknown Source) at Script130.run(Script130.groovy:16) Caused by: com.atlassian.jira.jql.parser.antlr.RuntimeRecognitionException: NoViableAltException(59@[]) at com.atlassian.jira.jql.parser.antlr.JqlParser.reportError(JqlParser.java:203) at com.atlassian.jira.jql.parser.antlr.JqlParser.operator(JqlParser.java:1362) at com.atlassian.jira.jql.parser.antlr.JqlParser.terminalClause(JqlParser.java:645) at com.atlassian.jira.jql.parser.antlr.JqlParser.notClause(JqlParser.java:555) at com.atlassian.jira.jql.parser.antlr.JqlParser.andClause(JqlParser.java:451) at com.atlassian.jira.jql.parser.antlr.JqlParser.orClause(JqlParser.java:366) at com.atlassian.jira.jql.parser.antlr.JqlParser.clause(JqlParser.java:328) at com.atlassian.jira.jql.parser.antlr.JqlParser.query(JqlParser.java:237) at com.atlassian.jira.jql.parser.DefaultJqlQueryParser.parseClause(DefaultJqlQueryParser.java:89) ... 3 more Caused by: NoViableAltException(59@[]) at com.atlassian.jira.jql.parser.antlr.JqlParser.operator(JqlParser.java:1192) ... 10 more
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Rob,
I have just tested this script on my local instance and it works. The error looks like the jql you are trying to pass it wrong.
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
//get user
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
//get the issues in project x of type Task that have a value in the CF field
def JQLQuery = jqlQueryParser.parseQuery("project = TEST AND issuetype = \"Task\" and \"Select List\" is not EMPTY")
def results = JQLQuery ? searchProvider.search(JQLQuery, user, PagerFilter.getUnlimitedFilter()) : null
def issues = results? results.issues : null
// get all the values of the CF field from the issues
def cF = customFieldManager.getCustomFieldObjectByName("Select List")
def values = []
issues.each {
values.add(it.getCustomFieldValue(cF))
}
//get the issue that is trying to be created, find the value of CF they have entered, check it isnt in the values list
// if it is then throw the error
Issue issue = issue
def cFValue = issue.getCustomFieldValue(cF)
if(cFValue in values) throw new InvalidInputException("Your error message")
As I know this script works for me, there can only be a few reasons why it wouldnt work for you:
If you still can't get it to work I am happy to do a screen share with you.
Thanks
Johnson
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Did you ever get this working?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Johnson, apologies ive been on leave for a week hence no reply.
The JQL ive got in the code works when i put it into Jira. A screen share would be great if you can?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Rob,
I will email you about getting this arranged, pretty sure we can sort it a few mins.
It will just be something to do with the jql string.
Can you mark the above code answer as accepted as this is technically the solution to the problem. Then I will help you figure out your specific issue.
Thanks,
Johnson
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Johnson, you were correct it was the JQL! Thanks so much........ but now we would like an override. Should i log another call?
Thanks
Rob
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Guys,
I am looking for something similar to this, except when creation of the ticket with the same custom field id info is denied, it also transitions the ticket it matched to another status, any help on this would be much appreciated.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Welcome to great meetings, with less work. Automatically record, summarize, and share instant recaps of your meetings with Loom AI.
Learn moreOnline forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.