SObject To SObjects - NikiforovAll/SF-Mapper GitHub Wiki

Example

@isTest static void test() {
    //Arrange
    SObjectMapper.initialize(
        new MapperConfigBuilder('Lead')
            .addObjectMapping('Account', new List<String>{'Phone', 'Company'})
            .addObjectMapping('Contact', new List<String>{'Email', 'City'})
            .addFieldMapping('Company', 'Name')
            .addFieldMapping('City', 'MailingCity')
    );
    Lead lead = new Lead(
        Email = '[email protected]',
        City = 'test address',
        Phone = '123-123-123',
        Company = 'Test Company'
    );
    //Act
    Map<String, sObject> result = SObjectMapper.mapObject(lead);
    Account account = (Account)result.get('Account');
    Contact contact = (Contact)result.get('Contact');
    //Assert
    system.assertEquals(lead.Company, account.Name);
    system.assertEquals(lead.Phone, account.Phone);
    system.assertEquals(lead.City, contact.MailingCity);
    system.assertEquals(lead.Email, contact.Email);
    
}
⚠️ **GitHub.com Fallback** ⚠️