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;
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.