Hi guys,
First post here hoping to get some help on this. I am looking for a script that sits in the workflow and basically does the following -
Ticket comes in and let’s say the custom field in question has number 456 entered
Another issue already created in that project with matching custom field data of 456 then transitions to a new status due to this duplication .
Would this be possible ?
Thanks
Hi @CaoimhinC .
Yes, it’s possible, to do that, the first thing that you need to do is to get the value of this custom field from the issue.
Then you can execute a JQL to obtain if there is any issue that has the same value, Perform a JQL Search in ScriptRunner for Jira - Adaptavist Library
and the last of all you should transition the issue using something similar to that piece of code:
def transitionValidationResult = issueService.validateTransition(loggedInUser, issue.id, transitionId, issueService.newIssueInputParameters())
if (transitionValidationResult.valid) {
def transitionResult = issueService.transition(loggedInUser, transitionValidationResult)
if (transitionResult.valid) {
log.warn "Transitioned issue ${issue} through action ${transitionId}"
} else {
log.warn 'Transition result is not valid'
}
} else {
log.warn 'The transitionValidation is not valid'
}
Let me know if this helps you
Hi @imadero
Appreciate the help . Could you show me what the both look like combined together as one code snippet ? And is then then placed as a post function in the workflow or as a behaviour against the custom field ?
Thanks
Also when you say the first thing I need to do is get the value of the custom field . How can I get this if the requirement is just if it’s a duplicate data entry into the custom field of a new ticket which then triggers the transition of the existing ticket to a new status ?
On a postfunction (and also listeners) you have some context variables created by scritprunner that you can use on the script, in this case that it’s a postfuncion on the create workflow, scriptrunner provides a variable call issue, that contains the information about the created issue, so using the correct methods, we can obtain the value of the custom field, in your example, we will obtain the value 456, and later we should use the 456 value on the jql function to check if exist any other issue with this value.
Hi this is an approximation about how the script should be, but probably is not going to work right now, but you must add the jql, the name of the field, and the transition id.
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchException
import com.atlassian.jira.web.bean.PagerFilter
import org.apache.log4j.Level
// Check actual value of a field
def customFieldManager = ComponentAccessor.customFieldManager
def customFieldObject = customFieldManager.getCustomFieldObjectsByName("your custom field name").first()
def customFieldValue = issue.getCustomFieldValue(customFieldObject) as String
// Set log level to INFO
log.setLevel(Level.INFO)
// The JQL query you want to search with
final jqlSearch = "Some JQL query"
// Some components
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def searchService = ComponentAccessor.getComponentOfType(SearchService)
// Parse the query
def parseResult = searchService.parseQuery(user, jqlSearch)
if (!parseResult.valid) {
log.error('Invalid query')
return null
}
try {
// Perform the query to get the issues
def results = searchService.search(user, parseResult.query, PagerFilter.unlimitedFilter)
def issues = results.results
if (issues){
def transitionValidationResult = issueService.validateTransition(loggedInUser, issue.id, transitionId, issueService.newIssueInputParameters())
if (transitionValidationResult.valid) {
def transitionResult = issueService.transition(loggedInUser, transitionValidationResult)
if (transitionResult.valid) {
log.warn "Transitioned issue ${issue} through action ${transitionId}"
} else {
log.warn 'Transition result is not valid'
}
} else {
log.warn 'The transitionValidation is not valid'
}
}
} catch (SearchException e) {
e.printStackTrace()
null
}
If you don’t have so much experience working with scripts, we have a paid service to help the clients with all the customizations that they need, like this one, so we will be the ones that develop, install and test the scripts for you. Jira Service Management
Thanks, Ivan.
Thanks for this . I am currently sharpening my groovy skills and have used scriptrunner in the past but thought I’d ask for help on this one as it’s beyond my skill level currently .
The custom field is named claim Id
The transition id is 431
On the jql search, is this search just to return the current field data ?
If you could help me on this one I’d really appreciate it and I can test it then in a dev environment. Really appreciate this help have been stuck on this one for a while