Assign User and Team

Vivo Bandito
Contributor
November 8, 2022

Hello,

I'm looking for a little help with scriptrunner and post functions. So far the assigning the user is not an issue as assigning as current user is a pretty easy post script. Where I am running into a problem is assigning the Custom Field "Assigned Team".  I found a script but that references our internal directories but I'd like to translate that into some action. Any and all help is appreciated. 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager

def userUtil = ComponentAccessor.getUserUtil()

def cf = customFieldManager.getCustomFieldObject("customfield_14202")  //AssignedTeam
def assignedTeam = issue.getCustomFieldValue(cf).toString()

if(userUtil.getGroupNamesForUser(currentUser.name).contains("DEPT - CORP - Service Desk - Managers") && assignedTeam == "Service Desk")
return true
if(userUtil.getGroupNamesForUser(currentUser.name).contains("DEPT - CORP - Service Desk - Supervisors") && assignedTeam == "Service Desk")
return true

1 answer

1 accepted

0 votes
Answer accepted
Florian Bonniec
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 8, 2022

Hi @Vivo Bandito 

 

What are you trying to achieve ?

This looks more like a condition script.

 

Regards

Vivo Bandito
Contributor
November 8, 2022

I'm looking to automatically update the custom field "Assigned Team" based off of the assigned users groups they are in. Example: Assigned users group contains "Dept-corp-service_desk" as one of the groups and  would update the custom field  "Assigned Team" to "Service Desk" as a post function. This would be an if else statement list listing out which groups are assigned to which assigned team.

The above listed script is a condition script. I had listed it because it referenced how I would get to the users groups they are associated with. I'd be looking for assigned user instead of current user because we sometimes have our team leads assign other users to the ticket.

Florian Bonniec
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 8, 2022

Something like that should work

 

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.fields.CustomField

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.issue.CustomFieldManager

import com.atlassian.jira.issue.ModifiedValue

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

import com.atlassian.jira.issue.index.IssueIndexingService

import com.atlassian.jira.util.ImportUtils

def userUtil = ComponentAccessor.getUserUtil()

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def cf = customFieldManager.getCustomFieldObject("customfield_14202")  //AssignedTeam

//def assignedTeam = issue.getCustomFieldValue(cf).toString()

boolean isIndex = ImportUtils.isIndexIssues();

ImportUtils.setIndexIssues(true);

IssueIndexingService issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)




if(userUtil.getGroupNamesForUser(currentUser.getUsername()).contains("DEPT - CORP - Service Desk - Managers")){

    def availableOptions = ComponentAccessor.optionsManager.getOptions(cf.getRelevantConfig(issue))

    def optionToSet = availableOptions.find { it.value == "Service Desk" }

    cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), optionToSet), new DefaultIssueChangeHolder())

    issueIndexingService.reIndex(issue);

    ImportUtils.setIndexIssues(isIndex);

}else{




}
Vivo Bandito
Contributor
November 8, 2022

Hey @Florian Bonniec ,

I was able to get that working using the code provided. The current code is able to use the current user groups. Is there a way to also use the user who is being assigned instead of the current user? 

Additionally do you have a good resources for coding in scriptrunner? I'd like to learn more but am currently just modifying available snippets available in my environment. 

Florian Bonniec
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 9, 2022

Hi @Vivo Bandito 

 

I usually check the javadoc https://docs.atlassian.com/software/jira/docs/api/7.6.1/

You can also checked some example on adaptavist site.

https://library.adaptavist.com/

 

You can replace 

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

By

def currentUser = issue.getAssignee() 

Vivo Bandito
Contributor
November 9, 2022

A scholar and a saint!

Suggest an answer

Log in or Sign up to answer