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
Santanu Roy 18Santanu Roy 18 

How to convert Custom Setting Masking email to Batch Apex class

Hi All,
We have a requirement of masking all email by appending .invalid with all the custom settings emails . I have written this below code but I am unable to convert this to batch class .Can somebody help me to convert it please . I am new to batch apex hence facing the challenges .

public class Post_Copy_CustomSettings_Email {
    //TODO handle encodingType for SPARK AGN_SPARK_USER_Settings custom settings
    String emailUTFEncoding = 'UTF-8';
    public void UpdateCustomSettingsEmails(){
        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()) {                    
                    if (String.valueOf( objectFields.get(s).getDescribe().getType()) == 'Email' || String.valueOf(objectFields.get(s).getDescribe().getType()) == 'String'){ 
                         //&& Integer.valueOf(objectFields.get(s).getDescribe().getLength()) > 100)) {                         
                        //System.debug( 'Iterating the email fields and retrieving');
                        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 && PostCopyValidateUtil.validateEmail(String.valueOf(obj.get(strField)))){ //Regex to compare value if email or not
                                obj.put(strField, obj.get(strField) +'.invalid');                                
                            }
                        }
                        try{
                            //update listRecords;
                        }
                        Catch(DmlException exp){
                            System.debug('DML operation has failed');
                        } 
                        
                        
                        
                        //}
                    }
                }
                
                /*BatchJobProcess_CustomMetadata custMetadataJob = new BatchJobProcess_CustomMetadata();
                 Database.executeBatch(custMetadataJob,20);*/
            }
        }
    }
}
Santanu Roy 18Santanu Roy 18
Please help if possible ... This is working code just need to convert it to a batch class .. 
Santanu Roy 18Santanu Roy 18
Please find email utility also 

public class PostCopyValidateUtil {
    public static Boolean validateEmail(String email) {
        Boolean res = true;
        if(!Pattern.matches('([a-zA-Z0-9_\\-\\.]+)@((\\[a-z]{1,3}\\.[a-z]{1,3}\\.[a-z]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})', email)){
            res = false;            
        }
        //System.debug('Validate email '+ res);
        return res;    
    }
}
krishan kumar yadavkrishan kumar yadav
here is updated code with Batch class structure:-
public class Post_Copy_CustomSettings_Email {
    //TODO handle encodingType for SPARK AGN_SPARK_USER_Settings custom settings
    //interface method to collect the batches of records or objects to be passed to execute method
    List <String> listFields = new List <String>();
    String emailUTFEncoding = 'UTF-8';
    public Database.QueryLocator start(Database.BatchableContext bc){ 
        return Database.getQueryLocator(UpdateCustomSettingsEmails());
    }
    
    //interface method to process each batch of records
    public void execute(Database.BatchableContext bc, List<sObject> scope){
        if (scope.size() > 0 ) {
            for (sObject obj : scope ) {
                for (String strField : listFields ) {
                    //Regex to compare value if email or not
                    if (obj.get(strField) != null && PostCopyValidateUtil.validateEmail(String.valueOf(obj.get(strField)))){
                        obj.put(strField, obj.get(strField) +'.invalid');                                
                    }
                }
            }
        try{
            update scope;
            
        }Catch(DmlException exp){
            System.debug('DML operation has failed');
        }  
   }     
}
    
    //interface method to execute any post-processing operations
    public void finish(Database.BatchableContext bc){
        
    }

    public String UpdateCustomSettingsEmails(){
        String query;
        Map < String, Schema.SObjectType > gd = Schema.getGlobalDescribe();        
        for ( String objectName : gd.keySet() ) {            
            Schema.SObjectType result = gd.get( objectName );            
            if ( result.getDescribe().isCustomSetting() ) {                
                query = 'SELECT ';
                Map <String, Schema.SObjectField> objectFields = result.getDescribe().fields.getMap();
                for (String s : objectFields.keySet()) {                    
                    if (String.valueOf( objectFields.get(s).getDescribe().getType()) == 'Email' || String.valueOf(objectFields.get(s).getDescribe().getType()) == 'String'){ 
                        //&& Integer.valueOf(objectFields.get(s).getDescribe().getLength()) > 100)) {                         
                        //System.debug( 'Iterating the email fields and retrieving');
                        query += s + ',';
                        listFields.add(s);                            
                    }
                }
                if (listFields.size() > 0 ) {
                    query = query.removeEnd( ',' );
                    query += ' FROM ' + objectName;                   
                }  
            }
        }
        Return query;
    }
}