How to get all First Name and Last Name Fields in Salesforce?

How to get all First Name and Last Name 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( ‘first name’ ) || fieldLabel.contains( ‘last name’ )   
  17.               || fieldLabel.contains( ‘firstname’ ) || fieldLabel.contains( ‘lastname’ ) )   
  18.             && ! fieldResult.isCalculated() ) {  
  19.           
  20.             System.debug ( objType + ‘.’ + fieldResult.getName() );  
  21.           
  22.         }  
  23.           
  24.     }  
  25.       
  26. }  

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



Leave a Reply