Last active
April 21, 2020 16:36
-
-
Save justin-lyon/26a06979609fcb915d44eb8918a23095 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public class CSVBatch implements Database.Batchable<PolicyMaster__c>, Database.AllowsCallouts, Database.Stateful { | |
private CSVState state; | |
private Integer scopeSize; | |
public CSVBatch (CSVState state) { | |
this.state = state; | |
} | |
public Database.QueryLocator start (Database.BatchableContext ctx) { | |
return Database.getQueryLocator([ | |
SELECT Id | |
FROM PolicyMaster__c | |
WHERE PolicyNumber__c IN :state.getPolicyNumbers()]); | |
} | |
public void execute (Database.BatchableContext ctx, List<PolicyMaster__c> scope) { | |
// for each record in scope | |
// add state.policies.get(scope.PolicyNumber__c) to scope | |
// upsert scope | |
// add errors to state.errors | |
} | |
public void finish (Database.BatchableContext ctx) { | |
if (state.hasNext()) { | |
System.enqueueJob(new CSVQueueable(state)); | |
} else { | |
// insert errors | |
// send email to admins | |
} | |
} | |
} |
This file contains hidden or 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
public class CSVQueueable implements Queueable, Database.AllowsCallouts { | |
private CSVState state; | |
private Integer csvChunkSize; | |
public CSVQueueable (CSVState state) { | |
this(state, state.getChunkSize()); | |
} | |
public CSVQueueable (Blob csvData, Integer chunkSize) { | |
this.state = new CSVState(csvData, chunkSize); | |
this.csvChunkSize = chunkSize; | |
} | |
public CSVQueueable(CSVState state, Integer chunkSize) { | |
this.state = state; | |
this.csvChunkSize = chunkSize; | |
} | |
public void execute (QueueableContext ctx) { | |
state.next(); | |
Database.executeBatch(new CSVBatch(state)); | |
} | |
} |
This file contains hidden or 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
public with sharing class CSVState { | |
// Policies currently in processing | |
private Map<String, PolicyMaster__c> policies; | |
private List<CSVRow> rows; | |
private Integer chunkSize; | |
// All Policy Numbers processed | |
private Set<String> processedPolicyNumbers; | |
// Any errors encountered during batch execution | |
private List<Error> errors; | |
public CSVState(Blob csvData, Integer size) { | |
this.errors = new List<Error>(); | |
this.chunkSize = size; | |
parseCSV(csvData); | |
} | |
public Integer getChunkSize() { | |
return this.chunkSize; | |
} | |
public void next() { | |
// Get the Next [chunkSize] Unique Policy Numbers from this.rows | |
// Replay CSVRows in chronological order into PolicyMaster__c records. | |
// this.policies = chronological, aggregated PolicyMaster__c changes in this.rows for [chunksize] | |
} | |
public Boolean hasNext() { | |
// has more policyNumbers to process | |
} | |
public Set<String> getPolicyNumbers() { | |
// All Policy Numbers in current chunk | |
return this.policies.keySet(); | |
} | |
public void addError (String policyNumber, Exception exc) { | |
// add error to this.errors | |
} | |
private void parseCSV(Blob csvData) { | |
// parse csv data into List<CSVRow> | |
this.rows = new List<CSVRow>(); | |
} | |
// Parse CSV Blob Data to List of Rows | |
public class CSVRow { | |
public String PolicyNumber; | |
} | |
public class Error { | |
public String policyNumber; | |
public String errorMessage; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment