How to fetch all the custom settings and update all the email data type field values with some value in Salesforce?

How to fetch all the custom settings and update all the email data type field values with some value in Salesforce?

Sample Code:
Map < String, Schema.SObjectType > gd = Schema.getGlobalDescribe();

for ( String objectName : gd.keySet() ) {

    Schema.SObjectType result = gd.get( objectName );
  
    if ( result.getDescribe().isCustomSetting() ) {
        
        String query = ‘SELECT ‘;
        List < String > listFields = new List < String >();
        Map < String, Schema.SObjectField > objectFields = result.getDescribe().fields.getMap();
        for ( String s : objectFields.keySet() ) {
          
            /* Checking whether the Field type is Email or Field Label contains Email */
            if ( String.valueOf( objectFields.get( s ).getDescribe().getType() ) == ‘Email’ ||
                 ( String.valueOf( objectFields.get( s ).getDescribe().getLabel() ).contains( ‘Email’ ) &&
                   String.valueOf( objectFields.get( s ).getDescribe().getType() ) == ‘String’ ) ) {
          
                system.debug( ‘Inside’ );
                query += s + ‘,’;
                listFields.add( s );
          
            }
          
        }
      
        if ( listFields.size() > 0 ) {
      
            query = query.removeEnd( ‘,’ );
            query += ‘ FROM ‘ + objectName;
            List < sObject > listRecords = Database.query( query );
          
            if ( listRecords.size() > 0 ) {
          
                for ( sObject obj : listRecords ) {
              
                    for ( String strField : listFields ) {
                       
                        if ( obj.get( strField ) != null )
                            obj.put( strField, obj.get( strField ) + ‘.test’ );
                  
                    }
              
                }
              
                update listRecords;
          
            }
      
        }

    }
    
}

Leave a Reply