Forums

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

Auto-assignee a Jira ticket upon selection of custom field option

Vijaya Ramya Allena June 29, 2022

HI All,

 

I'm using groovy script to make a particular user as a assignee on selection of an option in single select custom field. This custom field is having multiple contexts . 

My script is taking only few options from the custom field list and ignoring the others. I think, i'm missing out a logic on adding the exact context which my project is using.

Please help me in this

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.event.type.EventDispatchOption
def systemUserMap = [
Option1: "abc",
Option2: "cde",
["Option3" ,"Option4","Option5" ] : "efg"
]

if(webwork.action.ActionContext.getRequest().getParameter("assignee") == '-1'){
String userName;
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("Track")
def cfTrack = issue.getCustomFieldValue(cf)
if(cfTrack ) {
userName = systemUserMap[cfTrack.value]
log.error(userName)
log.error(cfTrack);
def assignee = ComponentAccessor.userManager.getUserByName(userName)
issue.setAssignee(assignee)
}
}

1 answer

1 accepted

1 vote
Answer accepted
Andrea Pannitti
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.
June 29, 2022

Hi @Vijaya Ramya Allena,

you could try with this code:

import com.atlassian.jira.component.ComponentAccessor

def systemUserMap = ["Option1": "abc", "Option2": "cde", "Option3": "efg", "Option4": "efg", "Option5": "efg"]

if (issue.assignee == null) {
def customFieldManager = ComponentAccessor.customFieldManager
def cf = customFieldManager.getCustomFieldObjectByName("Track")
def cfTrack = issue.getCustomFieldValue(cf).toString()

if (cfTrack) {
def userName = systemUserMap[cfTrack]
def assignee = ComponentAccessor.userManager.getUserByName(userName)
def issueService = ComponentAccessor.issueService
def validateAssignResult = issueService.validateAssign(assignee, issue.id, assignee.name)
issueService.assign(assignee, validateAssignResult)
}
}

 

Vijaya Ramya Allena June 29, 2022

HI @Andrea Pannitti 

 

Thank you for your response. I have tried the code as you suggested but getting the error as below.

error

["Atlassian jira "= "user5"] is a constant expression, but it should be a variable expression at line: 8 column: 26.
Unexpected node type: EXPR found when expecting type: LABELED_ARG at line: 9 column: 5. File: Script409.groovy

 

Code

import com.atlassian.jira.component.ComponentAccessor
def systemUserMap =
 [
    Option1 = "user1",
    option2 = "user2",
    option3 = "user3",
    option4 = "user4",
    "Atlassian jira "= "user5",
    "Dev Analytics" = "user6",
    "Cloud DevSecOps "= "user6" ,
    "Live Egnineering" = "user6" ,
    SRE= "user6",
    CodeStore = "user7",
    Service Store ="user7",
    Market Places="user7"
    ]

if(issue.assignee == null) {

def customFieldManager = ComponentAccessor.customFieldManager
    def cf = customFieldManager.getCustomFieldObjectByName("Track")
    def cfTrack = issue.getCustomFieldValue(cf).toString()

if (cfTrack) {
        def userName = systemUserMap[cfTrack]
def assignee = ComponentAccessor.userManager.getUserByName(userName)
        def issueService = ComponentAccessor.issueService
        def validateAssignResult = issueService.validateAssign(assignee, issue.id, assignee.name)
        issueService.assign(assignee, validateAssignResult)
    }
}
Andrea Pannitti
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.
June 29, 2022

Hi @Vijaya Ramya Allena,

when you define the systemUserMap, the keys must be all strings; so you should add double quote where missing.

Vijaya Ramya Allena June 29, 2022

HI @Andrea Pannitti 

 

Even after providing the Double quotes, I'm getting the same error.

Andrea Pannitti
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.
June 29, 2022

@Vijaya Ramya Allena,

yes, but you still have to replace the equals with the colons.

The correct definition of your map is the following:

 

def systemUserMap =
[
    "Option1" : "user1",
    "option2" : "user2",
    "option3" : "user3",
    "option4" : "user4",
    "Atlassian jira" : "user5",
    "Dev Analytics" : "user6",
    "Cloud DevSecOps" : "user6" ,
    "Live Egnineering" : "user6" ,
    "SRE" : "user6",
    "CodeStore" : "user7",
    "Service Store" : "user7",
    "Market Places" : "user7"
]
Vijaya Ramya Allena June 29, 2022

2022-06-30 12:24:57,678 ERROR [workflow.AbstractScriptWorkflowFunction]: Workflow script has failed for user 'vijayaramya.allena'. View here: <jira domine>/secure/admin/workflows/ViewWorkflowTransition.jspa?workflowMode=live&workflowName=Software+Simplified+Workflow+for+Project+QAUT&descriptorTab=postfunctions&workflowTransition=1&highlight=1
java.lang.IllegalArgumentException: Issue cannot be null.
at com.atlassian.jira.issue.fields.layout.field.AbstractFieldLayoutManager.getFieldLayout(AbstractFieldLayoutManager.java:137)
at com.atlassian.jira.bc.issue.DefaultIssueService.assign(DefaultIssueService.java:582)
at com.atlassian.jira.bc.issue.IssueService$assign$1.call(Unknown Source)
at Script544.run(Script544.groovy:28)

Thanks @Andrea Pannitti ,

I have tried creating the issue, but not able to auto-assign it to the users, Above is the error log. 

Andrea Pannitti
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.
June 30, 2022

@Vijaya Ramya Allena

in your log I see: Issue cannot be null.

Where you placed the code?

Thanks

Vijaya Ramya Allena June 30, 2022

@Andrea Pannitti 

I placed this in the postfuntion : Custom Script post-function(scriptrunner ) on " create " transition of the workflow

Thanks

Andrea Pannitti
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.
June 30, 2022

Hi @Vijaya Ramya Allena,

I think that the problem is in the postfunction position. You must move it after the "Creates the issue originally".

Vijaya Ramya Allena June 30, 2022

HI @Andrea Pannitti 

 

thanks a lot, it worked like charm,, I'm so glad . thank you so much for your time.

 

Allena

Suggest an answer

Log in or Sign up to answer