Update custom field whenever JQL filter matches

Noella Merrick January 23, 2018

I would like to create a script that changes a custom field whenever a JQL query is true:

I tried using inbuilt jira automation, but it doesn't work with custom fields therefore I'm trying to use script runner. Any help? I'm still a beginner therefore if you can guide step by step that would be great. Thanks.

3 answers

0 votes
Noella Merrick January 26, 2018

Thanks all for the replies, I have been trying to come up with something based off your replies and ended up with this:

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.bc.issue.IssueService.UpdateValidationResult
import com.atlassian.jira.bc.issue.IssueService.IssueResult
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option


def customFieldMgr = ComponentAccessor.getCustomFieldManager()
def groupManager = ComponentAccessor.groupManager
def groups = groupManager.getGroupNamesForUser(issue.reporter)

//def customField = customFieldMgr.getCustomFieldObjectsByName("Field 1")
def customField = customFieldMgr.getCustomFieldObject(12560L)
def category = issue.getCustomFieldValue(customField)

def acustomfield = 'fieldValue'
//incidents
if (issue.issueType.name == 'Incident' && groups.any{it.contains('aTeam')}) {
acustomfield = 'anotherTeam'
}
//else if (issue.issueType.name == 'Incident' && groups.contains ('team4')) {
else if (issue.issueType.name == 'Incident') {
acustomfield = 'team3'
}
//event
else if (issue.issueType.name == 'Event' && groups.contains('team2')) {
acustomfield = 'team5'
}
else if (issue.issueType.name == 'Event' && groups.contains('anotherTeam')) {
acustomfield = 'team'
}
else if (issue.issueType.name == 'Incident' && category == ("someOtherData")) {
acustomfield = 'team'
}

try {
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser

IssueService issueService = ComponentAccessor.issueService
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();
issueInputParameters.addCustomFieldValue(12562L, acustomfield)
issueInputParameters.setSkipScreenCheck(true)

UpdateValidationResult updateValidationResult = issueService.validateUpdate(user, issue.id, issueInputParameters);

if (updateValidationResult.isValid()) {
IssueResult updateResult = issueService.update(user, updateValidationResult);

if (updateResult.isValid()) {
log.info("Successfully updated $issue.key.")
} else {
log.error("Could not update $issue.key: ${updateResult.errorCollection}")
}
} else {
log.error("Could not update $issue.key: ${updateValidationResult.errorCollection}")
}
} catch (Exception ex) {
log.error("Could not update $issue.key: ${ex.message}")
}

 

This however isn't working and the custom field is set to the default value. The lines that I added were:

else if (issue.issueType.name == 'Incident' && category == ("stuff")) {
acustomfield = 'team2'
}

 

Apart from this I also imported the functions as you suggested, but it seems as its not picking up the ID of the custom field for some reason. Any help?

 

Thanks!

0 votes
Stephen Cheesley _Adaptavist_
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.
January 23, 2018

Hi Noella,

If you want a script that displays a "Resolver Group" based on whether an issue is in a given "Issue Category" and is of a given issue type, then the following script should do what you need:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option

def customFieldMgr = ComponentAccessor.getCustomFieldManager()

// Get the issue category field
def customField = customFieldMgr.getCustomFieldObjectsByName("Issue Category")

// Get the category and type
Option category = issue.getCustomFieldValue(customField)
def issueType = issue.issueType.name

if (category
&& category.value == "Subjective and PED Data (CATS-331)"
&& issueType == "Incident") {
return "A"
}

return "B"

If you create a script field with this script it should categorise as you require.

Note: If you want a field to be populated when the user runs a specific JQL query, then that is a different use case all together.

I hope this helps :-)

Steve

Stephen Cheesley _Adaptavist_
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.
January 23, 2018

P.S. if you need any guidance in setting up a scripted field, just let me know. To get you started there is some good documentation here on how to set them up. I would not recommend that you do any testing or "experimentation" on your production instance. You should use a test instance instead :-)

0 votes
MoroSystems Support
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.
January 23, 2018

Hello,

 

 could you please better explain, what do you want to achieve? For example: when should be the custom field updated (when issue is created and correspond to that JQL query? or when issue is updated?), field should be updated in that particular issue, which is created/changed, or somewhere else? Or make step by step explanation.

 

If you want, for example, change field when issue is created, its quite easy. You need to use ScriptRunner's Listeners to make it use the script, when you need it (e.g. on issue created). More about listeners here: https://scriptrunner.adaptavist.com/latest/jira/listeners.html#_built_in_listeners

 

I can help you with the script, if this is the case. Let me know.

 

regards, Frenk,

Morosystems

Suggest an answer

Log in or Sign up to answer