Automatic assignment based on custom field

Wojtek Misiarz February 27, 2018

Hi,

I have a problem and I don't know how to solve it. Maybe you have any ideas?

I would like to automate Jira Workflow and create automatic assignment in some situations to make sure that case is left unassigned.

Let's say I have custom field in Jira Software called Service Team  with options like: Logistic, IT, Finance, etc. 

For each Service Team I have dedicated person who is responsible for accepting tickets. It's always same person for each Service Team i.e. for Logistic it's Andrew, for Finance it's Jake and so on. 

Do you know how I can manage that after workflow transition to a specific state case will be assigned automatically to specific person depends on Service Team custom field value?

I tried with post function but I couldn't find option to make it.

Do you have any advice for me?

Thanks in advance

5 answers

1 accepted

6 votes
Answer accepted
Ivan Tovbin
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.
February 27, 2018

Hi Wojtek,

There's a couple of ways to do this:

1) Create a scripted post function that will set an assignee depending on your custom field value. This will require a 3rd party addon like ScriptRunner or JMWE.

2) Create an automation rule that will trigger when your issue is transitioned and set an assignee based on your custom field value. Automation for Jira is required for this approach.

3) If your are using Jira Service Desk, then you can do it using standard JSD automation which is available out of the box.

Ivan Tovbin
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.
February 28, 2018

Please mark the solution as 'Accepted' if it resolved your issue.

Callum Carlile _Automation Consultants_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 16, 2019

Hi Ivan,

I am trying to do option 1, as my client has ScriptRunner but does not want to purchase any other add-ons. I am pretty new to using scriptrunner, how would I go about writing the code for this? I can't find it anywhere online

Ivan Tovbin
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.
January 16, 2019

@Callum Carlile _Automation Consultants_

Sure thing. Please advise your custom field type and when do you want this to trigger?

Cheers.

Callum Carlile _Automation Consultants_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 16, 2019

The custom field is a single user picker. I would like this selected user to be assigned to a sub-task which is created automatically from a post function in a different workflow (I assume this is what you mean by trigger)

Ivan Tovbin
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.
January 16, 2019

Assuming your custom field is IN your sub-task (and not in its parent issue for example) the code would be as follows:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.user.ApplicationUser

ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
CustomField customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("fieldName")
ApplicationUser cfValue = issue.getCustomFieldValue(customField)
if (cfValue){
issue.setAssignee(cfValue)
ComponentAccessor.getIssueManager().updateIssue(currentUser, issue, EventDispatchOption.ISSUE_UPDATED, false)
}

 However, if your subtask is also created via script, the best approach would be to modify that script so the subtask is created already with an assignee set.

Otherwise use the code I provided as a post function for your subtask's 'Create' transition. Make sure you place this post function last.

Like Max Mustermann likes this
Andrii_Movchan April 23, 2020

@Ivan Tovbinis it possible to create similar script but for a bit complex conditions?

I want to set auto assignment for issue for certain user depending on custom field value (cascade type, two values).

For example, if value = A-a or B-b or C-c etc. assign to user1

if value = D-d or E-e assign to user2

 

How it

Gabriel January 14, 2021

@Ivan Tovbin how can I do this using option 3?

 

3) If your are using Jira Service Desk, then you can do it using standard JSD automation which is available out of the box.

Kanchan Katkar June 2, 2021

Hi, I get an error ComponentAccessor has not been initialised.

how to resolve it.

1 vote
Muhammad Akram Sharifi April 2, 2021

Go to the project setting 

Automations

Create new rule 

Issue created 

Assign issue << Specify user << Name of the user

in conditions 

issue fields condition

field: service team then Set the condition :equal and Value : to one of your values 

rememeber you have to create new rule for each user. 

Ankur Shrivastava June 25, 2021

finally a good solution

Like # people like this
0 votes
Swati Gupta June 7, 2022

Hi All,

We have a requirement, where 2 projects uses the same workkflow and it uses a post function by JMWE of auto assignment of user as assignee, when the user transition an issue.

Is there any way via scriptrunner, that we do not make changes to the workflow, but restrict this post function via scriptrunner for 1 project?

Thank You

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 7, 2022

@Swati Gupta Why don't you simply use the "Conditional execution" feature of the JMWE post function?

Swati Gupta June 10, 2022

Hi David,

Thanks for the suggestion, but can you please let me know how to do it?

Because when I add condition for current user, restricting JIRA project, then the transition is not visible for the specific project.

I would want to use condition in postfunction, but dont get the option Please help me with the solution.

Thank You

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 10, 2022

@Swati Gupta "Conditional Execution" is an option of every JMWE post-function - just look closely at the post-function configuration screen, you'll find it.

Swati Gupta June 11, 2022

Hi David,

Thanks for your comment.

 

I need help with 1 condition and 1 postfunction for scriptrunner:
1. Condition - restrict a global workflow status (Won't Do) from 20 projects (the transition is not visible for these projects) out of 150 projects using the global workflow
2. Post function - there is a custom field created as products - if it has 1 value, 1 subtask having its summary as product name should be created, if it has 2 values, 2 subtasks to be created and so on
Thank You
Swati Gupta June 12, 2022

Hi @David Fischer 

I tried script for post function requirement:

 

Post function is required- There is a custom field known as products (can have multiple values) - if it has 1 value, 1 subtask having its summary as product name should be created, if it has 2 values, 2 subtasks to be created, 3 values in products, 3 subtasks to be created and so on.
=====================================================

// Imports
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.MutableIssue

// import for debug only
import org.apache.log4j.Logger
import org.apache.log4j.Level
log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)

//Current issue - uncomment for post-function
Issue parentIssue = issue

//Get Value of Required Reviewers - Replace "XXXXX" in "customfield_XXXXX" with the actual value of your custom field
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
String customfieldID_MultiUserFieldName = 'customfield_18410'
CustomField customfield_MultiUserFieldName = customFieldManager.getCustomFieldObject(customfieldID_MultiUserFieldName)
Object customfieldIDValue_MultiUserFieldName = (ArrayList) parentIssue.getCustomFieldValue(customfield_MultiUserFieldName)

//Pre-define JQL Parser functions
def queryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
def activeUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

//Pre-define Sub-Task Generation functions
def issueFactory = ComponentAccessor.getIssueFactory()
def subTaskManager = ComponentAccessor.getSubTaskManager()
def constantManager = ComponentAccessor.getConstantsManager()
def issueManager = ComponentAccessor.getIssueManager()

//Get Values to copy from Parent
def summary = "Subtask for " + parentIssue.getSummary()
def dueDate = parentIssue.getDueDate()
def project = parentIssue.getProjectObject()

//Define Sub-Task Type - Replace "PUT CUSTOM FIELD NAME HERE" with the actual name you're using for your custom field
def issueTypeId = constantManager.getAllIssueTypeObjects().find{it.getName() == "products"}.id

//Iterate through and create subtasks
for (int i = 0; i < customfieldIDValue_MultiUserFieldName.size(); i++){
ApplicationUser reviewerObject = customfieldIDValue_MultiUserFieldName[i] as ApplicationUser
String reviewer = reviewerObject?.name
log.debug reviewer
//do not create sub-tasks if a sub-task already exists and has a certain Resolution
def query = queryParser.parseQuery("parent = ${parentIssue} and assignee = ${reviewer} and (resolution = Abstained or resolution = Approved or resolution = Unresolved)")
log.debug query
def search = searchService.search(activeUser, query, PagerFilter.getUnlimitedFilter())
def total = search.total
log.debug total
if (total == 0){
//Create core Sub-Task Values
MutableIssue newSubTask = issueFactory.getIssue()
newSubTask.setProjectObject(project)
newSubTask.setIssueTypeId(issueTypeId)
newSubTask.setParentObject(parentIssue)
newSubTask.setSummary(summary)
newSubTask.setDueDate(dueDate)
newSubTask.setReporter(activeUser)
//Assign Sub-Task
newSubTask.setAssigneeId(reviewer)
log.debug newSubTask.assigneeId
//Create Sub-Task
def newIssueParams = ["issue" : newSubTask] as Map
log.debug "Creating task for ${reviewer}"
def createdSubTask = issueManager.createIssueObject(activeUser, newIssueParams)
subTaskManager.createSubTaskIssueLink(parentIssue, createdSubTask, activeUser)
}

}

======================================================

Below is the error:

2022-06-12 10:54:17,052 ERROR [workflow.AbstractScriptWorkflowFunction]: Workflow script has failed on issue TPFSPP-2 for user 'ac-swagup'. View here: https://jira-dev.assaabloy.net/secure/admin/workflows/ViewWorkflowTransition.jspa?workflowMode=live&workflowName=Software+Simplified+Workflow+for+Project+TPFSPP&descriptorTab=postfunctions&workflowTransition=11&highlight=1 java.lang.NullPointerException: Cannot get property 'id' on null object at Script162.run(Script162.groovy:45)

0 votes
Allison September 17, 2018

Hi! 

May I ask you the code of script runner to assign a specific assignee based on custom fields? If it is possible through 'workflow properties', please let me know!!

Thank you for your help!

Best regards,

Jenny

0 votes
Wojtek Misiarz February 28, 2018

Hi Ivan,

thanks for your reply. I used your second proposal and it works great. Thanks for your help!

Prasad Andrews February 27, 2020

@Wojtek Misiarz - Could you please help how to achieve this.

If I create a Jira Issue and select a module and sub-module, can the issue be auto-assigned to a particular user based on the sub-module? 

Suggest an answer

Log in or Sign up to answer