Skip to content

Instantly share code, notes, and snippets.

@renatoliveira
Created November 14, 2018 15:02
Show Gist options
  • Save renatoliveira/c09119dbb8d55a3ce5534c21c88568b5 to your computer and use it in GitHub Desktop.
Save renatoliveira/c09119dbb8d55a3ce5534c21c88568b5 to your computer and use it in GitHub Desktop.
Method in which the platform gets all the fields for an object, and then executes a dynamic query with all of the fields. Retrieves one or more records.
public List<SObject> getObjects (Set<Id> recordIds) {
Id dummyId = (new List<Id>(recordIds))[0];
String objectName = Id.valueOf(dummyId).getSObjectType().getDescribe().getLocalName();
Set<String> fields = Id.valueOf(dummyId).getSObjectType().getDescribe().fields.getMap().keySet();
String query = 'SELECT ';
for (String f : fields) {
query += f + ', ';
}
query = query.removeEnd(', ');
query += ' FROM ' + objectName + ' WHERE Id IN :recordIds';
return Database.query(query);
}
public SObject getObject (Id recordId) {
return getObjects(new Set<Id>{recordId})[0];
}
// example
// System.debug(getObjects(new Set<Id>{'00629000007E0MsAAK'}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment