Forums

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

Script Condition not working for Insight Post Function

Tom Brown
Contributor
July 21, 2022

Hi Community,

I am using the 'Set the value of an object attribute with a predefined value' Insight Post Function to set an Insight Object Attribute based on the value of a single line text custom field.

The Post Function works fine but now I want to add a condition as I have several of these post function and only one should fire depending on the value of another custom field (select list).

It appears that the problem is to do with the custom field being a select list as I changed it to another field (text) and it worked. Should I be retrieving the value differently for a select list? I added logging and ran this as a custom script post funtion and it returned the correct value so scratching my head now!

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

def cfAttribute = ComponentAccessor.customFieldManager.getCustomFieldObject(11902) // 'Attribute selector' Custom Field ID
def cfAttributeValue = issue.getCustomFieldValue(cfAttribute)

//if 'Attribute selector' value is as required then evaluate to true
if (cfAttributeValue == "Required Value" ) {

return true;

}

return false;

 

1 answer

1 accepted

1 vote
Answer accepted
PD Sheehan
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.
July 22, 2022

For a Single Select field getCustomFieldValue() returns an instance of com.atlassian.jira.issue.customfields.option.Option 

So all you need to do is get the "value" property of that Option object.

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

def cfAttribute = ComponentAccessor.customFieldManager.getCustomFieldObject(11902) // 'Attribute selector' Custom Field ID
def cfAttributeValue = issue.getCustomFieldValue(cfAttribute) as Option

//if 'Attribute selector' value is as required then evaluate to true
return cfAttributeValue.value == "Required Value"

Also, you don't need the if-else blocks. Just return the results of the comparison. It will return either true or false.

Tom Brown
Contributor
July 22, 2022

@PD Sheehan works great, thanks!

jun lee
Contributor
March 26, 2023

Hi @PD Sheehan 

I added below script as a action at the automation rule.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.issue.context.IssueContextImpl;
import com.atlassian.jira.issue.fields.config.FieldConfig;
import java.util.ArrayList;
import java.util.stream.Collectors;
import com.atlassian.jira.issue.customfields.option.Option

 

def groupManager = ComponentAccessor.getGroupManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

 

def ENM_SYSTEM = ComponentAccessor.customFieldManager.getCustomFieldObject(13801) // 'Attribute selector' Custom Field ID
def cfAttributeValue = issue.getCustomFieldValue(ENM_SYSTEM) as Option

 

if (cfAttributeValue == "SYSTEM_A") {
def group = groupManager.getGroup("(ENM)SYSTEM_A") // user group
def usersInGroup = groupManager.getUsersInGroup(group) // get the users in that group
def fieldToSet = customFieldManager.getCustomFieldObject("customfield_13809") // enm_user_multi custom field
fieldToSet.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(fieldToSet), usersInGroup), new DefaultIssueChangeHolder()) // update users
}

 

Although audit log is sucess, custom field is not updated. When I delete 

def ENM_SYSTEM = ComponentAccessor.customFieldManager.getCustomFieldObject(13801) // 'Attribute selector' Custom Field ID
def cfAttributeValue = issue.getCustomFieldValue(ENM_SYSTEM) as Option

 

if (cfAttributeValue == "SYSTEM_A") {

, it works.

 

Can you help me how to fix?

PD Sheehan
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.
March 28, 2023

I am not sure that updating a custom field this way in project automation via groovy is a good idea.

Also, I don't know if you rick getting concurrent update issues or if the change will be logged and indexed.

So use at your own risk.

But for the logic to work, you need to compare the "Option.getValue()" result against your SYSTEM_A string. E.g:

if (cfAttributeValue.value == "SYSTEM_A"){
//... update the field
}

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events