Please provide me the groovy script to update priority value based on two custom field values

Vignesh June 3, 2016

I am searching for the groovy script to set priority value based on tow custom field(check box) values.

Here is my code ,but its not working.

Please help to fix it.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option
import com.onresolve.jira.groovy.user.FormField


def AccessType = getFieldByName("Type of Access")
def AccessID = getFieldByName("Type of ID")
def IssuePriority = getFieldByName("Priority")

def type = AccessType?.value
def ID = AccessID?.Value

if ((type == "New User Access" || type == "Update User Access") && ( ID == "Client" || ID == "Third Party"))
{
IssuePriority.setFormValue(optionFor(IssuePriority, "Critical").optionId)
}
else {
IssuePriority.setFormValue(optionFor(IssuePriority, "Normal").optionId)
}

private Option optionFor(FormField IssuePriority, String value) {
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObject(IssuePriority.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)
def optionToSelect = options.find { it.value == value }
optionToSelect
}

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 7, 2016

Hi Vignesh,

This requirement can be achieved using the Behaviours module of ScriptRunner.

 

I have attached below a sample script which can be placed as a behaviour on the second checkbox field and will set the priority if the two checkbox values match.

Below is the script i used as well as a screenshot of its configuration.

Code:

// Required Imports
import com.atlassian.jira.component.ComponentAccessor
import static com.atlassian.jira.issue.IssueFieldConstants.PRIORITY

// Required Custom Fields
def priority = getFieldById(PRIORITY)
def cf1 = getFieldByName("DemoCheckbox")
def cf2 = getFieldByName("DemoCheckBox2")

// Get the select list values
def cfVal1 = cf1.getValue().toString()
def cfVal2 = cf2.getValue().toString()

// If the Custom field options match xx set the priority
if (cfVal1 =="Yes" && cfVal2 == "1"){
priority.setFormValue("High") 
}

Config:

image2016-6-7 16:50:46.png

I hope this helps.

Kristian

 

 

2 votes
JamieA
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.
June 7, 2016

You need to get Priority values from com.atlassian.jira.config.ConstantsManager#getPriorities, not OptionsManager.

Example - the following will set a priority of Lowest. If you are using JIRA 6 use priorityObjects not priorities:

import com.atlassian.jira.component.ComponentAccessor

def constantsManager = ComponentAccessor.getConstantsManager()
def priority = constantsManager.priorityObjects.findByName("Lowest")
getFieldById("priority").setFormValue(priority.id)

 

 

Vignesh June 7, 2016

Can you please provide me the script.

JamieA
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.
June 7, 2016

updated my answer

Terry Beavers August 8, 2016

Hi @Jamie Echlin [Adaptavist],

I am trying to use your suggestion to set the Priority field based on the value of a calculated field (Score), but getting an error, which I am apparently missing the boat on, so any help would be gratefully appreciated:

 

import com.atlassian.jira.component.ComponentAccessor
def constantsManager = ComponentAccessor.getConstantsManager()
def scoreVal = getFieldById('customfield_11159')

if (scoreVal == 25) {
    def priority = constantsManager.priorityObjects.findByName("Emergency")
	getFieldById("priority").setFormValue(priority.id)
}
else {
    if (scoreVal > 14 && scoreVal < 25) {
    def priority = constantsManager.priorityObjects.findByName("Critical")
	getFieldById("priority").setFormValue(priority.id)
    }
else {
    if (scoreVal > 5 && scoreVal < 15) {
    def priority = constantsManager.priorityObjects.findByName("Major")
	getFieldById("priority").setFormValue(priority.id)
    }
else {
    def priority = constantsManager.priorityObjects.findByName("Minor")
	getFieldById("priority").setFormValue(priority.id)
    }

Expecting a '}' but found ' '

I don't see how that is in issue, and even tried adding another closing bracket, but no luck.

Any thoughts, ideas, or suggestions?

Thanks in advance for your support,

Terry

JamieA
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.
August 8, 2016

as far as I can tell you are missing two closing braces: but I don't if you meant "else if" or else.

Eg you could use the following but I don't know if it's what you want:

import com.atlassian.jira.component.ComponentAccessor

def constantsManager = ComponentAccessor.getConstantsManager()
def scoreVal = getFieldById('customfield_11159')

if (scoreVal == 25) {

    def priority = constantsManager.priorityObjects.findByName("Emergency")
    getFieldById("priority").setFormValue(priority.id)

} else {
    if (scoreVal > 14 && scoreVal < 25) {

        def priority = constantsManager.priorityObjects.findByName("Critical")
        getFieldById("priority").setFormValue(priority.id)

    } else {
        if (scoreVal > 5 && scoreVal < 15) {

            def priority = constantsManager.priorityObjects.findByName("Major")
            getFieldById("priority").setFormValue(priority.id)

        } else {

            def priority = constantsManager.priorityObjects.findByName("Minor")
            getFieldById("priority").setFormValue(priority.id)

        }
    }
}
Terry Beavers August 9, 2016

Hi @Jamie Echlin [Adaptavist],

I see what you mean by the missing closing braces to support the "else" statements.  DOH!

So I have appended the two missing braces to the script, but now I am getting another error and a warning, with the warning looking like it is related to my JIRA version (7.0.10)

Error: Cannot find matching method com.onresolve.jira.groovy.user.FormField#compareTo(int)

The error is associated with the following two lines:

if (scoreVal > 14 && scoreVal < 25) {
if (scoreVal > 5 && scoreVal < 15) {

Warning: Since v7.0

And the warnings are associated with all of the "def priority" lines, such as:

def priority = constantsManager.priorityObjects.findByName("Emergency")

I also tried using "else if", but still same errors, which makes sense I guess:

 

import com.atlassian.jira.component.ComponentAccessor
def constantsManager = ComponentAccessor.getConstantsManager()
def scoreVal = getFieldById('customfield_11159')

if (scoreVal == 25) {
    def priority = constantsManager.priorityObjects.findByName("Emergency")
    getFieldById("priority").setFormValue(priority.id)
}
else if (scoreVal > 14 && scoreVal < 25) {
    def priority = constantsManager.priorityObjects.findByName("Critical")   
    getFieldById("priority").setFormValue(priority.id)
}
else if (scoreVal > 5 && scoreVal < 15) {
    def priority = constantsManager.priorityObjects.findByName("Major")
    getFieldById("priority").setFormValue(priority.id)
}
else {
    def priority = constantsManager.priorityObjects.findByName("Minor")
    getFieldById("priority").setFormValue(priority.id)
}

 

Any additional thoughts, ideas or suggestions?

Thanks for your support,

Terry

1 vote
Vasiliy Zverev
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.
June 3, 2016

Here is code example how to do this. Adopt it to your needs:

package Priority

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue

issue.setPriorityId("2")

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
cstFld1 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Name1"))
cstFld1 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Name2"))

//logic expession

ComponentAccessor.getIssueManager().updateIssue(ComponentAccessor.getJiraAuthenticationContext().getUser().directoryUser, issue, EventDispatchOption.ISSUE_UPDATED, false)
Vignesh June 3, 2016

I want to update it dynamically while creating the request itself. Please help for that

Vasiliy Zverev
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.
June 7, 2016

Do you want to do it on creation crean?

TAGS
AUG Leaders

Atlassian Community Events