wrapper conversion apex


public class WrapperConversion {
    public static List<classB.wrapper> convert(List<classA.wrapper> classAList) {
        List<classB.wrapper> classBList = new List<classB.wrapper>();
        for (classA.wrapper aWrapper : classAList) {
            // Map fields from classA.wrapper to classB.wrapper
            classB.wrapper bWrapper = new classB.wrapper(aWrapper.field1, aWrapper.field2);
            classBList.add(bWrapper);
        }
        return classBList;
    }
}

// Usage example
// Create a list of classA.wrapper objects
List<classA.wrapper> classAList = new List<classA.wrapper>{
    new classA.wrapper('value1', 123),
    new classA.wrapper('value2', 456)
};

// Convert to classB.wrapper list
List<classB.wrapper> classBList = WrapperConversion.convert(classAList);

// Output the converted list
for (classB.wrapper bWrapper : classBList) {
    System.debug('classB.wrapper: ' + bWrapper.newField1 + ', ' + bWrapper.newField2);
}
Test class

@IsTest
private class UpsertUnitOfWorkHelperTest {
    @TestSetup
    static void setupTestData() {
        // Insert sample records for testing
        ObjectA__c objA = new ObjectA__c(Name = 'Test A', CustomField__c = 'Valid Value');
        ObjectB__c objB = new ObjectB__c(Name = 'Test B', LookupField__c = objA.Id);
        insert objA;
        insert objB;
    }

    @IsTest
    static void testSuccessfulUpsert() {
        // Arrange
        ObjectA__c objA = new ObjectA__c(Name = 'Updated A');
        ObjectB__c objB = new ObjectB__c(Name = 'Updated B');

        UpsertUnitOfWorkHelper upsertHelper = new UpsertUnitOfWorkHelper();
        upsertHelper.registerSObjectUpsert(objA);
        upsertHelper.registerSObjectUpsert(objB);

        // Act
        Test.startTest();
        upsertHelper.doWork();
        Test.stopTest();

        // Assert
        System.assertNotNull(upsertHelper.Results, 'Results should not be null');
        for (Database.UpsertResult result : upsertHelper.Results) {
            System.assert(result.isSuccess(), 'Upsert operation should succeed');
        }
    }

    @IsTest
    static void testPartialSuccess() {
        // Arrange
        ObjectA__c objA = new ObjectA__c(Name = 'Valid A');
        ObjectB__c objB = new ObjectB__c(Name = ''); // Invalid due to missing required field

        UpsertUnitOfWorkHelper upsertHelper = new UpsertUnitOfWorkHelper();
        upsertHelper.registerSObjectUpsert(objA);
        upsertHelper.registerSObjectUpsert(objB);

        // Act
        Test.startTest();
        upsertHelper.doWork();
        Test.stopTest();

        // Assert
        System.assertNotNull(upsertHelper.Results, 'Results should not be null');
        Boolean hasError = false;
        for (Database.UpsertResult result : upsertHelper.Results) {
            if (!result.isSuccess()) {
                hasError = true;
            }
        }
        System.assert(hasError, 'At least one record should have an error');
    }

    @IsTest
    static void testErrorDetails() {
        // Arrange
        ObjectA__c objA = new ObjectA__c(Name = null); // Invalid due to missing required field
        UpsertUnitOfWorkHelper upsertHelper = new UpsertUnitOfWorkHelper();
        upsertHelper.registerSObjectUpsert(objA);

        // Act
        Test.startTest();
        upsertHelper.doWork();
        Test.stopTest();

        // Assert
        List<String> errors = upsertHelper.getErrorDetails();
        System.assertNotEquals(0, errors.size(), 'Error details should not be empty');
        System.assert(errors[0].contains('Error in record'), 'Error message should describe the issue');
    }

    @IsTest
    static void testNoRecordsRegistered() {
        // Arrange
        UpsertUnitOfWorkHelper upsertHelper = new UpsertUnitOfWorkHelper();

        // Act
        Test.startTest();
        upsertHelper.doWork();
        Test.stopTest();

        // Assert
        System.assertEquals(0, upsertHelper.Results.size(), 'Results should be empty when no records are registered');
    }
}
wrapper conversion apex wrapper conversion apex Reviewed by dasfrogpractice on 02:27 Rating: 5

No comments:

Theme images by mariusFM77. Powered by Blogger.
Youtube Channel Image
Dasfrog Subscribe To watch more Salesforce Training
Subscribe