Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Case insensitive check script validator - Unique check before creation

Jst December 5, 2018

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

1 answer

1 accepted

0 votes
Answer accepted
Jst December 7, 2018

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!");}

Suggest an answer

Log in or Sign up to answer