Getting Started - NikiforovAll/SF-Mapper GitHub Wiki

Getting Started Guide

What is SF Mapper?

SF Mapper is an object-SObject mapper. Object-SObject mapping works by transforming an source object of one type into an output Map of SObjects.

Why use SF Mapper?

Mapping code is boring. Testing mapping code is even more boring. SF Mapper provides simple configuration of mapping source object (which is generally abstraction of any data source, it could be another SObject or JSON) to map of SObjects.

How do I use SF Mapper?

First, you need a source type to work with. As source you can use any class that implements IResourceProvide, so it can be sobject, json etc. For example if you want to split SObject into SObjects you just need to wrap SObject in SObjectResourceProvider

new SObjectResourceProvider(new Account(Name = 'Acc'));

To specify mapping rules you need to provide instance of class that implements IFieldMapping.

public interface IFieldMapping {
	String getDefaultMappingObject();
	List<FieldMappingRule> getMappingRules();
	Set<String> getObjectNames();
}

The advantage of IFieldMapping interface is that you can provide own implementation. For example dynamically fetch field mapping from custom settings etc.

SObjectMapper.initialize('mappingName',
    //FieldMapping instance or your custom implementation
    new IFieldMapping()
);

Also, instead of creating new map of SObject, you can specify SObjects to write to:

Lead lead = new Lead(
    Email = '[email protected]',
    City = 'test address',
    Phone = '123-123-123',
    Company = 'Test Company'
);
Account account = new Account();
Contact contact = new Contact();
Map<String, sObject> sObjectsToUpdateMap = new Map<String, sObject> {
    'Account' => account,
    'Contact' => contact
};

SObjectMapper.mapObject(lead, sObjectsToUpdateMap);

See examples for SObject-toSObjects mapping: example

⚠️ **GitHub.com Fallback** ⚠️