apex – SOQL select columns with names that contain specific word

No, there is not.

SOQL and SOSL generally demand that you explicitly list out all of the fields that you wish to query. FIELDS([ALL|CUSTOM|STANDARD]), and the automatic inclusion of the Id field (and RecordTypeId if the SObject has at least one custom record type defined for it) are the only exceptions to that at this time. In Apex, we can only use FIELDS(STANDARD).

If you only want to query a few (say, less than 15) fields, and only those fields, your best option is probably just to type them all out manually. Beyond that, you could use the information from SObject’s Describe to get all of the fields on a given object (e.g. Schema.SObjectType.Account.fields.getMap().keySet()), and then iterate over them to filter out the ones that don’t start with ‘location’ (startsWith() is a String method). You’d need to build a dynamic query to use that approach though.

As a final note, if you truly do have that many “location*” fields, that’s likely a sign of poor design. It sounds to me like you should consider creating a new SObject to hold multiple pieces of location data (rather than having X number of fields on your current object).

Read more here: Source link