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

Need help -- Selection from Drop Down list updates two custom user picker fields

Hello Community. 

 

Any help would be appreciated. 

 

I have a custom field drop down list, that details three different sites, 

Based on the selection of that site I have two custom fields that need to be updated with the correct people assigned to that site. 

Both fields are multiple users.

 

if drop down siteA

then 

 field 1, user1a and user2a

field 2, user3a

 

if siteb

then

field1, user1b and user2b

field2, user3b

 

if siteC

field1, user1c, user2c

field2 user3c

 

 

I have scriptrunner, Ive tried a couple of existing scripts but all of them do not work, so im at a loss. Rather than muddy the water, if anyone can help me I would be greatly appreciated. 

 

Running 8.5.4

Scriptrunner 6.17

 

 

1 answer

1 accepted

0 votes
Answer accepted
Hana Kučerová Community Leader Feb 03, 2021

Hi @Jeremy Cejka ,

it is not clear to me, when this automation should happen. Is it during issue creation or other transition or ... ? Something like your user just created/transitioned some issue and based on the siteA value, which is mandatory, the other fields should be changed...

Thank you for the clarification.

Sorry for not including this, At time of creation. This selection would occur on the create issue screen. And would be a post function trigger.  

I started with this, not sure if its right.  I kind of took pieces of scripts that kind of matched what i was trying to do.  

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption;
import com.atlassian.jira.user.util.UserManager;

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def siteField = customFieldManager.getCustomFieldObject("customfield_14203")
def securityField = customFieldManager.getCustomFieldObject("customfield_14308")
def opsField = customFieldManager.getCustomFieldObject("customfield_14303")

def siteFieldValue = ((LazyLoadedOption)siteField).getValue()
def security = [] as List
def ops = [] as List

if (siteFieldValue == "ALX")
{
security << ComponentAccessor.getUserManager().getUserByKey("user1a")
security << ComponentAccessor.getUserManager().getUserByKey("user2a")
ops << ComponentAccessor.getUserManager().getUserByKey("user3a")
log.debug "ALX"
}
if (siteFieldValue == "MLB")
{
security << ComponentAccessor.getUserManager().getUserByKey("user1b")
security << ComponentAccessor.getUserManager().getUserByKey("user2b")
ops << ComponentAccessor.getUserManager().getUserByKey("user3b")
log.debug "MLB"
}
if (siteFieldValue == "SATX")
{
security << ComponentAccessor.getUserManager().getUserByKey("user1c")
ops << ComponentAccessor.getUserManager().getUserByKey("user2c")
log.debug "SATX"
}
if (siteFieldValue == "Remote")
{
log.debug "Remote"
}


security = security.unique()
securityField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(securityField), security), new DefaultIssueChangeHolder())

ops = ops.unique()
opsField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(opsField), ops), new DefaultIssueChangeHolder())
Hana Kučerová Community Leader Feb 03, 2021

Hi @Jeremy Cejka ,

please try something like this. There's some configuration, which needs to be set up. First of all - option ids for your site custom field. I've used ids instead of names, because it is more safer (ids don't change). Then also keys for your users. I didn't test it much and there are some cases, which should be also handled (like if site custom field is empty etc), but in general I believe it should work.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager

Long CUSTOM_FIELD_ID_SITE = 14203
Long CUSTOM_FIELD_ID_SECURITY = 14308
Long CUSTOM_FIELD_ID_OPS = 14303

Long CUSTOM_FIELD_SITE_OPTION_ID_ALX = 11111
Long CUSTOM_FIELD_SITE_OPTION_ID_MLB = 22222
Long CUSTOM_FIELD_SITE_OPTION_ID_SATX = 33333
Long CUSTOM_FIELD_SITE_OPTION_ID_REMOTE = 44444

String USER_KEY_1A = "JIRAUSER11111"
String USER_KEY_1B = "JIRAUSER22222"
String USER_KEY_1C = "JIRAUSER33333"
String USER_KEY_2A = "JIRAUSER44444"
String USER_KEY_2B = "JIRAUSER55555"
String USER_KEY_2C = "JIRAUSER66666"
String USER_KEY_3A = "JIRAUSER77777"
String USER_KEY_3B = "JIRAUSER88888"

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
UserManager userManager = ComponentAccessor.getUserManager()

CustomField siteField = customFieldManager.getCustomFieldObject(CUSTOM_FIELD_ID_SITE)
CustomField securityField = customFieldManager.getCustomFieldObject(CUSTOM_FIELD_ID_SECURITY)
CustomField opsField = customFieldManager.getCustomFieldObject(CUSTOM_FIELD_ID_OPS)

Option siteFieldValue = issue.getCustomFieldValue(siteField) as Option
Long siteFieldOptionId = siteFieldValue.getOptionId()

List<ApplicationUser> securityUsers = []
List<ApplicationUser> opsUsers = []

switch (siteFieldOptionId) {
case CUSTOM_FIELD_SITE_OPTION_ID_ALX:
securityUsers << userManager.getUserByKey(USER_KEY_1A)
securityUsers << userManager.getUserByKey(USER_KEY_2A)
opsUsers.add(userManager.getUserByKey(USER_KEY_3A))
break
case CUSTOM_FIELD_SITE_OPTION_ID_MLB:
securityUsers << userManager.getUserByKey(USER_KEY_1B)
securityUsers << userManager.getUserByKey(USER_KEY_2B)
opsUsers << userManager.getUserByKey(USER_KEY_3B)
break
case CUSTOM_FIELD_SITE_OPTION_ID_SATX:
securityUsers << userManager.getUserByKey(USER_KEY_1C)
opsUsers << userManager.getUserByKey(USER_KEY_2C)
break
case CUSTOM_FIELD_SITE_OPTION_ID_REMOTE:
// TODO ?
break
}

if (securityUsers.size() > 0) {
DefaultIssueChangeHolder changeHolder = new DefaultIssueChangeHolder()
List<ApplicationUser> securityFieldValue = issue.getCustomFieldValue(securityField) as List<ApplicationUser>
securityField.updateValue(null, issue, new ModifiedValue(securityFieldValue, securityUsers), changeHolder)
}

if (opsUsers.size() > 0) {
DefaultIssueChangeHolder changeHolder = new DefaultIssueChangeHolder()
List<ApplicationUser> opsFieldValue = issue.getCustomFieldValue(opsField) as List<ApplicationUser>
opsField.updateValue(null, issue, new ModifiedValue(opsFieldValue, opsUsers), changeHolder)
}

ok thanks, ill plug it in and report. 

 

Thank you again for assistance. 

For 

Long CUSTOM_FIELD_SITE_OPTION_ID_ALX = 11111

 

I assume that is the supposed to be the backend value matched to the default string option correlating to ALX as a value?

 

In my example, you pull it from the url pattern when editing the particular value set under default value for the custom_field

https://.../jira/secure/admin/EditCustomFieldOptions!edit.jspa?fieldConfigId=15803&atl_token=BJPH-1N25-G6DR-L9SF_3bdfcd18ebf3ca6ff4344ff3fc6af0a3b37b8d54_lin&selectedValue=12700

 

selectedValue ~ CUSTOM_FIELD_SITE_OPTION_ID_ALX

Hana Kučerová Community Leader Feb 03, 2021

@Jeremy Cejka yes, exactly... I'm sorry I wasn't clear enough

Long CUSTOM_FIELD_SITE_OPTION_ID_ALX = 12700

Probably the next option will have id 12701, because you've created it immediately after the first one...

Good morning, 

 

I found this the other day and thought it might be a typo, the syntax checker threw a syntax error "unexpected token" on the break line after this line

 opsUsers.add(userManager.getUserByKey(USER_KEY_3A))

 

Where the other similar lines for the other site case was

opsUsers << userManager.getUserByKey(USER_KEY_3B)

 

So I changed it to match opsUsers << and the error went away. 

 

When running the script, it adds the securityUsers as expected (I had to properly place the postfunction after Create Issue Function.  A little trial and error solved that. 

The opsUsers isnt populated, heres the error

2021-02-03 19:09:04,523 ERROR [workflow.AbstractScriptWorkflowFunction]: Workflow script has failed for user 'jcejka'. View here: https://devportal.researchinnovations.com/jira/secure/admin/workflows/ViewWorkflowTransition.jspa?workflowMode=live&workflowName=HR+-+Out+Processing+Workflow&descriptorTab=postfunctions&workflowTransition=1&highlight=2
com.google.common.util.concurrent.UncheckedExecutionException: java.lang.NullPointerException: null value in entry: issue=null
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2050)
at com.google.common.cache.LocalCache.get(LocalCache.java:3952)
at com.google.common.cache.LocalCache$LocalManualCache.get(LocalCache.java:4871)
at com.atlassian.jira.issue.customfields.persistence.EagerLoadingOfBizCustomFieldPersister.getValuesForIssueId(EagerLoadingOfBizCustomFieldPersister.java:103)
at com.atlassian.jira.issue.customfields.persistence.EagerLoadingOfBizCustomFieldPersister.getValuesForTypeAndParent(EagerLoadingOfBizCustomFieldPersister.java:86)
at com.atlassian.jira.issue.customfields.persistence.OfBizCustomFieldValuePersister.updateValues(OfBizCustomFieldValuePersister.java:151)
at com.atlassian.jira.issue.customfields.persistence.EagerLoadingOfBizCustomFieldPersister.updateValues(EagerLoadingOfBizCustomFieldPersister.java:61)
at com.atlassian.jira.issue.customfields.persistence.OfBizCustomFieldValuePersister.createValues(OfBizCustomFieldValuePersister.java:108)
at com.atlassian.jira.issue.customfields.persistence.EagerLoadingOfBizCustomFieldPersister.createValues(EagerLoadingOfBizCustomFieldPersister.java:49)
at com.atlassian.jira.issue.customfields.persistence.OfBizCustomFieldValuePersister.createValues(OfBizCustomFieldValuePersister.java:101)
at com.atlassian.jira.issue.customfields.persistence.EagerLoadingOfBizCustomFieldPersister.createValues(EagerLoadingOfBizCustomFieldPersister.java:43)
at com.atlassian.jira.issue.customfields.impl.AbstractMultiCFType.createValue(AbstractMultiCFType.java:127)
at com.atlassian.jira.issue.customfields.impl.AbstractMultiCFType.createValue(AbstractMultiCFType.java:39)
at com.atlassian.jira.issue.fields.ImmutableCustomField.createValue(ImmutableCustomField.java:693)
at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:410)
at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396)
at com.atlassian.jira.issue.fields.OrderableField$updateValue$5.call(Unknown Source)
at Script233.run(Script233.groovy:64)
Caused by: java.lang.NullPointerException: null value in entry: issue=null
at com.google.common.collect.CollectPreconditions.checkEntryNotNull(CollectPreconditions.java:32)
at com.google.common.collect.SingletonImmutableBiMap.<init>(SingletonImmutableBiMap.java:42)
at com.google.common.collect.ImmutableBiMap.of(ImmutableBiMap.java:72)
at com.google.common.collect.ImmutableMap.of(ImmutableMap.java:124)
at com.atlassian.jira.issue.customfields.persistence.EagerLoadingOfBizCustomFieldPersister.lambda$getValuesForIssueId$0(EagerLoadingOfBizCustomFieldPersister.java:104)
at com.google.common.cache.LocalCache$LocalManualCache$1.load(LocalCache.java:4876)
at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3528)
at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2277)
at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2154)
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2044)
... 17 more
Cancel

The above script works as intended. 

Needed to adjust 

opsUsers.add(userManager.getUserByKey(USER_KEY_3A))

to 

opsUsers << userManager.getUserByKey(USER_KEY_3A)
Like Hana Kučerová likes this

@Hana Kučerová 

Any particular reason why it has to be after the "Creates the Issue originally"?

Hana Kučerová Community Leader Feb 04, 2021

@Jeremy Cejka I believe the reason is you're working with issue data - getting value from the site custom field

So the reason I ask, is that the next post function step is to copy these users to the watchers field. so that the notification scheme is a little more dynamic.  

 

When the copy JSU function runs which is after this, it copies both of the user fields plus some others.  It does not put these users that are generated based on site selection into the watcher field, just the others that are requestor filled or prefilled.

as a test

I tried using the same logic from the above script and adding a third array that just adds all other users in each case.  That didnt work either.

 

The end game so that users in the security or ops field have an email notification.  

Hana Kučerová Community Leader Feb 06, 2021

Hi @Jeremy Cejka ,

would you please share me all the post functions you are doing now during create and in which order? I'm sorry, it is a little bit confusing for me now. Thank you.

Hana Kučerová Community Leader Feb 10, 2021

Hi @Jeremy Cejka ,

please try to modify the script like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager

Long CUSTOM_FIELD_ID_SITE = 14203
Long CUSTOM_FIELD_ID_SECURITY = 14308
Long CUSTOM_FIELD_ID_OPS = 14303

Long CUSTOM_FIELD_SITE_OPTION_ID_ALX = 11111
Long CUSTOM_FIELD_SITE_OPTION_ID_MLB = 22222
Long CUSTOM_FIELD_SITE_OPTION_ID_SATX = 33333
Long CUSTOM_FIELD_SITE_OPTION_ID_REMOTE = 44444

String USER_KEY_1A = "JIRAUSER11111"
String USER_KEY_1B = "JIRAUSER22222"
String USER_KEY_1C = "JIRAUSER33333"
String USER_KEY_2A = "JIRAUSER44444"
String USER_KEY_2B = "JIRAUSER55555"
String USER_KEY_2C = "JIRAUSER66666"
String USER_KEY_3A = "JIRAUSER77777"
String USER_KEY_3B = "JIRAUSER88888"

IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
UserManager userManager = ComponentAccessor.getUserManager()

CustomField siteField = customFieldManager.getCustomFieldObject(CUSTOM_FIELD_ID_SITE)
CustomField securityField = customFieldManager.getCustomFieldObject(CUSTOM_FIELD_ID_SECURITY)
CustomField opsField = customFieldManager.getCustomFieldObject(CUSTOM_FIELD_ID_OPS)

ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

Option siteFieldValue = issue.getCustomFieldValue(siteField) as Option
Long siteFieldOptionId = siteFieldValue.getOptionId()

List<ApplicationUser> securityUsers = []
List<ApplicationUser> opsUsers = []

switch (siteFieldOptionId) {
case CUSTOM_FIELD_SITE_OPTION_ID_ALX:
securityUsers << userManager.getUserByKey(USER_KEY_1A)
securityUsers << userManager.getUserByKey(USER_KEY_2A)
opsUsers << userManager.getUserByKey(USER_KEY_3A)
break
case CUSTOM_FIELD_SITE_OPTION_ID_MLB:
securityUsers << userManager.getUserByKey(USER_KEY_1B)
securityUsers << userManager.getUserByKey(USER_KEY_2B)
opsUsers << userManager.getUserByKey(USER_KEY_3B)
break
case CUSTOM_FIELD_SITE_OPTION_ID_SATX:
securityUsers << userManager.getUserByKey(USER_KEY_1C)
opsUsers << userManager.getUserByKey(USER_KEY_2C)
break
case CUSTOM_FIELD_SITE_OPTION_ID_REMOTE:
// TODO ?
break
}

issue.setCustomFieldValue(securityField, securityUsers)
issue.setCustomFieldValue(opsField, opsUsers)
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.5.4
TAGS

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