Hello!
I took a big chunk of a script found here https://community.atlassian.com/t5/Jira-questions/Stop-issue-creation-if-open-issue-contains-custom-field-value/qaq-p/803176
Everything seems to work just as intended, although there is one problem with it as far as I observed: Case sensitivity. I tried converting values to lowercase and comparing afterwards and some options with equalsIgnoreCase but do not know where to exactly put those methods... Can someone help me to check the array called values against the string cFValue case insensitive?
This validator is for checking the uniqueness of a serial number that can only be added once and only lets you create duplicates using the value noserial.
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 CMDB of type Task that have a value in the CF field
def JQLQuery = jqlQueryParser.parseQuery("project = CMDB")
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(16109)
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 == "noserial") {;
}else if(cFValue in values) { throw new InvalidInputException("This serialnumber already exists!");}
Thanks!
Using Jira Server v7.1.7#71011
Already solved it by changing it:
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 CMDB of type Task that have a value in the CF field
def JQLQuery = jqlQueryParser.parseQuery("project = CMDB")
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(16109)
def values = []
issues.each {
values.add(it.getCustomFieldValue(cF).toLowerCase())
}
//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 == "noserial") {;
}else if(cFValue.toString().toLowerCase() in values) { throw new InvalidInputException("This serialnumber already exists!");}
Online 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.