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

Update field when project selected

Hello Team,

We have custom filed name "Team Name" where we have lit of names under label manager.
When selected project issue has been raised than appropriate "Team Name" filed would automatically come up with appropriate name.

For example:
Project names: Proj1, Proj2
Team Name: ProjT1, ProjT2 (drop down selection, associated with multiple projects)

If someone is raising issue in Proj1 than Team Name filed automatically updated with ProjT1 or if issue has been raised in Proj2 than Team Name field automatically updated with ProjT2 name.

Thanks,
Ronak

2 answers

2 accepted

1 vote
Answer accepted
Bryan Bojorque Community Leader Dec 29, 2019

Hi @Ronak ,

Do you have any automation add-on installed in your instance?

If so, we can setup an automation to do this either ScriptRunner or Automation for Jira.

Hello Bryan,

 

Yes, I have script runner installed on our DC instances. 

 

Thanks,

Ronak

Hi @Ronak ,

I have attached the script,  I would assume that you already know how to setup the Behaviour in ScriptRunner.

The parameters for the Behaviour are the following:

All projects (All issue types)

Put the script in the Initialiser, change the script according to your requirements.

import com.atlassian.jira.component.ComponentAccessor

def issue = getIssueContext()
def teamObj = getFieldById('customfield_10200') //Custom Field ID
def projectObj = issue.getProjectObject()

def team
switch(projectObj.key){
case 'PROJ1': //Change this to the actual project key
team = 'Team 1'
break;
case 'PROJ2': //Change this to the actual project key
team = 'Team 2'
break;
}

def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObject(teamObj.getFieldId())
def config = customField.getRelevantConfig(issue)
def options = optionsManager.getOptions(config)
def optionToSelect = options.find { it.value == team }
teamObj.setFormValue(optionToSelect.optionId)

 

Hello Bryan,

 

I am testing that in jira dev instance. I will update you soon,

 

Thanks,

Ronak

Hello Bryan,

 

I have added script in behavior Initialiser. Team name is label manager and not custom filed. I can see its id shows as <label for="customfield_12600-textarea">Team Name</label>

 

def teamObj = getFieldById('customfield_12600-textarea') but it didn't worked.

Thanks,
Ronak
Bryan Bojorque Community Leader Dec 30, 2019

Hi @Ronak ,

Ok so it is a label custom field.

I tried to fix it but it looks like Label customfield is not yet supported in Behaviour.

https://productsupport.adaptavist.com/browse/SRJIRA-1080

But you are welcome to try this script. I just suggest to create a dropdown customfield and set the values like the above solution.

import com.atlassian.jira.component.ComponentAccessor

def issue = getIssueContext()
def teamObj = getFieldById('customfield_10201-textarea') //Custom Field ID
def projectObj = issue.getProjectObject()

def team
switch(projectObj.key){
case 'SP': //Change this to the actual project key
team = 'Team1'
break;
case 'PROJ2': //Change this to the actual project key
team = 'Team2'
break;
}

teamObj.setFormValue([team])

Hello Bryan,

 

I am trying with new script you have given above. Let me give it try. 

Is it possible to add this script in create post function as scriptrunner "Add Label Manager field items"?

 

Thanks,

Ronak

Hello Bryan,

 

I am glad to tell you that 2nd script works like a charm, I have tested and it updates Team name filed in associated projects.

I will give bit try with 2-3 more issue raising and final result will share with you.

Thanks,

Ronak

Hello Bryan,

 

It looks like it prints directly the value of "team = 'Team1'" or "team = 'Team2'".

So it won't appear in view screen. If I select it from dropdown it will display in view issue or edit issue.

 

Thanks,

Ronak

Bryan Bojorque Community Leader Dec 30, 2019

Hi @Ronak ,

There is another way to solve this, instead of Behaviour, we can use Script Listener.

It will listen to an Issue that was recently created, depending on the criteria of the issue.

We can then trigger the Script that way, this time we will refactor the script to use the classes required to update the label.

Hello Bryan,

 

Should I use the same script and where I can place script?

 

Thanks,

Ronak

Hello Bryan,

 

I am getting error when I have used above code in script listener.  Attached screenshot.

1.jpg

Thanks,

Ronak

Bryan Bojorque Community Leader Dec 31, 2019

Hi @Ronak ,

It will be a slightly different script, unfortunately I'm mobile right so I cant get you the right one. 

I will add the script as soon as I can.

Hello Bryan,


Should I use the first script you have shared with me?

 

Thanks,

Ronak

Hello @Bryan Bojorque ,

Happy New Year 2020!

Are you able to help me today?

Thanks,

Ronak

Bryan Bojorque Community Leader Jan 01, 2020

Hi @Ronak ,

Add this to ScriptRunner Script Listener.

The parameters under Script Listener will be.

Project(s): All Projects

Event: Issue Created

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.label.Label
import com.atlassian.jira.project.Project
import com.atlassian.jira.user.ApplicationUser

MutableIssue issue = event.issue
IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
ApplicationUser applicationUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
CustomField teamObj = customFieldManager.getCustomFieldObject('customfield_10701') //Change this, remove the -textarea
Project projectObj = issue.getProjectObject()

String team
switch (projectObj.key){
case 'TS': //Change this to the project key you have
team = 'Team1'
break
case 'PS': //Change this to the project key you have
team = 'Team2'
break
}

issue.setCustomFieldValue(teamObj, [new Label(null, issue.getId(), teamObj.getIdAsLong(), team)] as Set)
issueManager.updateIssue(applicationUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
Like Ronak likes this

Hello Bryan,

 

Thank you its working like pro,

Just 1 correction here regarding error:

wrong iteration: MutableIssue issue = event.issue

correct version: MutableIssue issue = (MutableIssue) event.issue

 

Thanks,

Ronak

Like Bryan Bojorque likes this
0 votes
Answer accepted
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.label.Label
import com.atlassian.jira.project.Project
import com.atlassian.jira.user.ApplicationUser

MutableIssue issue = (MutableIssue) event.issue
IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
ApplicationUser applicationUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
CustomField teamObj = customFieldManager.getCustomFieldObject('customfield_10701') //Change this, remove the -textarea
Project projectObj = issue.getProjectObject()

String team
switch (projectObj.key){
case 'TS': //Change this to the project key you have
team = 'Team1'
break
case 'PS': //Change this to the project key you have
team = 'Team2'
break
}

issue.setCustomFieldValue(teamObj, [new Label(null, issue.getId(), teamObj.getIdAsLong(), team)] as Set)
issueManager.updateIssue(applicationUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

Hello @Bryan Bojorque ,

When I keep label filed mandatory that won't work with this script. I believe this script works when issue is created. So during issue creation it won't pick up the name from drop down list.

As project need that field must be mandatory to enter valid data.

Thanks,

Ronak

Hello @Bryan Bojorque 

 

Did you get chance to review if field is mandatory than how label will get values from field drop-down list?

 

Thanks,

Ronak

Bryan Bojorque Community Leader Jan 12, 2020

Hi @Ronak

You can merge the Required option together with the default option that was added in the script.

Right now only the default option is included no matter if you added an option before creating the issue.

Suggest an answer

Log in or Sign up to answer
Community showcase
Asked in Data Center

AMA: How to plan ahead for Data Center - Expert advice from an Atlassian panel

This AMA is now closed Hi! I'm Jacob Shepard a Product Marketing Manager on Atlassian’s Enterprise Team. We know that moving to Data Center is no small task. To do so effectively demands extensive ...

10,388 views 47 36
View question

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