Hi all!
I've written two very similar scripts for adding values to an attribute in Jira Assets.
First one works as expected - add all values (multiple) to the attribute:
def app_1, org_1, app_2, org_2
Assets.search('objectType = Items AND Application IS NOT empty AND Organisation IS NOT empty').each { item ->
app_1 = item.getAttributeValues('Application')[0].value
org_1 = item.getAttributeValues('Organisation')[0].value
Assets.search('objectType = Budgets').each { budget ->
app_2 = budget.getAttributeValues('Application')[0].value
org_2 = budget.getAttributeValues('Organisation')[0].value
if (app_2 == app_1 && org_2 == org_1) {
budget.update {
setAttribute('Items') {
add(item)
}
}
}
}
}
def items = []
def budgets = []
Assets.search('objectType = Items AND Application IS NOT empty AND Organisation IS NOT empty').each { item ->
items.add(item)
}
Assets.search('objectType = Budgets').each { budget ->
budgets.add(budget)
}
budgets.each { budget ->
items.each { item ->
if (budget.getAttributeValues('Application')[0].value == items.getAttributeValues('Application')[0].value &&
budget.getAttributeValues('Organisation')[0].value == items.getAttributeValues('Organisation')[0].value) {
// Not working as exptected with multiple objects - newly added attribute replaces the previous one
budget.update {
setAttribute('Items') {
add(item)
}
}
}
}
}
Does anybody have an idea what is going on in the background?
Could you try to add some logging parameters after this:-
Assets.search('objectType = Budgets').each { budget ->
budgets.add(budget)
}
log.warn "======== ${budgets.size()}"
budgets.each {
log.warn "====== Budget Content: ${i}"
}
and see what is printed out.
The other item I have observed is that when you are doing the iteration, you are using the each approach to do the iteration. I suspect that when using each, it is only storing the last value.
Instead, could you try removing the def budgets = [] variable, update the Assets.search iteration to collect(), and assign it to the budgets variable as shown below and see if there is any difference?
def budgets = Assets.search('objectType = Budgets').collect()
log.warn "======== ${budgets.size()}"
budgets.each {
log.warn "====== Budget Content: ${i}"
}
I hope this helps to answer your question. :-)
Thank you and Kind regards,
Ram
Hi @Ram Kumar Aravindakshan _Adaptavist_ ,
thank you for your suggestion.
The collections are identical using either of the methods. Actually the part of the code with odd behaviour is this one:
budget.update {
setAttribute('Items') {
add(item)
}
}
When I check the history of Assets objects, I can see the value of the attribute has changed multiple times (iteration) when running the script (second one), but only the last value is stored - the previous one is always replaced.
On the other hand, when running the first script, the value is added to the attribute and all the other are preserved.
I suspect it has something to do with how these objects are stored in memory, but not sure.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If I read correctly, when you search, you store as is values at that iteration.
First method re-searches everything on every iteration, so you are always updating latest values.
Second works from your saved values, so on every iteration, your end up with whatever you added on the last iteration.
HAPI made many quality-of-life improvements, many more to be added, I hope.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.