Set AllowedValuesDefinition dynamically based on prior forum field

Which IIQ version are you inquiring about?

8.4

Hi all,

I am attempting to create a provisioning form wherein the user selects a value from one dropdown, and the subsequent dropdown’s allowed values are set based on that value.

For example, if a user selects the state “Texas” from the “state” field, then the following “city” field should be a dropdown of “Austin, Dallas, Houston”, but if they change their selection to “California” the city dropdown should now only allow “LA, San Diego, Pasadena”

I have tried the following flags on the form fields:

<Field displayName="State" name="state" postBack="true" required="true" type="string">

<Field dependencies="state" displayName="City" dynamic="true" name="city" required="true" type="string">

As well as the following test code on the AllowedValuesDefinition of the City field

import sailpoint.object.field;

Field stateField = form.getField("state");

if (stateField != null) {
// code to set allowed values based on stateField value
}

However, stateField is seemingly always null, no matter what I have selected in it on the provisioning form. Does allowedValuesDefinition not run on postback refresh or am I referencing the field wrong?

Best,
Chris

@chris-hogan If you have your section type as text <Section type="text">remove it. if not please share your entire section here to check.

Hello @chris-hogan ,

As per my understanding, allowedValuesDefinition is not receiving the form definition object. therefore, you are getting stateField is Null value.

You can try to use: formData.get(“fieldName”)

Please confirm, if this resolves your issues.

Thanks

In a provisioning form’s AllowedValuesDefinition, you don’t have access to form object directly. You need to get the current field value differently.

can you try below and see if it works

<Field displayName="State" name="state" postBack="true" required="true" type="string">
  <AllowedValuesDefinition>
    <Script>
      <Source>
        import java.util.*;
        List states = new ArrayList();
        states.add("Texas");
        states.add("California");
        return states;
      </Source>
    </Script>
  </AllowedValuesDefinition>
</Field>

<Field displayName="City" name="city" dynamic="true" required="true" type="string">
  <AllowedValuesDefinition>
    <Script>
      <Source>
        import java.util.*;
        List cities = new ArrayList();
        if ("Texas".equals(state)) {
          cities.add("Austin");
          cities.add("Dallas");
          cities.add("Houston");
        } else if ("California".equals(state)) {
          cities.add("LA");
          cities.add("San Diego");
          cities.add("Pasadena");
        }
        return cities;
      </Source>
    </Script>
  </AllowedValuesDefinition>
</Field>
2 Likes

Hi Naveen,

This worked great (with an added null check for safety). Thank you!

Hi Naveen,

The only remaining issue I have is that a user can select “Texas”, then choose a Texas city, but then switch their state to “California” and submit the form without changing city. Now they have an invalid State/City combination.

Best,
Chris

Hi @chris-hogan,

You can use a rule to dynamically populate ‘DynamicValue’ object to populate the form field values dynamically (AllowedValuesDefinition population using rule, clear the value/set field value null in case the rule gets executed when the value is changed for the dropdown 1.

Thanks,

Pallavi

1 Like

Add a DefaultValue script on the City field that returns null` when the state changes, below is the example:

Since the City field has dynamic=“true”, every time the State field posts back, the City field is re-rendered and When it re-renders, the DefaultValueDefinition fires and sets the value back to null.

<Field displayName="City" name="city" dynamic="true" required="true" type="string">
<DefaultValueDefinition>
 
      <Source>
        return null;
      </Source>
    <

I am unable to save the form with DefaultValueDefinition and get an error that the XML element must be declared. Is this name correct?

Hi @chris-hogan - this is expected behavior based on the Forms technical whitepaper

"NOTE: Field value recalculations for fields marked as dynamic are not processed on postBack if a value has been entered manually in the field, based on the assumption that if a user manually enters a value, they generally do not want that overridden by an automatic process. To override this behavior, manually clear the fields using a hidden script elsewhere in the form..."

Have a workaround I’ve used myself a few times when running into this issue. You can use a hidden script to clear the value from the field when the new value would not be allowed based on the dependent field value updating.

This post does a good job walking through this use case. Hope this helps!

This post was answered by a Palyrian Solutions Architect. Feel free to message me directly if your problem requires a deeper dive.
:globe_with_meridians: palyrian.com | :telephone_receiver: ‪(301) 284-8124‬

1 Like

Hi Chris, it was a typo, you can use like below

<Field displayName="City" dynamic="true" name="city" required="true" type="string">
      <Script>
        <Source>

 
      
        return null;
      
</Source>
      </Script>
    </Field>

Hi @chris-hogan Yep, this code looks good, but the business case needs a small correction. It will work, I think, if we add clear the value from the field object if they change the state. field.setValue(““);

Thanks,

PVR