Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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

1 answer

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 Marketplace Apps & Integrations

Staying organized with Jira: best practices for a better project management

Project managers know this problem: A “mountain of work” lays in front of you, and you don’t know how and where to tackle them. Different to-dos lie ahead, but just one task after the other can be ha...

248 views 2 1
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