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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

202 views 1 18
Join discussion

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