Apex class method that fetches all the fields of a record except two specified fields
public class RecordFetcher {
public static SObject getRecordWithExclusions(Id recordId, String excludeField1, String excludeField2) {
// Get the SObject type from the ID
SObjectType objType = recordId.getSObjectType();
// Get the describe result for the object
Schema.DescribeSObjectResult describeResult = objType.getDescribe();
// Get all fields of the object
Map<String, Schema.SObjectField> fieldsMap = describeResult.fields.getMap();
// Build the SOQL query dynamically
String query = 'SELECT ';
List<String> fieldList = new List<String>();
for (String fieldName : fieldsMap.keySet()) {
// Exclude the specified fields
if (!fieldName.equalsIgnoreCase(excludeField1) && !fieldName.equalsIgnoreCase(excludeField2)) {
fieldList.add(fieldName);
}
}
// Join the fields into a query string
query += String.join(fieldList, ',') + ' FROM ' + objType.getDescribe().getName() + ' WHERE Id = :recordId';
// Execute the query and return the record
return Database.query(query);
}
}
Apex class method that fetches all the fields of a record except two specified fields
Reviewed by dasfrogpractice
on
08:06
Rating:
No comments: