11

How can I show only picklist values allowed for a specific Record Type?

For example:

Case.Status values are "Pending", "Open", "Closed"

  • Case RecordType1 only allows the Pending value
  • Case RecordType2 allows Open and Closed values

I'm developing a Lightning component in a Visualforce Page but I haven't found a convenient lightning-way to do this. I'm looking for a stable solution, that allows me to remove the component from the VF page at any time.

In Visualforce I used to invoke the describeLayout API offered with connection.js and it worked well:

sforce.connection.describeLayout(
    selectedObjectType,
    new Array(recordTypeId), {
       onSuccess: handleDescribeLayoutSuccess,
       onFailure: handleDescribeLayoutFailure
    }
); 

So I tried to import connection.js in my Lightning component using ltng:require and retrieving the SessionId with a server-side action. It works well with Locker Service disabled, but when it is enabled the describeLayout response always enter in the onFailure callback.

The data provided to the onFailure function shows the layout describe in XML, but it is treated as an error.

The response data is below (just skipped part of the inner describe):

Remote invocation failed, due to: 
<?xml version="1.0" encoding="UTF-8"?>
  <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Header>
      <LimitInfoHeader><limitInfo><current>2</current><limit>15000</limit><type>API REQUESTS</type></limitInfo>
      </LimitInfoHeader>
    </soapenv:Header>
    <soapenv:Body>
       <describeLayoutResponse>
         <result><-- correct layout describe, skipped for readability --></result>    
       </describeLayoutResponse>
    </soapenv:Body>
  </soapenv:Envelope> status code: 
4
  • 1
    I hope this sort of thing isn't still necessary, but this is what I did for Picklist values by record type for AngularJS UI 18 months ago to provide the data to the JavaScript based UI.
    – Keith C
    Nov 22, 2016 at 18:26
  • Can you add some more detail around what you mean by "not working"? That is pretty vague and not very helpful.
    – Adrian Larson
    Nov 22, 2016 at 18:27
  • 1
    Lightning's content security and session ID policy does not allow the use of any API endpoints - has been that way from day one and is not something Locker is responsible for - what makes you believe this is a Locker issue? Should not work in a Lightning Compinent regardless of Locker being activated. Is this a LC in Visualforce scenario or are you simply trying to port existing VF ways of doing things to Lightning? Nov 22, 2016 at 18:31
  • 3
    Thanks for the replies. The sessionId was retrieved via a server-side method and it worked since LockerService was activated in our org. @DougChasman are there any ways to do this (filter picklist values for record type) in a "lightning way"?
    – ctrl-z
    Nov 22, 2016 at 18:38

4 Answers 4

2

You can now use the UI API from your lightning component's apex controller to retrieve this information. It's not ideal because you have to use an API call into your own org, but at least it is now possible.

See the salesforce documentation on the UI API here.

The callout would be /ui-api/object-info/{objectApiName}/picklist-values/{recordTypeId}/{fieldApiName}. This retrieves picklist values based on the record type ID you pass it.

3
  • Using the above API and salesforce documentation I am not getting any response. Although using /ui-api/object-info/{objectApiName} I am getting the object info. Apr 9, 2018 at 15:56
  • Doesn't mean this answer is wrong. You are just calling the API incorrectly. May 6, 2018 at 7:27
  • @RishiGolyan I just tested this API call using a sandbox and Workbench and I was able to successfully retrieve picklist values from the Account object for a specific record type. Make sure you are calling it as per the get started intro docs, and you are calling it against a supported object
    – Kyle
    May 7, 2018 at 16:35
2

I just found this for LWC, that allows you to get the picklist options for a record type https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.reference_wire_adapters_picklist_values_record

So in theory you could write a generic LWC that you pass the object, record type name, field name and it would return the list of options.

Here is an example from their git repo - https://github.com/trailheadapps/lwc-recipes/tree/master/force-app/main/default/lwc/wireGetPicklistValuesByRecordType

1

There is a component in Lighting : (check document here)

lightning:recordEditForm

It is working fine without any other apex or controller code :

<lightning:recordEditForm recordId="{!v.case.Id}" recordTypeId="{!v.recordTypeId}" objectApiName="Case">
       <div class="slds-col slds-size_1-of-1">
             <lightning:inputField fieldName="Status" value="{!v.case.Status}"/>
        </div>
</lightning:recordEditForm>
0

I was able to go get this working with the wire adapters. LWCs now provide getPicklistValues to get picklist values based on record type

import { LightningElement, wire, api } from 'lwc';
 import { getPicklistValues } from 'lightning/uiObjectInfoApi';
 
 
 export default class PicklistTest extends LightningElement {
     @api recordTypeId;
     @api fieldApiName;
     picklistValues;
 
     @wire(getPicklistValues, { recordTypeId: this.recordTypeId, fieldApiName: this.fieldApiName })
     wiredNewWorkload({ data, error }) {
         if (data) {
             this.picklistValues = data.values;
             this.dispatchEvent('picklistvalueload', {
                 detail: this.picklistValues
             });
         }
         if (error) {
             console.error('Error in retrieving picklist values!' + JSON.stringify(error));
         }
     }
 }

recordTypeId and fieldApiName (Case.Status) can be set inside the parent Aura component which can get the picklist values based on picklistvalueload event.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .