How to Set up Field Dependency in Salesforce

I created a custom object in Salesforce to create new employee records. In that form, the employee needs to select the country and state of their native place.

However, the requirement was that I needed to modify the country and state fields so that when an employee selects a country, only the related state should be displayed in the state field.

For that, we have a field dependency feature in Salesforce, and I achieved this requirement using it.

In this Salesforce tutorial, we will learn about field dependency. I will explain step-by-step how to set up field dependency in Salesforce.

Field Dependency in Salesforce

In Salesforce, when we select a controlling field, the other field(the dependent field) should show only values related to the controlling field.

This is called field dependency. Using field dependency, we can save time and quickly create records.

Uses of Field Dependency

  • Using field dependency, we can filter dependent field values based on the value we selected in the controlling field.
  • It enhances accuracy when creating records, as selecting the controlling field in the dependent field displays only values related to it.

Controlling Field in Salesforce

  • The controlling field decides the behavior of the dependent fields. There are three Controlling Fields in Salesforce:

Dependant Field in Salesforce

  •  The dependent field shows only those values related to the controlling field; in the dependent field, we can set which value should be displayed according to the controlling field. There are two Dependent Fields in Salesforce:

Dependent Picklist Considerations

  • After converting the existing field to a controlling or dependent field, it does not affect your existing records. It will apply to new records.
  • Before creating a field dependency, make sure your picklist field has at least one Picklist Value.
  • If you change the existing controlling field value, then the dependent field value will lost.
  • The controlling field must be present in the Page Layout because if it is missing, the dependent picklist values don’t appear.
  • Make sure the dependent picklist is lower on the page layout than the controlling field.

Now, let’s see why there is a need for field dependency in Salesforce. See the following image: In the Country field(Picklist field), I have selected ‘USA.’ Then, when I click on the State field(Picklist field), it displays the USA states as well as the states of other countries.

field dependency salesforce

So here I want to customize, when I click on any country, in the state field, it should display only the states of a selected country; no other states should display

Let’s see how to create a field of dependency using the controlling and dependent fields.

Set up Field Dependency in Salesforce

Let’s take an example of the registration form; in the registration form, there is an option to select a country (controlling field), and after selecting the country, it should display only the state(dependent field) of the selected country.

  1. Go to Setup and click on the Object Manager tab. Select the object on which you want to create a field dependency. Here, I have selected Custom Object Patient.
field dependency in salesforce
  1. Then click on the Field & Relationships option and click on the Field Dependency button.
salesforce field dependencies
  1. Then, click the New button to create a field dependency and select Controlling Field and Dependent Field. Then click on the Continue button.
Field Dependency Matrix in Salesforce

Field Dependency Matrix

  1. Here, you will see the field dependency matrix, with the controlling field(Country) displayed on top and the dependent field(States) displayed under it.
    • Next, we select the states related to the country we want to display in the dependent field and then click the Include Values button.
    • Similarly, for the Exclude, if you want to remove a value, select that value and click on the Exclude button. After completing this, click on the Save button.
    • After selecting values, click on the Save button.
dependent fields salesforce
  1. Now, let’s check whether this field dependency has been applied to the selected fields. To do so, navigate to the object on which you created the field dependency.
    • Select any value in the controlling field(Country), then click on the dependent field(States). You will see that in the dependent field, only those values related to the selected country will be displayed.
field dependencies in salesforce

This is how we can create field dependency in Salesforce.

Edit Dependent Picklist Values in Salesforce

After creating the field dependency with controlling and dependent values, you need to edit the created field dependency to change the dependent picklist values.

You can edit the dependent picklist values in Salesforce using the following steps.

  1. Go to Object Manager, search for the object you want to edit the field dependency on, and click on it.
    • Then click on the Field & Relationships option and click on the Field Dependency button. As you click on it, you can see a list of field dependencies that you created.
    • Here, you need to click on the Edit option to make changes.
How to Edit Field Dependency in Salesforce
  1. After clicking the edit option, you will see the field dependency matrix, which shows the controlling and dependent fields. Here, you can only edit the dependent field values by selecting them, and you have two options: Include or Exclude.
    • That means you can choose whether to include values in the dependent picklist or remove existing values by clicking the exclude button. Then click the Save button.
Edit Field Dependency in Salesforce

In this way, you can edit the existing field dependencies in Salesforce.

Find Field Dependency by SOQL Query in Salesforce

When navigating to an object’s Field and Relationship, you’ll find multiple fields. To identify controlling and dependent fields, use a SOQL query in Salesforce.

Using the following steps, you can query to find controlling and dependent fields in Salesforce.

  1. To navigate the Query Editor, go to the Gear icon and select Developer Console.
How to Find Field Dependency by SOQL Query in Salesforce
  1. In the developer console, you will see the code editor; here, we want to query for field dependency. For that, click on the Query Editor. Then, enter the query in the query editor.
SELECT Label, QualifiedApiName, DataType, ControllingFieldDefinition.DeveloperName FROM FieldDefinition WHERE EntityDefinition.QualifiedApiName = 'Patient__c' AND ControllingFieldDefinitionId != null

Here, I explained the attributes that we used in the SOQL query:

  • Label: It will fetch the name you gave for the dependent field.
  • QualifiedApiName: It will display the API name of the dependent field.
  • DataType: The datatype that you selected for the particular field.
  • ControllingFieldDefinition: It will display the Field Name of the controlling field.
  • EntityDefinition.QualifiedApiName: Here, you need to provide the Object name from which you want to query the field dependency. For example, for standard objects, only provide ObjectName; for custom objects, provide ObjectName__c.
  • ControllingFieldDefinitionId != null: Make sure the controlling field ID is not equal to null, so we get only the controlling field and its dependent values.

Then, to run the query, click the Execute button.

SOQL Query to find field dependency in Salesforce
  1. After executing the query, you will get the field dependency attributes we entered.
Find Field Dependency by Query in Salesforce

In this way, we can use SOQL queries to get the field dependency in Salesforce.

Conclusion

I hope you have an idea about field dependency in Salesforce. In that, we have seen what field dependency is and its uses, types of field dependency, considerations while creating field dependency, and where we can use it. Then, I explained how to set up field dependency in Salesforce with examples and explanations.

I have also explained how to edit dependent picklist field values in Salesforce and find field dependency by SOQL query in Salesforce.

You may like to read:

live webinar

Build Your First Agentforce AI Agent in Salesforce

We will show demo on the complete process of building your first AI Agent in Salesforce using Agentforce. You’ll learn how agents work and how to connect your agent to Salesforce data to deliver intelligent business outcomes.

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.