Getting started with Mapper.apex
Common Mapping
We can do simple mappings like this:
Account acc = ...;
Mapper m = new Mapper()
.mapField('name', 'Name')
.mapField('description', 'Description')
.toObject(AccountDTO.class);
AccountDTO dto = (AccountDTO)m.run(acc);
// Create AccountDTO with fields 'name' and 'description'
This piece of code converts an SObject(Account) with 'Name' to a custom class AccountDTO with 'name'.
The value of 'Name' from Account is simply copied to 'name' in AccountDTO.
And we can easily reverse the mapping.
Mapper reversed = m.reverseToSObject(Account.class);
Account acc = (Account)reversed.run(dto);
// Map the AccountDTO back to Account
The pair of mappers help us to convert between two objects.