List<CustomObject__c> val= Database.query('SELECT ID, '+ fieldName +' FROM '+ objectName +' );
I need records according to fieldName and objectName given and store the result to 'val' list.
fieldName and objectName are choosen from picklist.
Hi Sanjai,
dry and run below code use as per your requirement
Query -1
String query = 'Select Id,Email From Contact Where Email like \'%google.com \' ';
private static String generateQuery(List<DatatableColumnDetail> datatableColumnDetails, String objectAPIName){
String query = '';
query += 'SELECT Id, ';
for(DatatableColumnDetail datatableColumnDetail : datatableColumnDetails){
if(!checkContains(query,datatableColumnDetail.fieldAPIName + ', ')){
query += datatableColumnDetail.fieldAPIName + ', ';
}
if(datatableColumnDetail.fieldType == HYPERLINK_TYPE){
if(!checkContains(query,datatableColumnDetail.fieldToShow + ', ')){
query += datatableColumnDetail.fieldToShow + ', ';
}
}
}
query = query.removeEnd(', ');
query += ' FROM ' + objectAPIName;
System.debug('Query ' + query);
return query;
}
Query- 2
String query = 'SELECT '
+ 'Name, Description__c, Geolocation__Latitude__s, '
+ 'Geolocation__Longitude__s, Picture__c, Contact__r.Name, '
+ 'BoatType__c, BoatType__r.Name, Length__c, Price__c '
+ 'FROM Boat__c';
if (String.isNotBlank(boatTypeId)) {
query += ' WHERE BoatType__c = :boatTypeId';
}
query += ' WITH SECURITY_ENFORCED ';
return Database.query(query);