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

Assign variable into a custom field base on component

Shah Baloch July 27, 2020

There are multiple components in the project, tickets get assign to users base on the component, for example if user select component "Product" then ticket gets assign to product lead, whenever he change status to "Approval Need" it should get assign to a Supervisor. I can add a supervisor in Post function but there are multiple components and there is only one work flow and issue type. If add user there then all ticket assign to him which we don't want, each component has there own lead and supervisor. For example Product component go product team, Customer components ticket assign to Customer team and Supplier to supplier team.

There is two approval Supervisor, I want to create two variable user1 and user2, if user select component "Product" to create a ticket, it should assign user2 otherwise it should add user1  to customer assignee field, then I'll use that assignee field in a User define Post function in workflow, so whenever product lead change the status it should get assign to Product Supervisor, rest of the component it should do nothing or assign to user1. I am trying add following Listener script but it won't working, I know it is not correct, I don't have an idea how to make it work.

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.db.DatabaseUtil
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import java.sql.Timestamp
import com.atlassian.jira.bc.project.component.ProjectComponent

def event = event as IssueEvent
def issue = event.issue
def customFieldObjects = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue)
def user1 = 'abc'
def user2 = 'xyz'

def cfAsignee = customFieldObjects.findById("customField_10401")
def components = getFieldById(getFieldChanged()).getValue() as List<ProjectComponent>

if(components == 'Product') {
cfAsignee.modifiedValue(user2)

else

cfAsignee.modifiedValue(user1)

}

Thank you

 

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Peter-Dave 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.
July 27, 2020

You say "listener" script, but I see "getFieldById" which is a behavior method.

If you are truly intending to use a listener, then yeah, that script will never work.

So I'll assume from here on out that this is a behaviour server-side script on the "component/s" field. 

The first issue with your script is that you are getting a list of project Components, but then comparing a single item to the list using ==. That won't work.

If you want to know if "product" was a selected component, you can try this

def components = getFieldById(getFieldChanged()).getValue() as List<ProjectComponent>
if(components.any{it.name == 'Product'){
//set the assignee here
}

The second issue is in trying to set that assignee custom field.

Since we're assuming behaviour script, that means you want the assignee to be applied immediately when the components are changed, this will need to be done by getting a behaviour formfield object (similar to how you got the component). Then using the setFormValue method to select the value

def customerAssigneeFormField = getFieldById('customField_10401')
customerAssigneeFormField.setFormField(user2)
Shah Baloch July 27, 2020

@Peter-Dave Sheehan Yeah I kind of mixed up, because trying both and make it work. I tried your script but getting errors. Do I need to add any  other lines or code? I appreciate your help.

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours

def components = getFieldById(getFieldChanged()).getValue() as List<ProjectComponent>
if(components.any(it.name == 'Product')){
def user2 = 'dthompson'



def customerAssigneeFormField = getFieldById('customField_10401')
customerAssigneeFormField.setFormField(user2)
}

 

Thanks

gv_01.pnggv_02.png

 

Peter-Dave 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.
July 27, 2020

What sort of errors are you getting specifically?

I see I was a little sloppy in my previous response (missing closing bracket and wrong method name)

This should work, provided that "Product" is the actual name of the component and that cvigil is an actual user name. Also, I've made the assumption that dthompson is the default assignee when it is not 'Product'. 

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours

def custAssignee = 'dthompson'
def components = getFieldById(getFieldChanged()).value as List<ProjectComponent>
if(components.any{it.name == 'Product'}){ //any requires curly brace
custAssignee = 'cvigil' //don't forget closing quote
}
def customerAssigneeFormField = getFieldById('customField_10401')
customerAssigneeFormField.setFormValue(custAssignee)

Like Shah Baloch likes this
Shah Baloch July 28, 2020

For some reason it is not assigning the value to the field. I copied and pasted the script, which didn't need any change except custom field id (customFieldId_10401), but it is not working. I can manually add user in the field which you can see in the screen shot.

Which field should I map? Component or custom Assignee field? I tried both added one field it didn't work,  deleted and tried the other one, same issue, not adding value in the custom assignee field.

When I click on "Assignee" custom field for configuration it shows me the value "customFieldId=10401", if I use equal sign instead of underscore, it won't let me to create a ticket "Leave Site" popup comes up on the screen.

I tried all the name of CF field, like 10401, cf[10401], customFieldId=10402, customFieldId_10401. It is not assign user to the field. I don't know what I'm doing wrong. Appreciate your help.

SR_07282020_01.pngSR_07282020_02.pngSR_07282020_03.png

Peter-Dave 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.
July 28, 2020

Looks like your custom field is a Multi User Picker. From the earlier discussion, I was assuming a single user picker and shows a corresponding example.

For multi user picker, you must do it like this:

def customerAssigneeFormField = getFieldById('customField_10402')
customerAssigneeFormField.setFormValue([custAssignee])
Like Shah Baloch likes this
Shah Baloch July 28, 2020

My apology, I just used an exiting one and didn't realize that can be an issue. I tried the above method, but it is still didn't. I create a new custom field (User Picker (customFieldId=16200)) but for some reason it won't work if I use "getFieldById". I changed it to "getFieldByName then work. Why it is not working with Field id?

Peter-Dave 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.
July 28, 2020

Ah sorry my mistake... this should work (lowercase f in customfield)

def customerAssigneeFormField = getFieldById('customfield_10402')
Like Shah Baloch likes this
Shah Baloch July 30, 2020

Thank you so much that works.

One more question if I wanna add one more component "Support", how I can make it work? I mean right now I'm just assigning "Product" component to another user and rest of the components are getting assign to the default assignee. If I want to assign "Support" to another user, how I can make it works?

This is how wanna make it works, "dthompson" is default assignee, "cvigil" is assignee of Product and "sbaloch" for Support. I tried to copy paste the If statement and changed component and assignee but didn't work. Is it possible to accomplish that?

Above mentioned code is working fine and I just want to assign Support component to another user.

Appreciate your help.

Shah Baloch July 30, 2020

I tried this, but didn't work.

 

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours

def custAssignee = 'dthompson'
def components = getFieldById(getFieldChanged()).value as List<ProjectComponent>
if(components.any{it.name == 'Product'}){
custAssignee = 'cvigil'
}
if(components.any{it.name == 'Support'}){ 
custAssignee = 'sbaloch' 
}

def customerAssigneeFormField = getFieldById('customfield_10401')
customerAssigneeFormField.setFormValue(custAssignee)

 

Thanks

Peter-Dave 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.
July 30, 2020

I see nothing wrong with this code.
What doesn't work? Are you seeing any errors in altassian-jira.log?

Like Shah Baloch likes this
Shah Baloch July 30, 2020

Thank you I think I was missing something, I tried it with another user and it works. Thank you so much.

TAGS
AUG Leaders

Atlassian Community Events