Forums

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

Updating a custom field with Groovy Script via Post Function

Brent Green August 6, 2019

Hello and Help...  I'm new to groovy and the only one in my group that can work on this.

We have a custom field called 'agile-team' that we would like to populate based on another field called 'Product/s'.   Product/s is a multi select field during issue creation.  This example is to check a list of Abend-AID products and if a match and if the agile-team is not already 'Abend-AID' then set it to 'Abend-AID'   

Here is a small part of what I've got.    I'm concerned about the updateValue code, is this correct? or are we better with the setCustomFieldValue function?

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue


CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
Issue issue = issue

def numproducts = ((getCustomFieldValue("Product/s")?.size())?:0) as Integer
//println "numproducts="+numproducts;
def products = getCustomFieldValue("Product/s")
def agile-team = customFieldManager.getCustomFieldObjectByName("Agile Team/s")
//multi-user custom field to be populated with product families involved in the issue
CustomField productLines = customFieldManager.getCustomFieldObjectByName("Product Line/s");

def result = ""
def i =0
for (i=0; i < numproducts; i++){
def product = products[i].toString()
switch (product) {
case ["Abend-AID","Abend-AID Common","Abend-AID Fault Analytics","Abend-AID Fault Analytics Distributed","Abend-AID for CICS","Abend-AID XLS"]:
if (result.contains("Abend-AID")) {break}
else {
result = agile-team.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(agile-team), "Abend-AID"),new DefaultIssueChangeHolder())
break }

 

Thanks,   This community is the best!

3 answers

0 votes
Brent Green August 28, 2019

Thanks, I was able to resolve the coding issue.  Now during execution i'm getting

2019-08-28 13:18:06,413 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2019-08-28 13:18:06,413 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: CWE-150887, actionId: 1, file: <inline script>
java.lang.ClassCastException: com.atlassian.jira.issue.customfields.option.LazyLoadedOption cannot be cast to java.util.Collection
 at com.atlassian.jira.issue.customfields.impl.AbstractMultiCFType.createValue(AbstractMultiCFType.java:39)
 at com.atlassian.jira.issue.fields.ImmutableCustomField.createValue(ImmutableCustomField.java:693)
 at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:410)
 at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396)
 at com.atlassian.jira.issue.fields.OrderableField$updateValue.call(Unknown Source)
 at Script160.run(Script160.groovy:37)

 

Recap on the backgrond.....

  I have a custom field called Product/s that can have multiple values, based on a value we want to pre-fill in another custom field called Agile-Team/s. that has a selection list.   Some previous examples led me to this code.....    

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
Issue issue = issue

def productField = customFieldManager.getCustomFieldObjectByName("Product/s")
def products = (List) issue.getCustomFieldValue(productField)
def numproducts = products.size();
def String abendaid = "Abend-AID"

def agileteam = customFieldManager.getCustomFieldObjectByName("Agile Team/s")
CustomField productLines = customFieldManager.getCustomFieldObjectByName("Product Line/s");
def groupManager = ComponentAccessor.getGroupManager()
def availableOptions = ComponentAccessor.optionsManager.getOptions(agileteam.getRelevantConfig(issue))


String result = ""
def i =0
for (i=0; i < numproducts; i++){
def product = products[i].toString()
switch (product) {
    case ["Abend-AID","Abend-AID Common","Abend-AID for CICS","Abend-AID XLS"]:
      if (result.contains("Abend-AID")) {break}
     else { def optionToSet = availableOptions.find { it.value == abendaid }
       result = agileteam.updateValue(null, issue, new            ModifiedValue(issue.getCustomFieldValue(agileteam), optionToSet),new DefaultIssueChangeHolder());
break }

 

I suppose there is an easier / better way...

 

Thanks for the assistance.  Learning alot

0 votes
Ilya Turov
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 6, 2019

I don't really understand if "Agile Team/s" field is text or what, but if it is, then this piece might be what you need:

import com.atlassian.jira.component.ComponentAccessor

def productsField = customFieldManager.getCustomFieldObjectsByName("Product/s")[0]
def agileTeamField = customFieldManager.getCustomFieldObjectsByName("Agile Team/s")[0]

def products = issue.getCustomFieldValue(productsField)*.value
if (products.any {it.contains("Abend-AID")}) {
issue.setCustomFieldValue(agileTeamField, "Abend-AID")
}

since it's executing on postfunction you don't need to store or update anything while you postfunction is before the one where the issue is stored to database

basically, if you want to start scripting, you can go to ComponentAccessor page since it's what you gonna use the most since it gives access to most of the methods and just start looking at what methods use what kind of arguments, what they return.

0 votes
Tanya Gordon
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 6, 2019

Hi,

Your solution looks fine but it will write the values to agile-team field without a historical record. Is this on purpose? 

Here are the 2 options you can use to update a custom field:

1) without a historical record

customFieldTarget.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customFieldTarget), value), changeHolder)

2) with a historical record

MutableIssue mIssue = issue as MutableIssue
mIssue.setCustomFieldValue(customFieldTarget, value)
ComponentAccessor.getIssueManager().updateIssue(user, mIssue, EventDispatchOption.ISSUE_UPDATED, false)
issue.store()

Hope this helps :-)

Tanya

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events