Behaviour plugin prepopulate single type field

Serj Shcherbakov August 19, 2016

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

1 vote
Answer accepted
Thanos Batagiannis _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 19, 2016

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

Serj Shcherbakov August 19, 2016

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!

Thanos Batagiannis _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 19, 2016

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)

Serj Shcherbakov August 21, 2016

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

Kind Regards,
Sergey

Suggest an answer

Log in or Sign up to answer