I'm trying to create a VF page for reporting. I'm hardcoding the object for right now (Product2) but would like the column's values to be dynamically populated based on the environment in case the fields are different. My error is on the VF page stating Invalid field for SObject Product2
. Here is my VF snippet with commented out, hardcoded columns that work.
<apex:pageBlockSection title="Results" collapsible="false">
<apex:pageBlockTable value="{!queryResultsList}" var="prod2">
<apex:repeat value="{!fieldAPINames}" var="fieldAPIName">
<apex:column value="{!prod2.[fieldAPIName]}"/>
</apex:repeat>
<!--
<apex:column value="{!prod2.Name}"/>
<apex:column value="{!prod2.Description}"/>
<apex:column value="{!prod2.IsActive}"/>
<apex:column value="{!prod2.ProductCode}"/>
<apex:column value="{!prod2.Family}"/>
-->
</apex:pageBlockTable>
</apex:pageBlockSection>
And here is the controller I'm calling. I'm very new to apex and controllers so feel free to question what I'm doing or if you could propose a better option that would be great!
public class ProductCatalogPageController {
public List<Product2> queryResultsList{get;set;}
public String query{get;set;}
public String strOfStuff{get;set;}
public Map<String, Schema.SObjectField> fieldAPINames{get;set;}
public PageReference testAction(){
String SobjectApiName = 'Product2';
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();
String csvFields = '';
for(String fieldName : fieldMap.keySet()){
if(csvFields == null || csvFields == ''){
csvFields = fieldName;
}else{
csvFields = csvFields + ', ' + fieldName;
}
}
query = 'select ' + csvFields + ' from ' + SobjectApiName;
queryResultsList = Database.query(query);
strOfStuff = 'Stuff goes here';
return null;
}
}
Apex
in the first place? Why not use an actualReport
? You can export it as.csv
and it requires zero code.Fieldset
. You don't really want to pull every field, I would think.