Utilities to make working with SObjects and DML easier in Apex.
-
This package requires my apex-core-utils.
The
StringBuilder
andDateUtil
.
-
Easily work with the compound address field.
-
Generates a templated error message when a CRUD exception occurs.
-
An abstract class which is extended by both
CrudException
andFlsException
, just to make catching easier. -
Used to easily find duplicates within the org based on the org's defined duplicate rules.
-
Easily work with field sets.
-
Generates a templated error message when a FLS exception occurs.
-
An interface used to specify a DML implementation within the
SObjectUnitOfWork
. -
OptionDml
-
SecureDml
-
SimpleDml
-
Adapted from this amazing code.
Simplifies rolling up child records in a lookup relationship.
-
Easily work with picklists.
-
Easily work with record types.
-
Allows easy creation of SObjects, auto generating required fields and relationships when needed.
You can extend this class to make your own custom factories, or use the built-in "Generic" factory.
-
Match SObjects using fields of your choice.
-
Adapted from this amazing code.
Used to query the database via SOQL.
You can extend this class to make your own custom selectors, or use the built-in "Generic" selector.
TODO: Would like to create a "QueryBuilder" instead
-
Adapted from this amazing code.
Used to gather all dml work and commits it in a single place.
-
A utility class that has many helper methods to make working with SObjects and their fields easier.
-
An Aura component to make accessing record data easier.
AddressUtil
Lead lead1 = new Lead();
lead1.Street = '123 Main St.';
lead1.City = 'City';
lead1.State = 'NY';
lead1.Country = 'US';
Lead lead2 = new Lead();
AddressUtil.copy(lead1, lead2);
System.assertEquals(lead2, lead1.Street);
System.assertEquals(lead2, lead1.City);
System.assertEquals(lead2, lead1.State);
System.assertEquals(lead2, lead1.Country);
CrudException
Account acc = new Account();
CrudException e = new CrudException(DatabaseOperation.READ, acc);
DuplicateFinder
Account acc = new Account();
acc.Name = 'Duplicate';
DuplicateFinder finder = new DuplicateFinder();
finder.find(acc);
List<SObject> duplicates = finder.getRecords();
FieldSetUtil
List<String> fields = FieldSetUtil.getFields('Account', 'My_Field_Set');
List<Schema.DescribeFieldResult> describes = FieldSetUtil.getDescribed('Account', 'My_Field_Set');
FlsException
Account acc = new Account();
FlsException e = new FlsException(DatabaseOperation.READ, acc, Account.Name);
LREngine
List<Opportunity> records = Trigger.new;
LREngine.Context ctx = new LREngine.Context(
Account.SObjectType,
Opportunity.SObjectType,
Opportunity.AccountId
);
ctx.add(new LREngine.RollupSummaryField(
Account.AnnualRevenue,
Opportunity.Amount,
LREngine.RollupOperation.Sum
));
List<SObject> masters = LREngine.rollUp(ctx, records);
PicklistUtil
List<String> labels = PicklistUtil.getLabels('Account', 'Type');
List<String> values = PicklistUtil.getValues('Account', 'Type');
// get state/country codes if its enabled in your org
List<String> stateCodes = PicklistUtil.getStateValues();
RecordTypeUtil
Id result = RecordTypeUtil.getId('Account', 'My_Record_Type');
String name = RecordTypeUtil.getName('Account', 'therecordtypeid');
SObjectFactory
SObjectFactory factory = new SObjectFactory.Generic(Account.SObjectType, 3);
List<SObject> records = factory.build();
System.assertEquals(3, records.size());
SObjectMatcher
Map<Schema.SObjectField, Object> valueByField = new Map<Schema.SObjectField, Object>{
Account.Name => 'Test',
Account.Email__c => 'test@test.com',
Account.Phone => '123-456-7890',
Account.CreatedDate => Date.today()
};
SObjectMatcher matcher = new SObjectMatcher(Account.SObjectType, valueByField);
matcher.find();
SObject result = matcher.getRecord();
SObjectSelector
SObjectSelector selector = new SObjectSelector.Generic(Account.SObjectType);
// select all records with all fields
List<SObject> results = selector.selectAll();
SObjectUnitOfWork
List<SObjectType> types = new List<SObjectType>{Account.SObjectType};
SObjectUnitOfWork uow = new SObjectUnitOfWork(types);
for (Integer i = 0; i < 100; i++) {
Account acc = new Account();
acc.Name = 'Test' + i;
uow.registerNew(acc);
}
uow.commitWork();
SObjectUtil
Contact cont = [SELECT Id, Account.Name FROM Contact LIMIT 1];
String sObjectName = SObjectUtil.convertIdToName(cont.Id);
Schema.SObjectType type = SObjectUtil.convertNameToType('Contact');
Object value = SObjectUtil.getFieldValue(cont, 'Account.Name');
// ...and many more
RecordData
<!-- YourComponent.cmp -->
<aura:handler
name="recordLoaded"
event="c:RecordDataLoaded"
action="{!c.recordLoaded}" />
<c:RecordData aura:id="recordData"
isLoading="{!v.isLoading}"
layoutType="FULL"
mode="EDIT"
recordId="{#v.recordId}"
targetFields="{!v.record}" />
Current test results:
Class | Percent | Lines |
---|---|---|
AddressUtil | 68% | 92/135 |
DuplicateFinder | 93% | 67/72 |
FieldSetUtil | 62% | 22/35 |
IconUtil | 92% | 77/83 |
LREngine | 92% | 204/221 |
PicklistUtil | 80% | 40/50 |
RecordTypeUtil | 88% | 46/52 |
SObjectFactory | 74% | 140/188 |
SObjectMatcher | 85% | 97/113 |
SObjectSelector | 84% | 83/98 |
SObjectUnitOfWork | 78% | 149/189 |
SObjectUtil | 92% | 244/263 |
UserUtil | 100% | 20/20 |