script listener to set Approvers field to component lead and project lead does not work

Tomas Arguinzones Yahoo
Contributor
March 26, 2018

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])

}

 

1 answer

0 votes
Vasiliy Zverev
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 26, 2018

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)
Tomas Arguinzones Yahoo
Contributor
March 27, 2018

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

Vasiliy Zverev
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 28, 2018

If you need to set assigne then you should use method 

issue.setAssigneeId()

Perhaps into postfunctions list you have more one to set assgnee. 

Arron Brice
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 11, 2021

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)

Suggest an answer

Log in or Sign up to answer