Forums

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

Scriptrunner validator to search for a JQL and compare

Andres Customer
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 30, 2019

I'm trying to find out how to write a validation with scriptrunner that checks if there are any existing issues with the same date as I've put in to a custom date field.

I have a custom date field: "Change Scheduled Date"

When I transition my issue it should check if there are any other issues (JQL) with the same date value as in my issue. If so, the transition should be blocked with a message "There are already changes planned on this date"

 

Any assistance would be much appreciated. 

 

1 answer

1 accepted

1 vote
Answer accepted
fran garcia gomera
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.
September 30, 2019

You could try

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

import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.ApplicationUsers

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser.class)
def searchProvider = ComponentAccessor.getComponent(SearchProvider.class)
def issueManager = ComponentAccessor.getIssueManager()
def userManager = ComponentAccessor.getUserManager()
ApplicationUser user = userManager.getUserByName("soporte")

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName('Start date')


def myDate = cf.getValue(issue) as Date


String JQL='createdDate >= ' + myDate.format("yyyy-MM-dd") + ' and createdDate < ' + (myDate.plus(1).format("yyyy-MM-dd"))

def query = jqlQueryParser.parseQuery(JQL)
if (searchProvider.searchCount(query, user)>0){
return false
}
return true

in a simple script validator

Change the user and the field name

Suggest an answer

Log in or Sign up to answer