Salesforce released some really exciting features in the Winter 22 release! In this blog, we’ll be discussing two useful features for Platform Developers.
- Retrieve Custom Metadata Records:
You no longer need to write a Salesforce Object Query Language (SOQL) to access custom metadata records in Apex. There are new methods similar to accessing Custom Settings. This removes any SOQL limits, making the code cleaner and faster.
- getAll() : Returns a map containing custom metadata records for the specific custom metadata type. The map’s keys are the IDs of the records and the map’s values are the record sObjects.
- getInstance(recordId) : Returns a single custom metadata type record sObject for a specified record ID.
- getInstance(developerName) : Returns a single custom metadata type record sObject for a specified developerName field of the custom metadata type object.
- getInstance(qualifiedApiName) : Returns a single custom metadata type record sObject for a qualified API name.
Example: This sample returns a single record sObject for the custom metadata type named Sample_mdt with developerName specified as FirstRecord.
Sample_mdt cm = Sample_mdt.getInstance(‘FirstRecord’);
If you note it is now similar to the way we retrieve Custom Settings.
2. SOQL FIELDS() Function
We all know how to write a SOQL, but sometime if you don’t know what fields an object has, you must first get a description of the object. Typically, you use a call to first get a description of the object, then parse the description to identify the fields. Then you construct a SOQL query that specifies the fields, and then make another call to submit the query.
To simplify this process, the FIELDS() function lets you select fields easily, without knowing their names in advance. The FIELDS() function eliminates the need for a round trip to the server to prepare a SOQL statement, eliminates the need for research and a lot of typing, simplifies query statements, and makes it much easier to explore the shape of your objects. It also helps avoid exceeding the character limits on SOQL queries and the URI length limit for REST calls.
You can now include any of these in the field list:
- FIELDS(ALL)—to select all the fields of an object.
- FIELDS(CUSTOM)—to select all the custom fields of an object.
- FIELDS(STANDARD)—to select all the standard fields of an object.
In each case, FIELDS() respects field-level security so it only shows the fields that you have permission to access. This function is available in API version 51.0 and later.
- SELECT FIELDS(ALL) FROM Account LIMIT 200
- SELECT FIELDS(CUSTOM) FROM Account LIMIT 200
- SELECT FIELDS(STANDARD) FROM Account WHERE Id IN : tempSet
You can also mix FIELDS() with other field names in the field list. For example:
- SELECT Name, Id, FIELDS(CUSTOM) FROM Account LIMIT 200
- SELECT someCustomField__c, FIELDS(STANDARD) FROM Account
FIELDS() is supported in these platform features:
- The SOQL language wherever query or queryAll operations can be executed.
- The Salesforce CLI.
- The /query and /queryAll resources in the Lightning Platform REST API.
- The query() and queryAll() operations in the Lightning Platform SOAP API.
The FIELDS() function is a brilliant addition but we if you already know which fields you want to retrieve, you’ll get better performance by specifying them explicitly rather than using FIELDS() and retrieving more fields than you need.