3

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));
                 }
5

Typically you should use the API Name as the map key because it is unique. Also if you want to ensure that you get an exact match, it is probably best to use field describes.

fieldsList.add(Opportunity.My_Custom_Field__c.getDescribe().getName());

Even a simple case mismatch or trailing whitespace could cause your manual additions to fail, and if those labels change then poof, your code breaks again.

In your VisualForce, you can still render the label using the API Name:

{!$ObjectType.Opportunity.fields[apiName].label}
|improve this answer|||||
  • But in my table I want to display the label and value by using the key and the value of the map, that's why I chose getLabel() instead of getName() – Yousra May 6 '15 at 14:13
  • 1
    Again, it is not a unique key and is a bad idea to use as your map key. You can still retrieve the label, I modified my answer to show how. – Adrian Larson May 6 '15 at 14:16
  • Deos sObjectType.getDescribe().fields.getMap() get every single field for that object ? It seems to me like it is not getting lookups and formula fields. Is that correct ? @AdrianLarson – SallyRothroat Jun 4 '19 at 23:29
  • Incorrect. It gets every field. – Adrian Larson Jun 5 '19 at 0:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.