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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Duplicate values custom field

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

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 

HI Andrey

 

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

Thanks for you response ,

Thanks for you response ,

I have resolved the request 

 

regards 

Like Richard Duffy likes this

Andrey

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

Thanks

Richard

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 

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
}

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"

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

🍻🍂Apptoberfest Update: Upcoming Virtual Events 🎉

Hello Community! I hope you've been enjoying the 🍂Apptoberfestivities🍂 (I know I have!) The event is heating up next week with a series of virtual events that we're calling the 🍻🍂Partner App ...

322 views 3 14
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you