Let's consider the below scenario:

I have a scenario to type cast sObjectList into the List<customObject__c> so that I can access customObject fields and store it into the following:

String customValue1 = '';
String customValue2 = '';
String customValue3 = '';
String customValue4 = ''; 

Based on the recordId I'm fetching the targetobjectApi name and getting the fields to query on this object from the custom metadata created.

After constructing the SOQL - String soqlQuery = 'SELECT ' + fields + ' FROM ' + targetObjectApiName; // It returns the sobject List

Now in the switch case I have to write a logic to convert this sobject list returned into customObject__c list so that I can access fields on the customObject__c.

Please find the code below:

String recordId = 'a2r540000014DwFAAU'; 

//Create String to store the query 
String query = ''; 
String fields = ''; 

//Create String to store custom Object values  
String customValue1 = '';
String customValue2 = '';
String customValue3 = '';
String customValue4 = ''; 

//Create List to Store customObject__c 
List<customObject__c> customObjectList = new List<customObject__c>(); 

Id newId = Id.valueOf(recordId);
String targetObjectApiName =  newId.getSObjectType().getDescribe().getName();

System.debug('targetObjectApiName-------->' + targetObjectApiName);

//Get Fields From Custom Metadata
List<customMetadata__mdt> customMetadataRecords = [SELECT Field__c, Object__c FROM customMetadata__mdt WHERE Object__c = :targetObjectApiName]; 
System.debug('customMetadataRecords---------->' + customMetadataRecords); 

if(customMetadataRecords!=null && !customMetadataRecords.isEmpty()){
    for(customMetadata__mdt mdtObj: customMetadataRecords){ 
        fields = mdtObj.Field__c; 
        System.debug('fields---------->' + fields);

//Construct SOQL Query 
String soqlQuery = 'SELECT ' + fields + ' FROM ' + targetObjectApiName; 
System.debug('soqlQuery----------->' + soqlQuery);

List<sObject> sObjectList = Database.query(soqlQuery); 
System.debug('sObjectList--------> ' + sObjectList);

//Store Values 
if(sObjectList!=null && !sObjectList.isEmpty()){
    switch on targetObjectApiName {
            when 'customObject__c' {    
               //Need help here in typecasting
               customObjectList = (List<customObject__c>) sObjectList; 
               //Need help here in storing customField__c values 
               customValue1 = customObjectList[0].customField1__c;
               customValue2 = customObjectList[0].customField2__c;
               customValue3 = customObjectList[0].customField3__c;      
               customValue4 = customObjectList[0].customField4__c;
            when else {      
                return 'Other Option';
        }//End Switch 

Any kind of help is appreciated. Thank you 

2 Answers 2


You can just statically declare the type in this switch block. Keeping it dynamic once you have gotten this far adds little (if any) value.

switch on targetObjectApiName {
    when 'customObject__c' {
        List<CustomObject__c> concreteRecords = sObjectList;
        CustomObject__c record = concreteRecords[0];
        customValue1 = record.CustomField1__c;
        // etc.

You do not need to cast the records at all. The SObject base class provides the get method that allows retrieval of a field value (as Object, which can be converted to a string using String.valueOf, for example). As long as you can get the field API names from your custom metadata, you can pass them in to this get method to find the field values.

You can, of course, use the object type name to retrieve the relevant custom metadata record(s) too.

See the SObject documentation for more detail.

  • Dynamic get calls tend to be slower than static field paths, so there could be performance implications.
    – Adrian Larson
    Commented Feb 11, 2022 at 21:21
  • If they are using custom metadata records with entity and field definitions then that will have a far bigger performance impact (unless this has been massively improved in the last 3 or 4 platform releases). It was terrible when I last tested it.
    – Phil W
    Commented Feb 11, 2022 at 21:27
  • @adrianlarson I would also say it is better not to optimize early. Better to actually measure and improve if necessary.
    – Phil W
    Commented Feb 11, 2022 at 21:29
  • Right that's kind of what I'm saying. Dynamic get calls are poor performance.
    – Adrian Larson
    Commented Feb 11, 2022 at 21:58
  • But if the solution requires use of admin-set custom metadata to map the detail, hard-coding doesn't fit the requirement.
    – Phil W
    Commented Feb 12, 2022 at 8:56

You must log in to answer this question.

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