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

Update field when project selected

Ronak December 18, 2019

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

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
brbojorque
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 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.

Ronak December 30, 2019

Hello Bryan,

 

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

 

Thanks,

Ronak

brbojorque
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 30, 2019

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)

 

Ronak December 30, 2019

Hello Bryan,

 

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

 

Thanks,

Ronak

Ronak December 30, 2019

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
brbojorque
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 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])
Ronak December 30, 2019

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

Ronak December 30, 2019

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

Ronak December 30, 2019

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

brbojorque
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 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.

Ronak December 31, 2019

Hello Bryan,

 

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

 

Thanks,

Ronak

Ronak December 31, 2019

Hello Bryan,

 

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

1.jpg

Thanks,

Ronak

brbojorque
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 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.

Ronak December 31, 2019

Hello Bryan,


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

 

Thanks,

Ronak

Ronak January 1, 2020

Hello @brbojorque ,

Happy New Year 2020!

Are you able to help me today?

Thanks,

Ronak

brbojorque
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 1, 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
Ronak January 1, 2020

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 brbojorque likes this
0 votes
Answer accepted
Ronak January 2, 2020
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)
Ronak January 3, 2020

Hello @brbojorque ,

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

Ronak January 8, 2020

Hello @brbojorque 

 

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

 

Thanks,

Ronak

brbojorque
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 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.

TAGS
AUG Leaders

Atlassian Community Events