function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Steve Berley [Left Propeller]Steve Berley [Left Propeller] 

writing generic code - how to abstract/dereference a variable to subject type

I'm writing some generic code and want to refer to an sobject type using a variable.  The goal being to write code similar to ...
string t = 'custom_object__c';
list<t> objs = new list<t>(); // creating a list of custom_object__c's
Of course, string is not the right type for t, but thi sis the idea I'm trying to accomplish

Can this even be done in apex?  

Thanks,

 
Alain CabonAlain Cabon
Hello Steve,

Apex considers the concept of "dynamic apex" for genericity.   
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dynamic.htm

More generally: the new way of writing the classes and triggers in Salesforce is to use the concept of separation of concern (service layer principles, domain and selector layor and unit of work among other thing)
https://trailhead.salesforce.com/module/apex_patterns_sl
https://trailhead.salesforce.com/module/apex_patterns_dsl

Regards
Alain