Created
August 6, 2021 17:27
-
-
Save rygramer/18b5014d955e84d14e5353b30e1551a3 to your computer and use it in GitHub Desktop.
An Account's stack maturity is determined by the bucket in which their email service provider (ESP) is in.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @Name: ESP | |
* @Author: Kicksaw | |
* @Date: 2021-08-02 | |
* @Description: An Account's stack maturity is determined by the bucket in which their email service provider (ESP) is in. | |
* This class controls the Batchable, Schedulable, and Utility logic surrounding the automation. | |
* | |
********************************************************* CHANGE HISTORY ********************************************************* | |
* ModifiedBy Date Reference Num Description | |
* -------------------------------------------------------------------------------------------------------------------------------- | |
* Kicksaw (Ryan Mercer) 2021-08-02 1 Initial Version | |
**********************************************************************************************************************************/ | |
public class ESP implements Database.Batchable<sObject>, Schedulable { | |
public static ESP.FieldsToCheck fields = ESP.queryHelper(); | |
/** | |
* @Description Schedulable execute method | |
* @Param context - SchedulableContext | |
* @Return void | |
*/ | |
public void execute(SchedulableContext context) { | |
Database.executeBatch(this); | |
} | |
/** | |
* @Description Batchable start method | |
* @Param context - Database.BatchableContext | |
* @Return Database.QueryLocator - Batchable Class Query Locator | |
*/ | |
public Database.QueryLocator start(Database.BatchableContext context) { | |
return Database.getQueryLocator(fields.Query); | |
} | |
/** | |
* @Description Batchable execute method | |
* @Param context - Database.BatchableContext | |
* @Param scope - List<sObject> | |
* @Return void | |
*/ | |
public void execute(Database.BatchableContext context, List<sObject> scope) { | |
ESP.bucket(scope,fields); | |
} | |
/** | |
* @Description Batchable finish method | |
* @Param context - Database.BatchableContext | |
* @Return void | |
*/ | |
public void finish(Database.BatchableContext context) {} | |
/** | |
* @Description Utility method to establish the Account's email service provide bucket. | |
* @Param accts - List<Account> | |
* @Param fields - ESP.FieldsToCheck fields | |
* @Return void | |
*/ | |
public static void bucket(List<Account> accts, ESP.FieldsToCheck fields){ | |
// Establish Map where key = ESP; value = Bucket as defined by ESP_to_Bucket__mdt. | |
Map<String,String> mapESPtoBucket = new Map<String,String>(); | |
for(ESP_to_Bucket__mdt ESPtoBucket : [SELECT ESP__c, Bucket__c | |
FROM ESP_to_Bucket__mdt]){ | |
mapESPtoBucket.put(ESPtoBucket.ESP__c,ESPtoBucket.Bucket__c); | |
} | |
List<Account> acctsToUpdate = new List<Account>(); | |
// Loop through the input Accounts. | |
for(Account acct : accts){ | |
List<String> ESPs = new List<String>(); | |
// Loop through the input ESP.FieldsToCheck. | |
for(String field : fields.ListofFields){ | |
String value = (String)acct.get(field); | |
if(value <> NULL){ | |
// After splitting the value of the field by ';', add the ESPs to the list. | |
ESPs.addall(value.split(';')); | |
} | |
} | |
// Create a list of Buckets and create a default 'Unknown' record. | |
List<Bucket> buckets = new List<Bucket>{new Bucket('Unknown')}; | |
// Loop through the ESPs | |
for(String ESP : ESPs){ | |
// Create Bucket records for each ESP and add them to the list. | |
buckets.add(new Bucket(mapESPtoBucket.get(ESP))); | |
} | |
// Bucket implements Comparable so that they can be sorted. | |
buckets.sort(); | |
// IF condition to check to make sure the Account even needs to be updated. | |
if(acct.StackMaturity__c <> buckets[0].Bucket){ | |
Account acctToUpdate = new Account( | |
Id = acct.Id, | |
// The Bucket record at the [0] index will always be the value of highest precedent. | |
StackMaturity__c = buckets[0].Bucket | |
); | |
acctsToUpdate.add(acctToUpdate); | |
} | |
} | |
// If there are accounts to update, update them. | |
if(!acctsToUpdate.isEmpty()){ | |
update acctsToUpdate; | |
} | |
} | |
/** | |
* @Description Helper method to establish the Account's email service provide bucket. | |
* @Return FieldstoCheck | |
*/ | |
public static FieldsToCheck queryHelper(){ | |
String fieldsToQuery = ''; | |
List<String> listOfFields = new List<String>(); | |
// Loop through ESP_Field_to_Check__mdt to grab all of the ESP fields we need to check on Account. | |
for(ESP_Field_to_Check__mdt field : [SELECT FieldtoCheck__r.QualifiedApiName | |
FROM ESP_Field_to_Check__mdt]){ | |
// Build the 'SELECT' part of the query string. | |
fieldsToQuery += field.FieldtoCheck__r.QualifiedApiName +','; | |
// Add the field's api name to the list. | |
listOfFields.add(string.valueof(field.FieldtoCheck__r.QualifiedApiName)); | |
} | |
// Build the rest of the query string. | |
String query = 'SELECT '+ fieldsToQuery +' Id, StackMaturity__c FROM Account'; | |
// Build the FieldsToCheck object to warehouse the data and return it for additional processing. | |
FieldsToCheck returnFieldsToCheck = new FieldsToCheck(); | |
returnFieldsToCheck.Query = query; | |
returnFieldsToCheck.ListofFields = listOfFields; | |
return returnFieldsToCheck; | |
} | |
/** | |
* @Description Apex object to warehouse the Bucket details and aid in sorting. | |
*/ | |
private class Bucket implements Comparable{ | |
public String Bucket; | |
private Integer Order; | |
public Bucket(String Bucket){ | |
this.Bucket = Bucket; | |
this.Order = 4; | |
if(Bucket == 'Modern') this.Order = 1; | |
if(Bucket == 'Legacy') this.Order = 2; | |
if(Bucket == 'Basic') this.Order = 3; | |
} | |
public Integer compareTo(Object compareTo){ | |
Bucket compareToBucket = (Bucket)compareTo; | |
if(Order == compareToBucket.Order) return 0; | |
if(Order > compareToBucket.Order) return 1; | |
return -1; | |
} | |
} | |
/** | |
* @Description Apex object to warehouse FieldsToCheck details. | |
*/ | |
public class FieldsToCheck{ | |
public String Query; | |
public List<String> ListofFields; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment