function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
baller4life7baller4life7 

FIELDSET elements usage in APEX

Hi guys,

I have a fieldset called "States" with all 51 us states: AL__c, AR__c, AZ__c, CA__c, etc...

Now I want to add all field values and labels to an item list like this (this is pseudo-code):

 

for( <fieldset-element> : <all fieldset-elements>)

{

items.add(<fieldset-element>.value, <fieldset-element>.label);

}

 

How can I do that in Apex?

 

Thanks

Josh :-)

Shashikant SharmaShashikant Sharma

Right now Field Set are not available in APEX, so you can not loop over field set in Apex, but what you can do is create a Map where key is FieldAPIName , and Value is Label and use this map in VFP to get Label, like

 

 

 

Map<String,String> mapFSlabel = new Map<String,String>();
Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();  
        //In this example I have hard coded the object name  
        Schema.sObjectType sObjType = globalDescription.get('Account');  
          
        Schema.DescribeSObjectResult r1 = sObjType.getDescribe();  
          
        Map<String , Schema.SObjectField> mapFieldList = r1.fields.getMap();  
        Integer i = 0;  
        for(Schema.SObjectField field : mapFieldList.values())  
            {  
                Schema.DescribeFieldResult fieldResult = field.getDescribe();  
                mapFSlabel.put(fieldResult.getName(), 'Your Label'
                i++;  
            }  

 And Use this Map in field set like

<apex:page standardController="Account">  
    
   <apex:form>  
       <apex:pageBlock title="Without Field Set Use">  
       <apex:pageBlockSection title="Dynamic Object">  
           <apex:pageBlockTable value="{!$ObjectType.Account.FieldSets.DemoFieldSet}"   var="f" >
             
             
             <apex:column>
              <apex:outputLabel value="{!MapFSLable[f]}"> </apex:outputLabel>
              <apex:inputField value="{!Account[f]}"/>
             </apex:column>
       </apex:pageBlockSection>  
       </apex:pageBlock>  
   </apex:form>  
  
  
</apex:page> 

 let me know if any issues in it.

baller4life7baller4life7

Thank you Shashikant! That's actually a good idea, but not completely the solution I am looking for.

What I basically want to do is have less work in writing apex code.

 

I don't want to write:

 

1. SELECT AL__c, AR__c, (... and the other 49 states) FROM Account

 

2. items.add(account.AL__c, account.AL__c.label()); <- Pseudo-Code

    items.add(account.AR__c, account.AR__c.label()); <- Pseudo-Code

    ...

 

I want to reduce the work to a few lines!

baller4life7baller4life7

I want my code to do something like this:

 

        List<String> states = new List<String>{'AL','AR','AZ','CA','CO',
                                       'CT','DC','DE','FL','GA',
                                       'IA','ID','IL','IN','KS',
                                       'KY','LA','MA','MD','ME',
                                       'MI','MN','MO','MS','MT',
                                       'NC','ND','NE','NH','NJ',
                                       'NM','NV','NY','OH','OK',
                                       'OR','PA','RI','SC','SD',
                                       'TN','TX','UT','VA','VT',
                                       'WA','WI','WV','WY'};
        
        acc =
        [                               
            SELECT AL__c, AR__c, AZ__c, CA__c, CO__c,
                   CT__c, DC__c, DE__c, FL__c, GA__c,
                   IA__c, ID__c, IL__c, IN__c, KS__c,
                   KY__c, LA__c, MA__c, MD__c, ME__c,
                   MI__c, MN__c, MO__c, MS__c, MT__c,
                   NC__c, ND__c, NE__c, NH__c, NJ__c,
                   NM__c, NV__c, NY__c, OH__c, OK__c,
                   OR__c, PA__c, RI__c, SC__c, SD__c,
                   TN__c, TX__c, UT__c, VA__c, VT__c,
                   WA__c, WI__c, WV__c, WY__c
            FROM Account
            WHERE ID = :accId
            LIMIT 1
        ];
        
for(String state : states) 
{
items.add(new ChartDataItem(acc.<state>__c, Account.<state>__c.getDescribe().getLabel()));
}

Is there a possibility to make it work like this? Or even a better solution?

rtuttlertuttle

I know my response is a little late, but hopefully this helps:

 

 

Map<String , Schema.SObjectField> mapFieldList = Account.SObjectType.getDescribe().fields.getMap();


... your code for setting a map of states


for(String state : states) 
{ 
 items.add(new ChartDataItem(acc.get(String.escapeSingleQuotes(state) + '__c'), mapFieldList.get(String.escapeSingleQuotes(state)  '__c').getDescribe().getLabel()));
}

 

baller4life7baller4life7

Rtuttle, thank you for providing this solution! I won't be able to test it now, because I already implemented my version. If somebody tests the code and it works, please let us know by writing a reply here and I will accept it as a solution.

 

Josh :-)

 

 

rtuttlertuttle

Here is a version you can test in an anonymous apex (via system log, eclipse, or workbench).  It would be hard for anyone else to test as we don't have those fields in our org.  All you need to add into this to test is an account id at the top, then execute it anyonymously.  I'm glad to hear you were able to get a solution that worked!

 

Map<String , Schema.SObjectField> mapFieldList = Account.SObjectType.getDescribe().fields.getMap();
String accid = ''; // PUT AN ACCOUNT ID HERE FOR TESTING

        List<String> states = new List<String>{'AL','AR','AZ','CA','CO',
                                       'CT','DC','DE','FL','GA',
                                       'IA','ID','IL','IN','KS',
                                       'KY','LA','MA','MD','ME',
                                       'MI','MN','MO','MS','MT',
                                       'NC','ND','NE','NH','NJ',
                                       'NM','NV','NY','OH','OK',
                                       'OR','PA','RI','SC','SD',
                                       'TN','TX','UT','VA','VT',
                                       'WA','WI','WV','WY'};
        
        acc =
        [                               
            SELECT AL__c, AR__c, AZ__c, CA__c, CO__c,
                   CT__c, DC__c, DE__c, FL__c, GA__c,
                   IA__c, ID__c, IL__c, IN__c, KS__c,
                   KY__c, LA__c, MA__c, MD__c, ME__c,
                   MI__c, MN__c, MO__c, MS__c, MT__c,
                   NC__c, ND__c, NE__c, NH__c, NJ__c,
                   NM__c, NV__c, NY__c, OH__c, OK__c,
                   OR__c, PA__c, RI__c, SC__c, SD__c,
                   TN__c, TX__c, UT__c, VA__c, VT__c,
                   WA__c, WI__c, WV__c, WY__c
            FROM Account
            WHERE ID = :accId
            LIMIT 1
        ];


for(String state : states) 
{ 
system.debug(acc.get(String.escapeSingleQuotes(state) + '__c') + ' - ' + mapFieldList.get(String.escapeSingleQuotes(state)  '__c').getDescribe().getLabel());
}

 

Adil_SFDCAdil_SFDC

Hi Please help me on this. 

I want to query if the data type of my field is a pick list

 

here is what i am doin


Schema.DescribeSObjectResult objSchema = Opportunity.sObjectType.getDescribe();
Map<String, Schema.SObjectField> fieldMap = objSchema.fields.getmap();
system.debug ('XXXXXFields'+ fieldMap);

for (String fieldName : fieldMap.keySet()) {
fieldMap.get(fieldName).getDescribe().getpicklistvalues();//It provides to get the object fields label.
if(get(fieldname).getpicklistvalues()){
}

 

I dont know if this is right. 

 

Please help 

rtuttlertuttle

Hi Adil,

 

So close with your code.  Check out this alternate function off of the field describe called getType.  It returns an enum of the field types.

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_fields_describe.htm#apex_displaytype

 

 

Schema.DescribeSObjectResult objSchema = Opportunity.sObjectType.getDescribe();
Map<String, Schema.SObjectField> fieldMap = objSchema.fields.getmap();
system.debug ('XXXXXFields'+ fieldMap);
for (String fieldName : fieldMap.keySet()) {
Schema.DisplayType fieldType = fieldMap.get(fieldName).getDescribe().getType();
if(fieldType == Schema.DisplayType.Picklist) {
system.debug('its a picklist!');
}
}