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
keshin okawakeshin okawa 

N>Help Error: Compile Error: expecting right curly bracket, found 'for' at line 8 column 7

here is my code...

public class objectlist {
    //Do not forget to get instance in constructor for this list
    //String type='Account';
     Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
       List<string> fieldLable = new List<string>();
        
       Map <String, Schema.SObjectField> fieldMap = gd.get('Account').getDescribe().fields.getMap();
       for(Schema.SObjectField fieldAPI : fieldMap.values())
       {
               fieldLable.add(+ '<' +fieldAPI.getDescribe().getName()+ '/>' );
       }
       
       system.debug('fieldLable-----'+fieldLable);
}

IDK what part is wrong...pls help me fix this...THX!!!
Best Answer chosen by keshin okawa
surasura
replace getName() with getLabel()
 
public class objectlist {
    //Do not forget to get instance in constructor for this list
    //String type='Account';
	public List<string> fieldLable {get;set;}
	
	public objectlist()
	{
		getAccountFields
	
	}
	
	
	public void getAccountFields()
	{
          Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
           fieldLable = new List<string>();
        
          Map <String, Schema.SObjectField> fieldMap = gd.get('Account').getDescribe().fields.getMap();
          for(Schema.SObjectField fieldAPI : fieldMap.values())
          {
               fieldLable.add(+ '<' +fieldAPI.getDescribe().getLabel()+ '/>' );
          }
          fieldLable.sort();
         system.debug('fieldLable-----'+fieldLable);
	}   
	   
}

 

All Answers

shiva pendemshiva pendem
HI miczster,

Try this following link which may be helpfull to you.

http://forceschool.blogspot.in/2011/06/dynamic-binding-when-object-name-is.html

Thanks,
Shiva.
surasura
you code should be inside a method not directly inside class
 
public class objectlist {
    //Do not forget to get instance in constructor for this list
    //String type='Account';
	
	public void getAccountFields()
	{
          Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
          List<string> fieldLable = new List<string>();
        
       Map <String, Schema.SObjectField> fieldMap = gd.get('Account').getDescribe().fields.getMap();
       for(Schema.SObjectField fieldAPI : fieldMap.values())
       {
               fieldLable.add(+ '<' +fieldAPI.getDescribe().getName()+ '/>' );
       }
       
       system.debug('fieldLable-----'+fieldLable);
	}   
	   
}

 
JabarajJabaraj
where is the method? You have to write the code inside the method.

Public class classname{
    public void methodname{
     ------------code---------
   }
   }

Note :same code will be working in developer console.



 
keshin okawakeshin okawa
How do I put the output in a table? How do I automatically sort the output fields before passing them to the table?
surasura
below code will do the soring 

just call the sort() method on your list
 
public class objectlist {
    //Do not forget to get instance in constructor for this list
    //String type='Account';
	public List<string> fieldLable {get;set;}
	
	public objectlist()
	{
		getAccountFields
	
	}
	
	
	public void getAccountFields()
	{
          Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
          List<string> fieldLable = new List<string>();
        
          Map <String, Schema.SObjectField> fieldMap = gd.get('Account').getDescribe().fields.getMap();
          for(Schema.SObjectField fieldAPI : fieldMap.values())
          {
               fieldLable.add(+ '<' +fieldAPI.getDescribe().getName()+ '/>' );
          }
          fieldLable.sort();
         system.debug('fieldLable-----'+fieldLable);
	}   
	   
}

this is a sample page , do necessary modifications according to your requirment 
 
<apex:page Controller="objectlist">

    <apex:pageBlock title="My Content">

        <apex:pageBlockTable value="{!fieldLable}" var="item">

            <apex:column value="{!item}" headervalue="field labels"/> 

        </apex:pageBlockTable> 

    </apex:pageBlock> 

</apex:page>

 
keshin okawakeshin okawa
this is the result...no output field names
User-added image
surasura
sorry my bad , replace your class with below code
public class objectlist {
    //Do not forget to get instance in constructor for this list
    //String type='Account';
	public List<string> fieldLable {get;set;}
	
	public objectlist()
	{
		getAccountFields
	
	}
	
	
	public void getAccountFields()
	{
          Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
           fieldLable = new List<string>();
        
          Map <String, Schema.SObjectField> fieldMap = gd.get('Account').getDescribe().fields.getMap();
          for(Schema.SObjectField fieldAPI : fieldMap.values())
          {
               fieldLable.add(+ '<' +fieldAPI.getDescribe().getName()+ '/>' );
          }
          fieldLable.sort();
         system.debug('fieldLable-----'+fieldLable);
	}   
	   
}
keshin okawakeshin okawa
THX so much sura...here is the result...
User-added image
I was wondering what do I change to retrieve the Field Label instead of Field API Name...
surasura
replace getName() with getLabel()
 
public class objectlist {
    //Do not forget to get instance in constructor for this list
    //String type='Account';
	public List<string> fieldLable {get;set;}
	
	public objectlist()
	{
		getAccountFields
	
	}
	
	
	public void getAccountFields()
	{
          Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
           fieldLable = new List<string>();
        
          Map <String, Schema.SObjectField> fieldMap = gd.get('Account').getDescribe().fields.getMap();
          for(Schema.SObjectField fieldAPI : fieldMap.values())
          {
               fieldLable.add(+ '<' +fieldAPI.getDescribe().getLabel()+ '/>' );
          }
          fieldLable.sort();
         system.debug('fieldLable-----'+fieldLable);
	}   
	   
}

 
This was selected as the best answer
keshin okawakeshin okawa
Last thing...How do I only show the custom fields only? I don't need the standard fields( ex. Account ID , Account Description )
surasura
:D try below code  there is a method called IsCustom() 
 
public class objectlist {
    //Do not forget to get instance in constructor for this list
    //String type='Account';
	public List<string> fieldLable {get;set;}
	
	public objectlist()
	{
		getAccountFields
	
	}
	
	
	public void getAccountFields()
	{
          Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
           fieldLable = new List<string>();
        
          Map <String, Schema.SObjectField> fieldMap = gd.get('Account').getDescribe().fields.getMap();
          for(Schema.SObjectField fieldAPI : fieldMap.values())
          {
		       if(fieldAPI.getDescribe().isCustom() )
			   {
               fieldLable.add(+ '<' +fieldAPI.getDescribe().getLabel()+ '/>' );
			   }
          }
          fieldLable.sort();
         system.debug('fieldLable-----'+fieldLable);
	}   
	   
}

refer this for all available methods in fields

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_fields_describe.htm#apex_methods_system_fields_describe

 
keshin okawakeshin okawa
thx once again sura..I think I know what to do next for now....