Filter Picklist Values Based on Record Type in Lightning

Filter Picklist Values Based on Record Type

  • Retrieving a picklist value based on record type is possible in VF page using standard controller
  • In lightning, Salesforce UI API supports a functionality to filter a picklist value based on record type based on this idea
  • You can download the code from Here

 

Use Case: 

I have picklist values based on Record Type defined as below:

Now, If we want to get this metadata in lightning and want to display dropdowns based on Record Type of any given record.

We might be thinking of using any Schema class. But unfortunately, we don’t have schema class supported for this use case.  We wanted output like this for the record which has record type mentioned in the previous screenshot.

So, Let’s take a look at the solution.

Below is the approach to leverage Salesforce UI API to retrieve picklist value based on record type:

  • Create a connected app
  • Create an Auth Provider
  • Create Named Credentials
  • Create lightning component
  • Create Apex class to call UI API

 

  1. Create a connected app:
  • setup > create apps > create new app
  • Provide any URL as a callback URL for now. We will replace it with new URL in later steps when we will create Auth Provider
  • For Oauth scope provide “Full access (full)” & ”Perform requests on your behalf at any time (refresh_token, offline_access)”

connected_app

 

  1. Create an Auth Provider
  • Create Auth Provider from Setup > Security Control > create a new Auth. provider
  • Set provider type as “Salesforce”
  • Provide consumer key & consumer secret from apps that is created in step 1
  • Set the default scope as a “full refresh_token offline_access”
  • Click save

auth_provider

  • Cope the Callback URL from “Salesforce Configuration” section and paste it to Callback URL in app that you have created in step 1.

 

  1. Create Named Credentials
  • Setup > Named Credentials > Create new named credentials
  • Set the URL as a base URL of your org
  • Set Identity type as a “Named Principal”
  • Set authentication protocol as a “Oauth 2.0”
  • Select Authentication Provider as a Auth. Provider that you created in step 2.
  • Provide the scope as a “full refresh_token offline_access”
  • Select “Start Authentication Flow on Save”
  • Click Save > It will ask for credentials to authenticate, provide that

named_credentials

 

  1. Create a lightning component

  • Javascript controller.js code

  • Javascript helper.js code

 

  1. Create an apex controller