July 29, 2020

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;
          
            }
      
        }

    }
    
}

6 comments:

  1. Thank you so so much ... this has made it really easy now for me .. Just one question Mag so this part
    String.valueOf( objectFields.get( s ).getDescribe().getLabel() ).contains( 'Email' ) &&
    String.valueOf( objectFields.get( s ).getDescribe().getType() ) == 'String'

    will give me those field where the field is text but containing email value right ?

    I was thinking to check the value for these kind of text fields with some email regex pattern if there is any email. I think now I don't need that to compare with email Regex right please confirm. Thank you so much

    ReplyDelete
    Replies
    1. Yes correct. It will fetch all the fields where the data type is Text and Label contains Email string.

      Delete
  2. yes ok thanks for all this .. but shouldn't i need to check the value also .. i mean there could be text field but that field still can store email value ...and if there is any email value in those any text field .i should consider to update those field as well by appending '.test'... that's why i was thinking if i need to extract the value also and then compare with regex ..if there is any email in those text field.. as text field also can contain emails... please respond...if i need to change anything in the code to get the field values.thanks...

    ReplyDelete
    Replies
    1. You can definitely do that. I am doing null check alone using this - if ( obj.get( strField ) != null )

      Delete
  3. Ohh ok then that's all sum up my requirement many thanks to you .. I was thinking obj.get( strField ) will only retrieve the fieldname not the value .. If it picks the value then I can compare with the regex and that will be done thanks. :)

    ReplyDelete
    Replies
    1. You can definitely do that but you may hit Apex CPU Time if you add so much logic.

      Delete