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

auto assignment of issue to Reporter's manager

Mahdi Mohammadnejad May 29, 2023

Hi guys

I'm using Jira service management data center. 

I want to assign issues in 1st step of workflow to reporter's manager automatically to approve issue. Information of users and their managers are in Asset. In assets I have an object schema with "Employee" object type. Information of users are under this object type. In this object type i have 2 attributes. "Username" and "Manager".

I want to fill "manager_customfield" automatically.

For example Reporter is "User1", if "Username" attribute is equal with reporter then fill "manager_customfield" with "Manager" attribute.

To do this i find a script but it's not complete and i couldn't run it. could you please help me?

Or do you have any other suggestion to do this?

 

import org.apache.catalina.Manager
import com.atlassian.sal.api.user.UserManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

def reporterUsername = issue.reporter?.name

def manager = getManagerFromAssets(reporterUsername)

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManafer = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def cf = ComponentAccessor.customFieldManager.getCustomFieldObject("customfield_10500")

issue.setCustomFieldValue(cf,manager)
issueManafer.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)


def getManagerFromAssets(reporterUsername) {
    def assetObjects = queryAssets()
    def manager = assetObjects.find { it.username == reporterUsername }?.manager
    return manager
}

def queryAssets() {
    def assetService = ComponentAccessor.getComponent("***")
    return assetService.getAllAssets()
}

 

 

 

1 answer

1 vote
Taha El Majidi
Contributor
May 30, 2023

Hi @Mahdi Mohammadnejad ,

If you are using ScriptRunner, you can start using Insight Manager.

Once you set the insight manager you can use the following script in a scripted listener:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.atlassian.jira.event.type.EventDispatchOption
import customRiadaLibraries.insightmanager.InsightManagerForScriptrunner
 
//Get the InsightManager
@WithPlugin("com.riadalabs.jira.plugins.insight")
InsightManagerForScriptrunner im = new InsightManagerForScriptrunner()
 
//Get necessary managers
IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.customFieldManager
 
//Get the event issue of the listener
def issue = event.issue
//Get a user (an admin for example or the current user)
def adminUser = ComponentAccessor.userManager.getUserByName("admin")
 
//Get your manager custom field
CustomField managerCF = customFieldManager.getCustomFieldObject("manager_customfield ID")
 
//Get the issue reporter
def reporter = issue.reporter.displayName
//Get the employee using Insight manager
def  employee = im.iql(<ID of your Asset schema>,"objectTypeId = <ID of your object type Employee> AND Name =\"${reporter}\"")[0]

//Get the manager from the employee object using insight manager
def manager = im.getObjectAttributeValues(employee, "Manager")[0] as String

//Get the jira user using manager
def user = ComponentAccessor.getUserManager().getUserByName(manager)

 
 
//Set the custom field with the manager
issue.setCustomFieldValue(managerCF, user)
 
//update the issue
issueManager.updateIssue(adminUser,issue, EventDispatchOption.ISSUE_UPDATED,false)

I did not test the script si if you have any errors or questions dont hesitate to ask again.

Taha 

Taha El Majidi
Contributor
May 30, 2023

Or you can simply use Insight post functions and go for "Set a Jira custom field with the attribute value from a selected object"

Mahdi Mohammadnejad May 31, 2023 edited

Thanks for your answer. I don't want to select object manually.

Mahdi Mohammadnejad May 31, 2023

@Taha El Majidi 

If i use post function how could i update t during workflow?

I mean if i change reporter after creation of issue how could i update atribute of object in custom field?

Taha El Majidi
Contributor
June 1, 2023 edited

Hi @Mahdi Mohammadnejad

To answer you first comment, you don't have to choose manually. The aim is to use the post function to update the manager field based on the reporter field

To answer your second comment the best way to do it is to use the scripted listener I sent before. But if you are not confortable with scripting, here's a suggestion :

  1. Create an Insight read only field call it "Employee" and configure it as follows :
    • Employee_Field_Configuration.png
  2. Remove reporter field from the edit screen and put it in a transition screen
  3. Create a self reflecting transition to edit the reporter field :
    • self_Reflecting_Transition.png
  4. Use the transition screen from step 2 and the two post functions bellow on this transition
    1. Use "Assign Objects from an IQL query based on data from an issue." to set "Employee" field based on reporter field. You will need a user attribute on your "Employee" Object Type.
      • Employee_BasedOn_Reporter.png
    2. Use "Set a Jira custom field with the attribute value from a selected object" to set Manager field based on Employee Object.
      • Manager_BasedOn_Employee.png
Mahdi Mohammadnejad June 3, 2023

Dear @Taha El Majidi 

Thanks for your reply.

Let me check it. I think your 2nd suggestion is better for me.

Taha El Majidi
Contributor
July 2, 2023

Hi @Mahdi Mohammadnejad  Did it work ?

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, ace, atlassian community event, donation, girls who code, women in tech, malala fund, plan international, kudos, community badge, badge, atlassian badge, International Women’s month, International Women’s Day, women's month, women's day

10 for Change at Atlassian Community Events

Show up and give back by attending an Atlassian Community Event: we’ll donate $10 for every event attendee in March!

Join an Atlassian Community Event!
AUG Leaders

Upcoming Jira Service Management Events