Implementation

Estimated reading: 5 minutes 4 views

Initializing Unit of Work

In order to use the Unit of Work manager you must instantiate it by simple creating an instance of the UnitOfWork class.

UnitOfWork uow = new UnitOfWork();

Apart from initializing the UnitOfWork you must explicitely set which SObjectTypes you are going to handle in the transaction. This can be done in two different ways.

// At initialization time
UnitOfWork uow = new UnitOfWork(listOfSObjectTypes);

// Using register Object method.
UnitOfWork uow = new UnitOfWork();
uow.registerObject(Account.getSObjectType());

Adding Operations to the UoW

To add operations to the queue, you only need to call the different available add methods: addInsert, addUpdate, addUpsert or addDelete

Although there are more specific methods that will be described later, the basic ones are the following:

    addInsert(SObject record)
    addUpsert(SObject record)
    addUpdate(SObject record)
    addDelete(SObject record)

All of them receive the SObject to be added to the UoW.

If the SObject passed to an addInsert method has an Id set, the manager will return an error.

Methods

void registerObject(SObjectType objectType)

Register the specified SObjectType in the UoW. Once registered, the UoW will accept records of that SObjectType.

  • objectType: the SObjectType to be registered in the UoW.

void registerObject(List objectTypes)

Register the specified SObjectTypes in the UoW. Once registered, the UoW will accept records of these SObjectType.

  • objectType: the SObjectType to be registered in the UoW.

void addInsert(SObject record)

Add the specific record to the Insert queue.

  • record: the record to be added to the queue.

void addInsert(SObject record, integer level)

Add the specified record at the desired level.

  • record: the record to be added to the queue.
  • level: the queue level to be set.

The level is used when you have relationships between objects of the same type that has relationships between them.

For instance, if we have an Account and a Parent Account, we would need to specify different levels.

    uow.addInsert(parentAccount, 0);
    uow.addInsert(childAccount, 1);

The lower the level, the earlier it will be processed.

void addInsert(SObject record, integer level, Schema.SObjectField relatedToParentField, SObject relatedToParentRecord)

Add the specified record at the desired level with the defined relationship.

  • record: the record to be added to the queue.
  • level: the queue level to be set.
  • relatedToParentField: the SObjectField which reference the LookUp field in the child record.
  • relatedToParentRecord: the parent record.

The level is used when you have relationships between objects of the same type that has relationships between them.

For instance, if we have an Account and a Parent Account, we would need to specify different levels.

    uow.addInsert(parentAccount, 0);
    uow.addInsert(childAccount, 1);

The lower the level, the earlier it will be processed.

void addInsert(SObject record, Schema.SObjectField relatedToParentField, SObject relatedToParentRecord)

Adds the record to the insert queue with the specified relationship.

  • relatedToParentField: the SObjectField which reference the LookUp field in the child record.
  • relatedToParentRecord: the parent record.

void addInsert(List records)

Adds a list of records to the insert queue.

  • records: the records to be added to the queue.
    The list can have different SObjectType records as far as they are registered in the UoW.

The records will be added at level 0.

void addInsert(List records, integer level)

Adds a list of records to the insert queue at the specified level.

  • records: the records to be added to the queue.
  • level: the queue level to be set.

The list can have different SObjectType records as far as they are registered in the UoW.

void addInsert(List records, integer level, Schema.SObjectField relatedToParentField, SObject relatedToParentRecord)

Adds a list of records to the insert queue, at the specified level and relationship.

  • records: the record to be added to the queue.
  • level: the queue level to be set.
  • relatedToParentField: the SObjectField which reference the LookUp field in the child record.
  • relatedToParentRecord: the parent record.

void addDelete(SObject record)

Adds the specified record to the Delete queue.

  • record: the record to delete.

void addUpdate(SObject record)

Add the specified record to the update queue.

  • record: the record to update.

void addUpsert(SObject record)

Add the specified record to the upsert queue.

  • record: the record to update.

void registerRelationship(SObject record, Schema.SObjectField relatedToField, SObject relatedTo, integer Level)

Add the specified relationship to the UoW at the specified level.

  • record: the child record.
  • relatedToField: the Lookup Field on the child record.
  • relatedTo: the parent record.
  • Level: the level in the queue.

void registerRelationship(SObject record, Schema.SObjectField relatedToField, Schema.SObjectField externalIdField, Object externalId, integer level)

Add the specified relationship to the UoW at the specified level using an external id as reference instead of the parent record Id.

  • record: the child record.
  • relatedToField: the Lookup Field on the child record.
  • externalIdField: the external id field in the parent object
  • externalId: the external id value.
  • Level: the level in the queue.

The parent SObjectType is retrieved automatically from the externalIdField.

void reset()

Reset all the UoW queues: insert, delete, update and upsert.

boolean commitWork()

Executes the UoW

Leave a Comment

       
Euphoria, forever till the end of times

Euphoria

Share this Doc

Implementation

Or copy link

CONTENTS