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

Bryan Karsh
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.
July 7, 2014

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

1 vote
Sumit Kumar
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.
July 31, 2014

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
0 votes
Bryan Karsh
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.
July 8, 2014

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.

0 votes
Bryan Karsh
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.
July 8, 2014

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

0 votes
Bryan Karsh
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.
July 8, 2014

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)

Suggest an answer

Log in or Sign up to answer