Hi...I have a script to set the Approvers field that comes with Jira Service Desk to either the component lead and project lead or only to the project lead in a post function on the create transition and it works perfectly. However, the same script as a listener does not work (I need it as a listener because the post function does not execute when an issue is moved to the project). It just does not set the Approvers field. I cant figure out what I have to modify so it works as script listener as well.
Thank you for your assistance. Script below (as I said it works as a post-function but as a listener it does not set the Approvers field)
import com.atlassian.jira.ComponentManager import com.atlassian.jira.project.Project import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.type.EventDispatchOption
projectlead = issue.getProjectObject().getProjectLead()
def components = issue.componentObjects.toList()
if (components)
{ componentlead = components?.first()?.componentLead
}
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField customFieldApprovers = customFieldManager.getCustomFieldObjectByName( "Approvers" )
if (components && componentlead != projectlead) { issue.setCustomFieldValue(customFieldApprovers, [projectlead,componentlead])
}else{
issue.setCustomFieldValue(customFieldApprovers, [projectlead])
}
Here is refactored code. Should work, a did not test it. Main point - multiselect custom field needs List<Applicationuser> object. So, you could start with it.
import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
//Uncomment for usage into script console to get specified issue
//Issue issue = ComponentAccessor.getIssueManager().getIssueObject("issue key");
List<ApplicationUser> approvers = new ArrayList<>();
approvers.add(issue.getProjectObject().getProjectLead())
ProjectComponent[] components = issue.getComponentObjects().toArray()
ApplicationUser componentlead;
if (components.length > 0) {
if(!approvers.contains(components[0].getLead()))
approvers.add(components[0].getComponentLead());
}
issue.setCustomFieldValue(
ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName( "Approvers" ),
approvers)
Hi Vasiliy....thank you for your assistance...it is still not working....it just does not do the assignment to the Approvers field for some reason. The last line of code, the one that actually should assign the list of values to the Approvers field is not executing for some reason. That is the same issue I am having with my code in a listener. In a post-function works fine. Any thoughts?
Thank you for your help
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If you need to set assigne then you should use method
issue.setAssigneeId()
Perhaps into postfunctions list you have more one to set assgnee.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
I'm trying to use the same code to assign component leads as approvers. It works fine if there's only a single component selected but once you select more than one, its only pulling the first lead as an approver.
import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
List<ApplicationUser> approvers = new ArrayList<>();
// Not needed for my use case
//approvers.add(issue.getProjectObject().getProjectLead())
ProjectComponent[] components = issue.getComponentObjects().toArray()
ApplicationUser componentlead;
if (components.length > 0) {
if(!approvers.contains(components[0].getLead()))
approvers.add(components[0].getComponentLead());
}
issue.setCustomFieldValue(
ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName( "Approvers" ),
approvers)
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.