Update custom field whenever JQL filter matches Edited

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

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

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

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 :-)

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!

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Wednesday in Jira

Join our webinar: How 1B+ feature flag events helped us build the new Jira

Every time you release software, there's a bit of risk – that there's a bug, that something breaks, or that the feature doesn't resonate with customers. Feature flagging helps make high stakes s...

132 views 0 3
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you