How to get all Address Fields in Salesforce?

How to get all Address Fields in Salesforce?

Sample Code:


  1. Set < String > listsObjs = new Set < String > {‘Account’‘Lead’};      
  2. Map<String, Schema.SObjectType > globalDescription = Schema.getGlobalDescribe();   
  3.         
  4. for ( String obj : listsObjs ) {  
  5.   
  6.     Schema.sObjectType objType = globalDescription.get( obj );  
  7.     Schema.DescribeSObjectResult r1 = objType.getDescribe();   
  8.     Map<String , Schema.SObjectField > mapFieldList = r1.fields.getMap();    
  9.   
  10.     for ( Schema.SObjectField field : mapFieldList.values() ) {    
  11.       
  12.         Schema.DescribeFieldResult fieldResult = field.getDescribe();    
  13.         String fieldLabel = fieldResult.getLabel().toLowerCase();  
  14.         Schema.DisplayType fielddataType = fieldResult.getType();  
  15.   
  16.         if ( ( fieldLabel.contains( ‘address’ ) || fieldLabel.contains( ‘state’ )   
  17.               || fieldLabel.contains( ‘street’ ) || fieldLabel.contains( ‘city’ )   
  18.               || fieldLabel.contains( ‘postal’ ) || fieldLabel.contains( ‘zip’ ) )   
  19.             && ! fieldResult.isCalculated() ) {  
  20.           
  21.             System.debug ( objType + ‘.’ + fieldResult.getName() );  
  22.           
  23.         }  
  24.           
  25.     }  
  26.       
  27. }  

In the Developer Console, select Debug Only to see the debug logs.



Leave a Reply