Behaviour plugin prepopulate single type field

Hi Community!
Please help me with my behavior code:

def sourceReferral = getFieldByName("Off-site PM").getValue()?.toString()
def sourceReferral2 = getFieldByName("PM / Off-site PM needed?")
if(sourceReferral.getValue() == null ||sourceReferral.getValue().equals("")){
sourceReferral2.setFormValue(18422) 
}

Where sourceReferral = User Picker Field

sourceReferral2 = Single Select List 

This code currently not working, maybe I did mistake somewhere?

1 answer

1 accepted

Hi Sergey

So you have in the first line you get the string value

def sourceReferral = getFieldByName("Off-site PM").getValue()?.toString()

and in your if statement you are trying to get the value again.

And then I suppose the 18422 is the id of the option to set right ? 

import com.atlassian.jira.component.ComponentAccessor

def sourceReferral = getFieldByName("Multi User Picker").getValue()
def sourceReferral2 = getFieldByName("Single Select")

if (!sourceReferral) { // if is null or empty (which means no user selected) select list will have OptionA selected
    log.debug("Is empty I will set option")
    def cfConfig = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Single Select")?.getRelevantConfig(getIssueContext())
    def optionToSet = ComponentAccessor.getOptionsManager().getOptions(cfConfig)?.find {it.value == "OptionA"}?.optionId
    sourceReferral2.setFormValue(optionToSet)
} else { // else (if there is at least on user selected in the multi user group) single select list will have None as selected
    sourceReferral2.setFormValue(null)
}

I bet this is more readable smile

regards

Thanos

Hi, Thanons!
You bet, I will! laugh

So, this what we have after your expertise:

def sourceReferral = getFieldByName("User Multy Select").getValue()?.toString()

def sourceReferral2 = getFieldByName("Single Select")

def cfConfig = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Single Select")?.getRelevantConfig(getIssueContext())
def optionToSet = ComponentAccessor.getOptionsManager().getOptions(cfConfig)?.find {it.value == "OptionA"}?.optionId
selectList.setFormValue(optionToSet)

if(sourceReferral.getValue() == null ||sourceReferral.getValue().equals("")){
sourceReferral2.setFormValue(18422)
}


Seems not excellent, can you review once again, please.
Many-many thanks!

Sergey I updated the original script above. In order to run the script every time the value of the multi user group changes, assign the above server side script in the

Field: Multi User Picker (or whatever you name it)

Hi, Thanos! It's worked!
Many-many thanks!

Kind Regards,
Sergey

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

89 views 0 5
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you