Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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

If you need to set assigne then you should use method 

issue.setAssigneeId()

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

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
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you