For loops in Behaviors?

Bellina Bui March 24, 2023

We are adding a for loop in a Behavior to collect the option IDs of a multi-select field. 

On Script Console, the for loop works -- it gathers the option IDs and puts it in a list. 

However, when added to a Behavior, the for loop breaks the entire behavior. Dx

 

Has anyone had success with a for loop on Behaviors? For reference here is our behavior: 

 

import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.issue.search.SearchProvider
import static org.apache.commons.collections.CollectionUtils.*
final productLine = "Product Line(s)"
if (getBehaviourContextId() == "create-epic-button") {
//Testing
getFieldByName("Summary").setFormValue("Testing")
// Get some components
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def issueManager = ComponentAccessor.issueManager
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def optManager = ComponentAccessor.getOptionsManager()
//Parent Link
def parentLink = getFieldByName("Parent Link")
def parentLinkValue = parentLink.getValue()
def contextIssue = issueManager.getIssueObject(getContextIssueId())
parentLink.setFormValue("${contextIssue.key}")
//Product Line
def productLineName = ComponentAccessor.customFieldManager.customFieldObjects.findByName(productLine) //Get Objects
def productLineList = getFieldByName(productLine).getValue() as List //Get Values as List
log.warn("Product Line List: $productLineList")
//Get Product Line Field Config
def productLineConfig = productLineName.getRelevantConfig(contextIssue)
log.warn("Product Line Field Configuration: $productLineConfig")
//Get Product Line Field Value Options
def productLineOptions = optManager.getOptions(productLineConfig)
log.warn("Product Line Options: $productLineOptions")
def targetValues = []
for (i in productLineList) {
log.warn "processing $contextIssue value = $i"
//Convert i to a string and define it in a variable
def newVal = i.toString()
//Get Product Line Option ID
def productLineOptionID = productLineOptions. find { it.value == newVal }
log.warn("Product Line Option ID: $productLineOptionID")
//Add to targetValues List
targetValues += productLineOptionID.optionId
log.warn("Product Line Target Values: $targetValues")
getFieldByName("Product Line(s)").setDescription("Woooorrkkkkk")
}
} else {
}

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Peter-Dave 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 24, 2023

There is no reason for a for-loop to break anything. 

But how does the for-loop break your behaviour?

What errors are you getting?

What gets outputted to your log?

Bellina Bui March 27, 2023

Hi Peter just validated that in fact you can use for loops in behaviors! 

 

When I was trying to grab the product line field I wasn't referencing the originated issue, but rather the destination issue. Corrected the below:

def productLineValue = contextIssue.getCustomFieldValue("Product Line(s)") as List
TAGS
AUG Leaders

Atlassian Community Events