|
- /**
- * @description : Invocable for doing large batch DML from Flow and specifies a batch. Use the MassTransactorActionTest class to test this
- * @author : Matt Comer, CapTech Consulting
- * @group :
- * @last modified on :
- * @last modified by :
- * Modifications Log
- * Ver Date Author Modification
- * 1.0 08-19-2020 Matt Comer Initial Version
- * 1.1 08-20-2020 Adan White Added email inputs / send on complete
- **/
- public with sharing class MassTransactorBatchHelper implements Database.Batchable<SObject>, Database.Stateful {
-
- public enum OperationType {DO_INSERT, DO_UPDATE, DO_UPSERT, DO_DELETE}
- private List<SObject> records;
- private OperationType action;
- private String emailTo;
- private String subject;
- private String body;
-
- public class ListIterator implements Iterator<SObject> {
- private List<SObject> records;
- Integer index;
- public ListIterator(List<SObject> records) {
- this.records = records;
- index = 0;
- }
- public boolean hasNext() {
- return (index < records.size());
- }
- public SObject next() {
- if (index == records.size()) {return null;}
- index++;
- return records[index - 1];
- }
- }
-
- public class ListIterable implements Iterable<SObject> {
- private List<SObject> records;
- public ListIterable(List<SObject> records) {
- this.records = records;
- }
- public Iterator<SObject> Iterator(){
- return new ListIterator(records);
- }
- }
-
- public MassTransactorBatchHelper(List<SObject> records, OperationType action) {
-
- this.records = records;
- this.action = action;
- }
-
- public MassTransactorBatchHelper(List<SObject> records, String actionIn, String emailTo, String subj, String bod) {
- this.records = records;
- String a = actionIn.toUpperCase();
- this.emailTo = emailto;
- this.subject = subj;
- this.body = bod;
-
- if (a == 'CREATE') {
- this.action = OperationType.DO_INSERT;
- } else if (a == 'UPDATE') {
- this.action = OperationType.DO_UPDATE;
- } else if (a == 'UPSERT') {
- this.action = OperationType.DO_UPSERT;
- } else if (a == 'DELETE') {
- this.action = OperationType.DO_DELETE;
- }
- }
-
- public System.Iterable<SObject> start(Database.BatchableContext ctx) {
- return new ListIterable(records);
- }
-
- public void execute(Database.BatchableContext ctx, List<SObject> records) {
- // fix problem with picklist field values which can occur in async context (seems to be due to serialization / storage of state)
- if (action != OperationType.DO_DELETE) {
- Map<String, Schema.SObjectField> sObjectFieldsMap = Schema.describeSObjects(new List<String> { records[0].getSObjectType().getDescribe().getName() })[0].fields.getMap();
- for (SObject o: records) {
- for (String fieldName : o.getPopulatedFieldsAsMap().keySet()) {
- Schema.DisplayType dt = sObjectFieldsMap.get(fieldName).getDescribe().getType();
- if ((dt == Schema.DisplayType.PICKLIST || dt == Schema.DisplayType.MULTIPICKLIST) &&
- String.valueOf(o.get(fieldName)) != null ) {
- o.put(fieldName, String.valueOf(o.get(fieldName)).replace('[', '').replace(']', ''));
- }
- }
- }
- }
-
- switch on action {
- when DO_INSERT {
- Database.insert(records, false);
- }
- when DO_UPDATE {
- Database.update(records, false);
- }
- when DO_UPSERT {
- Database.upsert(records, false);
- }
- when DO_DELETE {
- Database.delete(records, false);
- }
- }
- }
-
- public void finish(Database.BatchableContext ctx) {
- If(this.emailTo != null && this.subject != null && this.body != null) {
- Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
- String[] toAddresses = new List<String>();
- toAddresses.add(this.emailTo);
- mail.setToAddresses(toAddresses);
- mail.setSubject(this.subject);
- mail.setPlainTextBody(this.body);
-
- Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
- }
- }
- }
|