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
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.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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])
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.