Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Assign by custom field to a project rol or to a user.

Adolfo Rodriguez Lopez May 21, 2020

Hi.

We have a problem with this groovy.Seem´s like work, but doesn´t.

We need to assign in a transition depend by value of a custom field.The custom field name is "So_stage"

 

When So_stage value = "Acquisition" we need to assign the issue to BD rol

When So_stage value = "Execution" we need to assign the issue to PM rol

When So_stage value = "1st_mp_order" we need to assign the issue to Supply chain rol

When So_stage value = "MP" we need to assign the issue to user macarena.gonzalez

 

This is the script.

 

import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.UpdateIssueRequest
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.bc.projectroles.ProjectRoleService;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.project.ProjectManager;
import com.atlassian.jira.security.roles.ProjectRole;
import com.atlassian.jira.security.roles.ProjectRoleActors;
import com.atlassian.jira.security.roles.ProjectRoleManager;
import com.atlassian.jira.security.roles.RoleActor;

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();

CustomField cf = customFieldManager.getCustomFieldObjectByName("SO_Stage");
String dept_value = issue.getCustomFieldValue(cf).toString();
IssueManager issueMgr = ComponentAccessor.getIssueManager();

ProjectRoleManager projectRoleManager = ComponentAccessor.getComponentOfType(ProjectRoleManager.class) as ProjectRoleManager

// Get BD Role
ProjectRole rsbdAuth = projectRoleManager.getProjectRole("BD")
ProjectRoleActors rsbdActors = projectRoleManager.getProjectRoleActors(rsbdAuth, issue.getProjectObject())
def rsbdAssignee = rsbdActors.getUsers().toList().first()

// Get PM Role
ProjectRole rspmAuth = projectRoleManager.getProjectRole("PM")
ProjectRoleActors rspmActors = projectRoleManager.getProjectRoleActors(rspmAuth, issue.getProjectObject())
def rspmAssignee = rspmActors.getUsers().toList().first()

// Get Supply Chain Role
ProjectRole rsscAuth = projectRoleManager.getProjectRole("Supply Chain")
ProjectRoleActors rsscActors = projectRoleManager.getProjectRoleActors(rsscAuth, issue.getProjectObject())
def rsscAssignee = rspmActors.getUsers().toList().first()

//usuario por defecto si no lo pones, no funciona
def loggedUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
def assigneeName = "usuario.generico"

//Set Acquisition Custom field

if (dept_value.equals("Acquisition"))
{
log.error("assignee set")
issue.setAssignee(rsbdActors.getUsers().toList()?.first())
}

//Set Execution Custom field

if (dept_value.equals("Execution"))
{
log.error("assignee set")
issue.setAssignee(rspmActors.getUsers().toList()?.first())
}

//Set 1st_MP_Order Custom field

if (dept_value.equals("1st_MP_Order"))
{
log.error("assignee set")
issue.setAssignee(rsscActors.getUsers().toList()?.first())

//Set MP as user
}

if (dept_value.equals("MP")){
assigneeName = "macarena.gonzalez";
}

MutableIssue mutableIssue = issueMgr.getIssueObject(issue.id);
ApplicationUser ciuser = ComponentAccessor.getUserManager().getUserByName(assigneeName);
issue.assignee = ciuser;
mutableIssue.setAssignee(ComponentAccessor.getUserManager().getUserByName(assigneeName))
issueMgr.updateIssue(loggedUser, mutableIssue, EventDispatchOption.DO_NOT_DISPATCH, false);

 

 

seems like works, but doesn´t

2 answers

0 votes
Adolfo Rodriguez Lopez May 25, 2020

mistakes.jpgHi!

Thanks for the help but the script doesn´t work.I send you the errors.

 

Many thanks again!

0 votes
PD Sheehan
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.
May 22, 2020

Since you mentioned "need to assign in a transition", I'm assuming you have this script in a post Function?

If so, what may be happening is that you update the issue with your script, then the issue object already present in the transition is used to overwrite the change you just made.

Without making any changes to your script, you can move the order of the post-function to put it at the end.

Alternatively, leave it where it is, but don't use the mutable issue. Just use the provided issue object (remove the last 2 lines) to update that object area.

Here is a simplified script that you may like:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
def customFieldManager = ComponentAccessor.customFieldManager

def cfName = 'SO_Stage'

def cf = customFieldManager.getCustomFieldObjectsByName(cfName).first()
def dept_value = issue.getCustomFieldValue(cf).value

def getFirstUserOfRole(String roleName){
def projectRoleManager = ComponentAccessor.getComponentOfType(ProjectRoleManager.class) as ProjectRoleManager
def role = projectRoleManager.getProjectRole(roleName)
if (role){
def actors = projectRoleManager.getProjectRoleActors(role, issue.projectObject)
(actors.users) ? actors.users.toList().find{it.active} : null
}
}

def departmentRoleMap = [
Acquisition : [type:'role', roleName:'BD'],
Execution : [type:'role', roleName:'PM'],
MP : [type:'user', userName:'macarena.gonzalez'],
'1st_MP_Order' : [type:'role', roleName:'Supply Chain']
]
def assignee
if(departmentRoleMap[dept_value].type == 'role'){
assignee = getFirstUserOfRole(departmentRoleMap[dept_value].roleName)
} else {
assignee = ComponentAccessor.userManager.getUserByName(departmentRoleMap[dept_value].userName)
}
(assignee) ? issue.assignee = assignee : log.warn("No assignee found for $dept_value")
Adolfo Rodriguez Lopez May 25, 2020

Thanks for the help but the script doesn´t work.

 

I send you the errors

 

mistakes.jpg

PD Sheehan
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.
May 25, 2020

"Static type checking" errors are often safe to ignore. It just means the code editor is unable to confirm the class of an object, so it can't be sure what methods are allowed.

The important information is what you get in the logs when you actually try to run the script.

Other than a small error in how issue was being passed to the short function I created called getFirstUserOfRole, this should be close to working.

But we can remove the static type errors with a little bit of effort:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import com.atlassian.jira.user.ApplicationUser
def customFieldManager = ComponentAccessor.customFieldManager

def cfName = 'SO_Stage'
def cf = customFieldManager.getCustomFieldObjectsByName(cfName).first()
def dept_value = issue.getCustomFieldValue(cf) as LazyLoadedOption

ApplicationUser getFirstUserOfRole(String roleName, MutableIssue issue){
def projectRoleManager = ComponentAccessor.getComponentOfType(ProjectRoleManager.class)
def role = projectRoleManager.getProjectRole(roleName)
if (role){
def actors = projectRoleManager.getProjectRoleActors(role, issue.projectObject)
(actors.users) ? actors.users.toList().find{it.active} : null
}
}

def departmentRoleMap = [
Acquisition : [type:'role', roleName:'BD'],
Execution : [type:'role', roleName:'PM'],
MP : [type:'user', userName:'macarena.gonzalez'],
'1st_MP_Order' : [type:'role', roleName:'Supply Chain']
]
def assignee
if(departmentRoleMap[dept_value].type == 'role'){
assignee = getFirstUserOfRole(departmentRoleMap[dept_value].roleName as String, issue)
} else {
assignee = ComponentAccessor.userManager.getUserByName(departmentRoleMap[dept_value].userName as String)
}
(assignee) ? issue.assignee = assignee : log.warn("No assignee found for $dept_value")

Suggest an answer

Log in or Sign up to answer