Skip to content

Tips and tricks

Apex code snippets that provide helpful functionality.

Picklist entry: Label to API name

Get the API name of a picklist entry by querying its label. If you are using the Translation Workbench to set the label, keep in mind that it will depend on the users language settings.

public static String picklistValue(String objectName, String fieldName, String label) {
    // Get the object's schema and field
    Schema.SObjectType sObjectType = Schema.getGlobalDescribe().get(objectName);
    Schema.DescribeSObjectResult describeRes = sObjectType.getDescribe();
    Schema.SObjectField sObjectField = describeRes.fields.getMap().get(fieldName);

    // Get picklist values for the field
    Schema.DescribeFieldResult fieldDescribe = sObjectField.getDescribe();
    List<Schema.PicklistEntry> picklistValues = fieldDescribe.getPicklistValues();

    // Iterate through picklist values and match the label
    for (Schema.PicklistEntry entry : picklistValues) {
        if (entry.getLabel() == label) {
            return entry.getValue();
        }
    }

    return null;
}

To get the ID of the entry "Automotive" in Account.Industry run:

String industryId = picklistValue('Account', 'Industry', 'Automotive');

For more options (including a Flow-only implementations), check medium.com.

Record Type ID

To obtain the ID of a record type (by its developer name) without using a SOQL query (governor limits!), you can use the following command (source):

Id prospectRt = SObjectType.Account.getRecordTypeInfosByDeveloperName().get('End_Customer').getRecordTypeId();

If you are trying to query a record type that does not exist (on the specified object) you will receive the error: static can only be used on fields of a top level type.

Test setup: Create records and setup entries

When trying to create records (e.g. Accounts, Opportunities) and "setup entries" (e.g. Users, Custom Settings) in the @testSetup method, you will encounter the MIXED_DML_OPERATION error. The reason is, that it is not allowed to alter records of these two types in the same transaction. More details can be found on developer.salesforce.com.

To fix this, use the following code (source):

@testSetup
static void setupTestData() {
    System.runAs(new User(Id = UserInfo.getUserId())) {
        // Create your setup entries here.
    }
    // Create your records here.
}