Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scripted Behaviour to handle updated fields

Becky Compton August 29, 2018

I have a scripted behavior that is set (on FieldA and FieldB) and works correctly.  Here is the logic:

IF FieldA or FieldB is populated, then make FieldC required.

This logic works, but I am trying to put an additional behaviour in place(on FieldC) that says

"IF FieldC is updated back to "None" (Default) after it is made required, check to see if FieldA or FieldB is populated.  If neither is populated, clear error and make FieldC no longer required.  If either is populated, throw original error on FieldC again"

Here is how I have written the serverside script for the behaviour, but it is not working.  Can anyone help guide me to the correct scripting?

{code}

def FieldAValue= FieldAField?.getValue() as String
def FieldBValue = FieldBField?.getValue() as String
def FieldCValue = FieldCField?.getValue() as String


if ((FieldCValue == "None" && FieldAValue) || (FieldCValue == "None" && FieldBValue ))


{
FieldCField.setRequired(true)
FieldCField.setError("Please make a selection other than 'None'")
}

else
{
FieldCField.setRequired(false)
FieldCField.clearError()
}

{code}

1 answer

0 votes
Jenna Davis
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 31, 2018

Hello, 

I've been able to get this script to work as you described, let me know if you still have trouble with it. :)

def FieldA = getFieldByName("TextFieldA")
def FieldB = getFieldByName("TextFieldB")
def FieldC = getFieldByName("SelectListA")

def FieldAValue = FieldA.getValue() as String
def FieldBValue = FieldB.getValue() as String
def FieldCValue = FieldC.getValue() as String

if((!FieldCValue && FieldAValue) || (!FieldCValue && FieldBValue)){
FieldC.setRequired(true)
FieldC.setError("Please make a selection other than 'None'.")
}
else{
FieldC.setRequired(false)
FieldC.clearError()
}
Becky Compton August 31, 2018

Thanks @Jenna Davis,

 

It works fine if I select a value in FieldA or FieldB (it makes FieldC required), but if I leave FieldA or FieldB populated and update FieldC to "none", it does not throw the error on Field C again.  I am trying to account for all possible combination of clicks within the form, and this is the only one left that fails.

Jenna Davis
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 31, 2018

Ahh, I see. 

You'll need to add additional scripts like this onto both FieldA and FieldB in order to achieve that. 

def FieldA = getFieldByName("TextFieldA")
def FieldC = getFieldByName("SelectListA")

def FieldAValue = FieldA.getValue() as String
def FieldCValue = FieldC.getValue() as String

if(FieldAValue && !FieldCValue){
FieldC.setRequired(true)
FieldC.setError("Please make a selection other than 'None'.")
}
else{
FieldC.setRequired(false)
FieldC.clearError()
}

This is because the first behaviour, on FieldC, won't recalculate when you update FieldA or Field B, it will only run when FieldC changes.

Let me know if this fixes the issue or if you have any more questions. :)


Jenna

Becky Compton August 31, 2018

Thanks @Jenna Davis, But just to be clear, I am wanting it to recalculate when FieldC is updated from any value to NULL/None (while FieldA or FieldB are still populated).  The action on FieldC should drive this specific action.  But here is the script I have on Field A:

 

{code}

def launchtypeField = getFieldByName("Launch Type")
def affiliateMID = getFieldById(getFieldChanged())
def formTracking = getFieldByName("Affiliate Tracking Method")
def displayMIDField = getFieldByName("Parent DIsplay MID")
def custReportingField = getFieldByName("Customer Status Reporting")
def parentMIDField = getFieldById("customfield_19266")

def affiliateMIDValue = affiliateMID?.getValue() as String
def formTrackingValue = formTracking?.getValue() as String
def parentMIDValue = parentMIDField?.getValue() as String
def custReportingValue = custReportingField?.getValue() as String

if (affiliateMIDValue != "" && !formTrackingValue)


{
formTracking.setRequired(true)
formTracking.setError("As Affiliate is selected as a launch type, please provide the client's Affiliate Tracking Method")
}

else {
formTracking.setRequired(false)
formTracking.clearError()
}


if (affiliateMIDValue && !custReportingValue)

{
custReportingField.setRequired(true)
custReportingField.setError("Since you populated an Affiliate MID and/or Display MID please select a customer status reporting option for those conversion tags")
}
else {
custReportingField.setRequired(false)
custReportingField.clearError()
}

if (affiliateMID.getValue() != "")

{
affiliateMID.setRequired(false)
affiliateMID.clearError()
}

{code}

Jenna Davis
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 31, 2018

Okay, in that case you shouldn't need the scripts on FieldA or B. However, the first script should work for what you're describing, I've tested it out myself. 

Here's the behaviour I see when using the only first script I posted that should go on FieldC, let me know if you're wanting something different that I'm not understanding. 

  • If you have values in one or both of FieldA and FieldB and have something like "test" selected in FieldC, there is no error and the field is not required. 
  • If you change FieldC from 'test' to 'None', you get an error and the field is required.
  • If you delete the values in FieldA and FieldB, then change FieldC from 'test' to 'None', there is no error and the field isn't required. 

Now, with my second script included (on FieldA and FieldB) you'll also have this added behavior. 

  • If you have value 'None' selected in FieldC, then change FieldA/B from having no value to containing a value, you get an error on FieldC. 

After your last post I'm thinking the second script I sent isn't what you want, but I believe using just the first script works as you've described. Please correct me if I'm wrong in what I'm understanding. Also, send me what you're currently using on FieldC so I can try out what you've got. :) 

Jenna

Becky Compton August 31, 2018

Hey @Jenna Davis - You are close.  Hopefully this is better description:

  1. I put a Value in FieldA (or FieldB) 
    1. The script on FieldA (or FieldB) sets FieldC as required with an error
  2. I set a value in FieldC other than "none" (default)
    1. The error set from step 1 is cleared
  3. I then change the value in FieldC from what I entered in Step 2 back to "none" (but FieldA(or FieldB) is still populated)
    1. The error should pop up again and alert the user that FieldC is still required, but this is what is not happening.

I hope that makes sense.  I still need the scripts on FieldA and FieldB as those are what make field C required, but I need the error to pop back up if FieldC is updated (after a selection is made) and changed back to the default "none".

Jenna Davis
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 31, 2018

Can you please send me the script you're using on FieldC so I can see exactly what you're doing? I was able to get this behavior using the first script I'd sent, I believe the second bullet point I made mentioned this. I could've made my explanation a bit clearer there, my apologies. 

Becky Compton September 4, 2018

@Jenna Davis - Sure!

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField

def customFieldManager = ComponentAccessor.customFieldManager
def optionsManager = ComponentAccessor.optionsManager
def parentMIDField = getFieldById("customfield_19266")
def custReportingField = getFieldById("customfield_19579")
def affiliateMIDField = getFieldById("customfield_19264")

def affiliateMIDValue = affiliateMIDField?.getValue() as String
def parentMIDValue = parentMIDField?.getValue() as String
def custReportingValue = custReportingField?.getValue() as String


if ((custReportingValue == "None" && affiliateMIDValue) || (custReportingValue == "None" && parentMIDValue))


{
custReportingField.setRequired(true)
custReportingField.setError("please select a customer status reporting option for those conversion tags")
}

else
{
custReportingField.setRequired(false)
custReportingField.clearError()
}
Becky Compton September 10, 2018

@Jenna Davis  - Do you have any recommendations for how to do this?

Suggest an answer

Log in or Sign up to answer