Skip to content

Instantly share code, notes, and snippets.

@LokeshSagi
Last active March 28, 2020 13:21
Show Gist options
  • Save LokeshSagi/65d5c756d6c8aa8a04cd74e1333512ee to your computer and use it in GitHub Desktop.
Save LokeshSagi/65d5c756d6c8aa8a04cd74e1333512ee to your computer and use it in GitHub Desktop.
public class ServiceClass {
public void sampleMethod(String accountIdParent) {
// Define the Unit of Work instance.
// Mention the objects in independent to dependent order.
fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(new List<SObjectType> { Account.SObjectType,
Contact.SObjectType,
AccountContactRelation.SObjectType});
// Selector class
AccountContactRelationSelector acsSelector = new AccountContactRelationSelector();
AccountSelector accountSelect = new AccountSelector();
List<Account> childSites = accountSelect.selectLocationsByParent(new Set<Id> {accountIdParent});
for( Account acc : childSites) {
// process records or call Domain class.
.....
// Register the record for DML.
uow.registerNew(acc);
}
// To create new records
uow.registerNew(record);
uow.registerNew(records);
// To create new records + lookup fields
// uow.registerNew(SObject record, Schema.sObjectField lookupFieldAPIName, SObject relatedToParentRecord)
uow.registerNew(accountContactRelation, AccountContactRelation.AccountId, accountRecord);
// To create relationships additionally
// uow.registerRelationship(SObject record, Schema.sObjectField relatedToField, SObject relatedTo)
uow.registerRelationship(accountContactRelation, AccountContactRelation.ContactId, contactRecord);
// To update existing records
uow.registerDirty(record);
uow.registerDirty(records);
// To delete the records
uow.registerDeleted(records);
// Call the commitWork() to make the DML operation. Unless calling this method, DML wont happen.
// Make sure that this uow.commitWork() only once in the transaction.
uow.commitWork();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment