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

Duplicate values custom field

Richard Duffy
Contributor
November 13, 2019

Hey 

Looking for some general advice and pointing in the right direction!

Tools: Jira, Scriptrunner , JSU

Field: Artefact ID (single line text type)

Problem: We want to prevent users entering duplicate values on this field on a transition screen. Ideally we would show a error message telling the user it is a duplicate value. And if possible link this ticket to the duplicate when they enter a unique Artefact ID.

So I believe we would need to do some validation using SQL on the database for this custom field values. Is that possible using groovy/scriptrunner?

Hoping to avoid installing/purchasing any other plugins and do this with scriptrunner

Any help much appreciated

Rich

2 answers

Suggest an answer

Log in or Sign up to answer
0 votes
Andrey Miranda Diaz July 6, 2021

HI @Richard Duffy ,

Do you resolved is request?

I have the same  request .

I try to use this script but i dont have success result.

if you have some idea please tell me

 

thanks 

Richard Duffy
Contributor
July 6, 2021

HI Andrey

 

No we decided to simplify our business process, best of luck!

Andrey Miranda Diaz July 6, 2021

Thanks for you response ,

Thanks for you response ,

I have resolved the request 

 

regards 

Like Richard Duffy likes this
Richard Duffy
Contributor
July 6, 2021

Andrey

Did you manage to script this? If so can you share with us?

Thanks

Richard

Andrey Miranda Diaz July 8, 2021

Yes  i share my script :

 

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.component.ComponentAccessor

def parser = ComponentAccessor.getComponent(JqlQueryParser)
def query = parser.parseQuery("project = ${issue.projectObject.key} and 'Wallet Address' = ${cfValues['Wallet Address'].value}")

def searcher = ComponentAccessor.getComponent(SearchService)
def kount = searcher.searchCountOverrideSecurity(currentUser, query)

return kount == 0

 

My custom field is Wallet Address 

0 votes
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.
November 13, 2019

Hi Richard,

What you need is a scripted validator that would run a JQL search and if that returns any results then show an error to the user. The logic is as follows:

1. User enters some artefact id on a transition screen

2. A JQL search is ran with the following query: "Artefact ID = ${artefact id from screen}"

3. Assuming all artefact ids are unique if that search returns any results then the user is trying to submit already existing id

4. Show an error message with the ticket with the same id

The code for such validator is as follows:

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.query.Query
import com.opensymphony.workflow.InvalidInputException

Issue issue = issue
CustomField artefactId = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Artefact ID")
String artefactIdValue = issue.getCustomFieldValue(artefactId)
String jql = "\"Artefact ID\" ~ ${artefactIdValue}"
List<MutableIssue> jqlIssues = getIssuesFromJQL(jql)

if (jqlIssues) {
throw new InvalidInputException("Artefact ID already exists in issues: ${jqlIssues}")
}
return true

List<MutableIssue> getIssuesFromJQL(String jqlQuery) {
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
IssueManager issueManager = ComponentAccessor.getIssueManager()
JqlQueryParser jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser.class)
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
Query query = jqlQueryParser.parseQuery(jqlQuery)
SearchResults searchResults = searchService.search(user, query, PagerFilter.getUnlimitedFilter())
List<MutableIssue> mutableIssueList = searchResults.getIssues().collect { issue -> issueManager.getIssueObject(issue.getId()) }
return mutableIssueList
}
Richard Duffy
Contributor
November 14, 2019

Hi Ivan

Thanks for the reply. This looks very promising.

Let me start with a quick question?

Q. Should I use a "custom script validator" or "simple script validator" and what is the difference between the two options?

I have tried the custom script validator, it does give some errors/warnings

Script Errors & Warnings.PNG

Outcome:

No effect on the workflow transition screen when entering a duplicate

*** Note ***

my field (Artefact ID) - Select list is "Free Text Searcher"

Like Roman Jedlička likes this
TAGS
AUG Leaders

Atlassian Community Events