Behaviours Plugin question -- how to clear field(s) if other mapped field(s) of same behavior are modified?

Hi guys,

Here is my use case: I have three fields: Region, Customer, and Country. In a ticket, the user will pick a region, which will present the corresponding customers. Based on the customer chosen, a specific country will be mapped.

For the most part, this works well.

However, what I can't figure out is, how do I clear values based on whether another field is changed? For example,

I can set customer field to "None" if region is null :

if (region.getValue() == null) {
region.setError("You need to choose a region.")
customer.setFormValue(-1)

}...

But what I really want is the same behavior if either Customer or Region is modified.

For example, let's say I have a Region of "Region1", a customer of "Customer3", and a country of "Mexico". If Customer changes, country should remap to new country. If Region changes (for example to say, Region2), country should be changed to None, and Customer should also be changed to None.

Any tips? *Note* -- When I pick a region, it isn't a one-to-one mapping, which is why I am passing the FieldOptions. Once a specific option from FieldOptions is chosen, that is what maps to Country in a 1-1 relationship.

4 answers

With the following code in the behaviors for the Region and Country fields, I can get the functionality I want.... *except* if I edit the ticket -- then the fields for country and customer go to null, even if I am not editing them.

Here is the behavior I have for Region:

FormField region = getFieldByName("Region")
FormField customer = getFieldByName ("Customer")
FormField country = getFieldByName("Country")

    Map fieldOptions = [:]
    fieldOptions.put ("-1", "None")


if (region.getValue() == null) {
        region.setError("You need to choose a region.")
        customer.setFormValue(-1)
        customer.setHidden(true)
        country.setFormValue(-1)
        country.setHidden(true)
    } else {
        region.clearError();
      customer.setFormValue(-1)
      country.setFormValue(-1)
        customer.setHidden(false)
        country.setHidden(false)
             }
     
     
    
        
        
        switch (region.getValue()) {
            case "Region1" :
                fieldOptions.putAll (["14263":"Customer1", "14402":"Customer2", "14266":"Customer3"])
                break

            case "Region2" :
                fieldOptions.putAll (["14264":"Customer4", "14265":"Customer5", "14270":"Customer6"])
                break
                
            case "Region3" :
                fieldOptions.putAll (["14276":"Customer7", "14283":"Customer8", "14286":"Customer9"])
                break

            case "Region4" :
                fieldOptions.putAll (["14267":"Customer10", "14268":"Customer11"])
                break


        }
    
    customer.setFieldOptions (fieldOptions)

And here is behaviour for Customer:

FormField customer = getFieldByName("Customer")
FormField country = getFieldByName("Country")
FormField region = getFieldByName("Region")

if (region.getValue() == null) {
      customer.setFormValue(-1) 
      country.setFormValue(-1)
} 



def conMap = [
        "Customer1": 14382, // maps to country option of India
        "Customer2": 14382,  // maps to country option of India
        "Customer3": 14388,   // maps to country option of  Mexico
        "Customer4": 14381,  // maps to country option of Honduras
        "Customer5": 14386, // maps to country option of  Malaysia
        "Customer6": 14396,  // maps to country option of  US
        "Customer7": 14396,  // maps to country option of  US
        "Customer8": 14396,  // maps to country option of  US
        "Customer9": 14379,  // maps to country option of  Guatamala
        "Customer10": 14372, // maps to country option of  Caribbean
        "Customer11": 14380,  // maps to country option of  Haiti
]    



String cusName = customer.getValue()

//country.setFormValue(conMap[cusName])


if (cusName == null) {
      customer.setError("You need to choose a Customer.")
      country.setFormValue(-1)
      country.setHidden(true)      
} 


else {
    customer.clearError()   
    country.setHidden(false)
    country.setFormValue(conMap[cusName])
   
}

So basically -- looking for help on modifying this so I can get the behavior I want, whether I am creating a ticket, or editing a ticket. Right now, it seems to do what I want only if I create a ticket.

hi Bryan,

In the else part

else {
region.clearError();
customer.setFormValue(-1)
country.setFormValue(-1)
customer.setHidden(false)
country.setHidden(false)
}
You are setting Customer and country value to None
please replace it with
else {
region.clearError();
// customer.setFormValue(-1)
// country.setFormValue(-1)
customer.setHidden(false)
country.setHidden(false)
}
and the problem should be resolved.
You might need extra changes as well if other things are impacted by this change
thanks,
Sumit

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,281 views 14 20
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot