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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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

Behaviours: Checking that Text Field Value Doesn't Already Exist in Current Issue's Jira Project Edited

Trying to make sure a custom text field value (single line) does NOT already exist in the current issue's project. Right now this script is doing nothing and throws no errors on the Behaviors screen.

 

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

def issue = underlyingIssue
def CPID = customFieldManager.getCustomFieldObject("customfield_10210")
def CPIDValue = underlyingIssue?.getCustomFieldValue(CPID)
String jql = "\"Clarity Project ID\" ~ ${CPIDValue}"
List<MutableIssue> jqlIssues = getIssuesFromJQL(jql)

if (jqlIssues) {
throw new InvalidInputException("Clarity Project 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.getResults().collect { issue -> issueManager.getIssueObject(issue.getId()) }
return mutableIssueList
}

 

2 answers

1 accepted

0 votes
Answer accepted
Leo Community Leader Oct 22, 2020

Hi @Lacey McDonnell

If I'm not wrong, you want to compare the field text/id which is entered during create/edit

but your current script will fetch the field value which is stored in the DB already

def CPID = customFieldManager.getCustomFieldObject("customfield_10210")
def CPIDValue = underlyingIssue?.getCustomFieldValue(CPID)
String jql = "\"Clarity Project ID\" ~ ${CPIDValue}"

so you may try this script with following lines

def CPID = getFieldByID("customfield_10210").formValue
String jql = "\"Clarity Project ID\" ~ ${CPID}"

 

BR,

Leo

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Opsgenie

Investigate deployment-related incidents with Bamboo and Opsgenie together

Back in April of last year one of the major product announcements from Opsgenie was the launch of the Incident investigation view which created a deep connection between Bitbucket and Opsgenie, empow...

93 views 1 4
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