You need to sign in to do that
Don't have an account?
Santanu 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);*/
}
}
}
}
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);*/
}
}
}
}
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;
}
}
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;
}
}