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

This widget could not be displayed.

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)

This widget could not be displayed.

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])
   
}

This widget could not be displayed.

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.

This widget could not be displayed.

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 Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

226 views 3 0
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