// This batch process is used to update Country_Region__c field in fan object // example {Le Cannet,Meyreuil = FR(france) } global class FanCountryCodeFormattingBatchProcess implements database.Batchable, Schedulable, Database.Stateful, Database.RaisesPlatformEvents { public map countryCode = Countries__c.getAll(); public List codes; public map pattern; public String s = ''; public integer j; public string k = 'FanID' + ',' + 'Country Region'; public FanCountryCodeFormattingBatchProcess() { codes = new List(); pattern = new Map(); s = k; // mapping codes from Countries__c object for (Countries__c co : countryCode.values()) { codes.add(co.CountryCode__c); pattern.put(co.Name.touppercase(), co.CountryCode__c); } // mapping pattern name and country code from CountryCodeMatchingPattern__c object for (CountryCodeMatchingPattern__c iso : [ SELECT id, name, Pattern_Name__c, Country_Code__c FROM CountryCodeMatchingPattern__c ]) { pattern.put(iso.Pattern_Name__c.touppercase(), iso.Country_Code__c); } } global Database.queryLocator start(Database.BatchableContext bc) { return Database.getQueryLocator( [ SELECT id, Email__c, Country_Region__c FROM Fan__c WHERE Country_Region__c NOT IN :codes AND Country_Region__c != NULL ] ); } global void execute(Database.BatchableContext bc, List Scope) { list mismatchedFans = new List(); list mismatched = new List(); list match = new List(); list tosend = new List(); // logic to update countryregion field in fan object for (Fan__c fan : Scope) { if (string.isNotBlank(fan.Country_Region__c)) { if (pattern.containsKey(fan.Country_Region__c.touppercase())) { fan.Country_Region__c = pattern.get(fan.Country_Region__c.touppercase()); match.add(fan.id); } else { mismatchedFans.add( new Mismatched_Country_Code__c( FanID__c = fan.Id, email__c = fan.Email__c, Fan_Country_Region__c = fan.Country_Region__c ) ); tosend.add(fan.Id); } } } try { update Scope; upsert mismatchedFans email__c; } catch (DmlException e) { system.debug('Updation Failed'); } // logic to delele the match fan id from Mismatched_Country_Code__c if (!match.isempty()) { list existingmismatchedFans = [ SELECT id, FanID__c, name, processed__c, email__c FROM Mismatched_Country_Code__c WHERE FanID__c IN :match ]; try { delete existingmismatchedFans; } catch (DmlException e) { } } for (j = 0; j < mismatchedFans.size(); j++) { s += '\n' + mismatchedFans[j].FanID__c + ',' + mismatchedFans[j].Fan_Country_Region__c; } } global void finish(Database.BatchableContext bc) { // logic for email notification with success and mismatched vaules count Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setToAddresses(ConfigUtils.getConfigMultipleStrings(ConfigUtils.FAN_COUNTRY_CODE_RECIPIENTS)); if (s.length() > 20) { Blob b = Blob.valueOf(s); Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment(); attach.setFileName('InvalidCountryCodes.csv'); attach.setBody(b); mail.setSenderDisplayName('no-reply@ISOCodeCleanUp.com'); mail.setSubject('Countrycode CleanUp Job Status - Success'); mail.setHtmlBody( '

' + 'The Countrycode cleanup processed successfully on ' + Date.today().Month() + '/' + Date.today().Day() + '/' + Date.today().Year() + '.' + ' Please find the attached Invalid Country Codes' ); mail.setFileAttachments(new List{ attach }); Messaging.sendEmail(new List{ mail }); } else { mail.setSenderDisplayName('no-reply@ISOCodeCleanUp.com'); mail.setSubject('Countrycode Cleanup Job Status - Success'); mail.setHtmlBody( '

' + 'The Countrycode cleanup processed successfully on ' + Date.today().Month() + '/' + Date.today().Day() + '/' + Date.today().Year() + '.' + ' No Invalid Country Codes found.' ); Messaging.sendEmail(new List{ mail }); } } global void execute(SchedulableContext sc) { FanCountryCodeFormattingBatchProcess format = new FanCountryCodeFormattingBatchProcess(); ID batchprocess = Database.executeBatch(format, 2000); } }