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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Assign variable into a custom field base on component

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

0 votes

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)

@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

 

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

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

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

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?

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

def customerAssigneeFormField = getFieldById('customfield_10402')
Like Shah Baloch likes this

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.

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

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

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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Opsgenie

Investigate deployment-related incidents with Bamboo and Opsgenie together

Back in April of last year one of the major product announcements from Opsgenie was the launch of the Incident investigation view which created a deep connection between Bitbucket and Opsgenie, empow...

90 views 1 3
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you