I’ve faced many times two problems on SOQL queries:
1. They have not * (star) operator and hence it is really tough to list all fields on large objects.
2. If you try to modify a field name or delete or update the type, salesforce won’t allow you if it is referenced on the code. It is really painful on deploys.
As a best practices when I have to retrieve all fields I use this simple function.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public static SObject[] selectByName(String objectName, String additionalFilters){ //get object fields List<String> fieldList = new List<String>(Schema.getGlobalDescribe() .get(objectName).getDescribe().fields.getMap().keySet()); String fields = String.join(fieldList,','); return (SObject[])Database.query( 'SELECT '+fields+' FROM '+objectName+ ' '+additionalFilters ); } system.debug( Helper.selectByName('Contact', 'Order by Lastname Limit 20')); |