|
| 1 | +/************************************************************************************************* |
| 2 | +* This file is part of the Nebula Framework project, released under the MIT License. * |
| 3 | +* See LICENSE file or go to https://github.com/jongpie/NebulaFramework for full license details. * |
| 4 | +*************************************************************************************************/ |
| 5 | +public abstract class DML extends NebulaCore implements IDML { |
| 6 | + |
| 7 | + private Schema.SObjectType sobjectType; |
| 8 | + |
| 9 | + public DML(Schema.SObjectType sobjectType) { |
| 10 | + this.sobjectType = sobjectType; |
| 11 | + } |
| 12 | + |
| 13 | + public virtual void insertRecords(SObject record) { |
| 14 | + this.insertRecords(new List<SObject>{record}); |
| 15 | + } |
| 16 | + |
| 17 | + public virtual void insertRecords(List<SObject> records) { |
| 18 | + Database.insert(records); |
| 19 | + } |
| 20 | + |
| 21 | + public virtual void updateRecords(SObject record) { |
| 22 | + this.updateRecords(new List<SObject>{record}); |
| 23 | + } |
| 24 | + |
| 25 | + public virtual void updateRecords(List<SObject> records) { |
| 26 | + Database.update(records); |
| 27 | + } |
| 28 | + |
| 29 | + public virtual void upsertRecords(SObject record) { |
| 30 | + this.upsertRecords(this.castRecords(record)); |
| 31 | + } |
| 32 | + |
| 33 | + public virtual void upsertRecords(List<SObject> records) { |
| 34 | + Database.upsert(records); |
| 35 | + } |
| 36 | + |
| 37 | + public virtual void undeleteRecords(SObject record) { |
| 38 | + this.undeleteRecords(new List<SObject>{record}); |
| 39 | + } |
| 40 | + |
| 41 | + public virtual void undeleteRecords(List<SObject> records) { |
| 42 | + Database.undelete(records); |
| 43 | + } |
| 44 | + |
| 45 | + public virtual void deleteRecords(SObject record) { |
| 46 | + this.deleteRecords(new List<SObject>{record}); |
| 47 | + } |
| 48 | + |
| 49 | + public virtual void deleteRecords(List<SObject> records) { |
| 50 | + Database.delete(records); |
| 51 | + } |
| 52 | + |
| 53 | + public virtual void hardDeleteRecords(SObject record) { |
| 54 | + this.hardDeleteRecords(new List<SObject>{record}); |
| 55 | + } |
| 56 | + |
| 57 | + public virtual void hardDeleteRecords(List<SObject> records) { |
| 58 | + this.deleteRecords(records); |
| 59 | + if(!records.isEmpty()) Database.emptyRecycleBin(records); |
| 60 | + } |
| 61 | + |
| 62 | + // Not all objects will have external ID fields, so these methods are protected (instead of public) |
| 63 | + // Any object that needs an upsert by external ID can expose these methods in their repos |
| 64 | + protected virtual void upsertRecords(SObject record, Schema.SObjectField externalIdField) { |
| 65 | + this.upsertRecords(this.castRecords(record), externalIdField); |
| 66 | + } |
| 67 | + |
| 68 | + protected virtual void upsertRecords(List<SObject> records, Schema.SObjectField externalIdField) { |
| 69 | + Database.upsert(records, externalIdField); |
| 70 | + } |
| 71 | + |
| 72 | + private List<SObject> castRecords(SObject record) { |
| 73 | + // Salesforce will only allow upsert calls for SObjects if a declared-type list is passed in. |
| 74 | + // This is fine for the bulk method, where we can assume the caller is passing in an explicit list, but for a single record, |
| 75 | + // the only way to successfully perform the upsert is to dynamically spin up a list of the SObject's type |
| 76 | + |
| 77 | + String listType = 'List<' + this.sobjectType + '>'; |
| 78 | + List<SObject> castRecords = (List<SObject>)Type.forName(listType).newInstance(); |
| 79 | + castRecords.add(record); |
| 80 | + |
| 81 | + return castRecords; |
| 82 | + } |
| 83 | + |
| 84 | +} |
0 commit comments