Hi
I was wondering if anyone could help, I'm currently trying to write a post function script that updates a checkbox field on a parent issue. The script takes the environment value from a text field in the subtask called "Release Environment" and checks the equivalent box on the parent tick checkbox field "App Deployed To Environment"
So far I've come up with this, which seems to run fine without any errors however the field doesn't get updated. Am I missing anything obvious?
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.MutableIssue
def parent = issue.getParentObject() as MutableIssue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()
def issueManager = ComponentAccessor.getIssueManager()
def releaseEnvironment = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Release Environment'}
def releaseEnvironmentVal = issue.getCustomFieldValue(releaseEnvironment)
def parentAppDeployedTo = customFieldManager.getCustomFieldObjects(parent).find {it.name == 'App Deployed To Environment'}
def parentAppDeployedToValue = parent.getCustomFieldValue(parentAppDeployedTo)
def fieldConfig = parentAppDeployedTo.getRelevantConfig(parent)
def options = optionsManager.getOptions(fieldConfig)
def optionsToSet = options.findAll { it.toString() == releaseEnvironmentVal }
parent.setCustomFieldValue(parentAppDeployedTo, [optionsToSet, parentAppDeployedToValue])
parent.setCustomFieldValue(parentAppDeployedTo, [optionsToSet, parentAppDeployedToValue])
I think, what you mean to do with this line is to ADD the list of environments listed on the child to the list of Environments already on the parent.
So instead, I'd advise the following
def newOptions = options.findAll { it.toString() == releaseEnvironmentVal }
def optionsToSet = newOptions + parentAppDeployedToValue
optionsToSet = optionsToSet.unique{it.optionId} // optionsToSet.unique() may work too.. I haven't tested
parent.setCustomFieldValue(parentAppDeployedTo, optionsToSet)
Then, you will still need to update the issue as mentioned by @Ivan Tovbin and you will also need to index your parent issue.
import com.atlassian.jira.issue.index.IssueIndexingService
ComponentAccessor.getComponent(IssueIndexingService.class).reIndex(parent)
Hi guys,
Thanks for the responses.
@PD Sheehan yes you are right I'm trying to a add a value from the child ticket to a existing field (may already be populated).
I'm still getting a error regards the
newOptions + parentAppDeployedToValue
Error - jira java.util.list #plus(t) arguments
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What field type is "Release Environment" field?
Is that a checkbox too?
Then your problem is with:
def newOptions = options.findAll { it.toString() == releaseEnvironmentVal }
I suspect it returned null because it.toString() would never match releaseEnvironmentVal (which is a list of options)
Try this instead:
def newOptions = options.findAll { it.value in releaseEnvironmentVal*.value}
if(newOptions){
def optionsToSet = newOptions + parentAppDeployedToValue
optionsToSet = optionsToSet.unique{it.optionId} // optionsToSet.unique() may work too.. I haven't tested
parnt.setCustomFieldValue(parentAppDeployedTo, optionsToSet)
}
Since releaseEnvironmentVal is a list of options from a different field than parentAppDeployedTo field and the two main fields on an option are optionId and value, then you need to find all the matching options by comparing the values.
Using *.value will return an array of value strings and we'll try to find a match for each of the options values (it.value) that are included in that array.
And wrapping the whole thing in an if will prevent attempting to update the parent when no match exists.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@PD Sheehan releaseEnvironmentVal is actually a text field so that's ok. However thanks for the advice on data types I've now worked out the issue was -
newOptions and parentAppDeployedToValue need to be the same option type to + them together.
I've converted parentAppDeployedToValue into ArrayList and found the corresponding options.
def parentAppDeployedToVal = parent.getCustomFieldValue(parentAppDeployedTo)
def newOption = options.findAll { it.toString() == releaseEnvironmentVal }
def list = new ArrayList<String>()
for (item in parentAppDeployedToVal) {
log.debug "item " + item
list.add(item.toString())
}
def currentOption = options.findAll { it.value in list }
log.debug "List - " + list
log.debug "newOption - " + newOption
log.debug "currentOption - " + currentOption
ModifiedValue mVal = new ModifiedValue(parentAppDeployedToVal, currentOption + newOption);
parentAppDeployedTo.updateValue(null, parent, mVal, new DefaultIssueChangeHolder());
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Sam Morris
Thing is, after you call any setter method from MutableIssue interface (that would be setCustomFieldValue() in your case), you need to update that issue to persist those changes into the database. Here's how I do it usually:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.user.ApplicationUser
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ComponentAccessor.getIssueManager().updateIssue(currentUser, issue, EventDispatchOption.ISSUE_UPDATED, false)
Another thing to consider is that according to your code your are passing an array of two elements as your field's new value. Problem is that the first element optionsToSet is an array itself and that might be the reason why your field's value is not being set properly. You need to pass an array of options and you are passing an array of an array and an option, which is wrong.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Sam Morris ,
AFAIK setCustomFieldValue is not advised. Please try this instead :
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
parentAppDeployedTo.updateValue(null, issue, new ModifiedValue(parentAppDeployedToValue, optionsToSet), new DefaultIssueChangeHolder())
Antoine
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The problem with this approach is that the parent issue will not show that change in the change history.
Also, you have no opportunity to decide whether to dispatch an event for that issue. Maybe the watchers want to know about the change.
But really, I'm not sure that there is a best approach for changing a single custom field.
There is 3 that I'm aware of:
I tend to use the issueManager because that's the one that I first learned. But issueService is probably more robust. And the customField.updateValue() seems to be at the "lowest" level (i.e. least validations).
I think it really depends on what you want to do and how much of the default behavior (compared to the Web UI) you want to achieve.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Exactly,
Most of the time I will want to update the field "underwater" and leave no trace in the history. I also use issueManager sometimes when to use an event. If I remember correctly IssueService is indeed the proper way, I have been using it only with Insight fields because they were not updating correctly.
Thanks for the input !
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Join us to learn how your team can stay fully engaged in meetings without worrying about writing everything down. Dive into Loom's newest feature, Loom AI for meetings, which automatically takes notes and tracks action items.
Register today!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.