I'm using Schema to retreive the fields and put them in a map that I can use in my Visualforce page.
public Map<String,String> m1{get;set;}
public List<String> fieldsList {get; set;}
Schema.DescribeSObjectResult oppDescribe = T1_Passif__c.sObjectType.getDescribe();
Map<String, Schema.SObjectField> fields = oppDescribe.fields.getMap();
for (Schema.SObjectField field: fields.values())
{
Schema.DescribeFieldResult dr = field.getDescribe();
if(dr.isCustom())
{
if(obj.get(field)==null)
{
m1.put(field.getDescribe().getLabel(),'');
}
else
{
m1.put(field.getDescribe().getLabel(),String.valueOf(obj.get(field)));
}
}
}
//add their labels to a list to maintain an order
FieldsList.add('Capital social ou personnel (1)');
FieldsList.add('Moins: actionnaires');
visualforce:
<apex:pageBlockTable value="{!fieldsList}" var="dirKey">
<apex:column >
<apex:outputText value="{!dirKey}" />
<apex:facet name="header"><div align="center"> PASSIF </div></apex:facet>
</apex:column>
<apex:column style="text-align:center">
<apex:outputText value="{!m1[dirKey]}"/>
<apex:facet name="header" > <div align="center"> Exercice </div> </apex:facet>
</apex:column>
</apex:pageBlockTable>
And I get the following error even if the field I'm trying to access exist in the object custom fields and should exist in the map.
the only solution is to manually add:
FieldsList.add('Total des capitaux propres (A)');
if(obj.Total_des_capitaux_propres_A__c==null)
{
m1.put('Total des capitaux propres (A)','');
}
else
{
m1.put('Total des capitaux propres (A)',String.valueOf(obj.Total_des_capitaux_propres_A__c));
}