function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nevin O'Regan 3Nevin O'Regan 3 

Help creating a test class for a trigger.

Hi guys, 

I have built my first trigger with the help of this group. I'm hoping someone can help me out building a test class for it so that I can deploy it into production. The trigger basically maps fields from OppLineItemSchedule to the OppLineItem

trigger MapMontsOnSchedule on OpportunityLineItemSchedule (after insert, before update) {
    
    // UPDATED: Must handle multiple children for a single parent
    // Instead of a single OpportunityLineItem, hold a list of them
    Map<Id,List<OpportunityLineItemSchedule>> MapMonths = new Map<Id, List<OpportunityLineItemSchedule>>();
    
    // Have a temp list ready for looping
    List<OpportunityLineItemSchedule> tempOlisList;
    
    // Now populate the months map
    for(OpportunityLineItemSchedule sch : trigger.new) {
        // Check if the map already has an entry for the parent Id (key)
        if(MapMonths.containsKey(sch.OpportunityLineItemId)) {
            // If it does, then update the list with the new value (so it will not override the previous value)
            tempOlisList = MapMonths.get(sch.OpportunityLineItemId);
            tempOlisList.add(sch);
            MapMonths.put(sch.OpportunityLineItemId, tempOlisList);
        } else {
            // Otherwise, we will create a new entry in the map with a list value of just the current iteration's OLIS
            tempOlisList = new List<OpportunityLineItemSchedule>();
            tempOlisList.add(sch);
            MapMonths.put(sch.OpportunityLineItemId, tempOlisList);
        }
    }

    List<OpportunityLineItem> OppLineItemList = new List<OpportunityLineItem>();
    for(OpportunityLineItem oli:[
        Select id, January__c,February__c, March__c, April__c, May__c, June__c, July__c, August__c, September__c, October__c, November__c,December__c 
        From OpportunityLineItem 
        Where Id IN :MapMonths.Keyset()
    ]) {
        // Then, the following is the updated if statement inside your for loop
        if(MapMonths.containsKey(oli.id)) {
            
            // UPDATE: Because we have a list of children now, we will need to loop through all of them to assign values to each month before moving on to the next Opportunity Line Item
            // Create a for loop to go through the list of children
            for(OpportunityLineItemSchedule olis : MapMonths.get(oli.id)) {
                
                // Create a switch statement to check what value is the Month
                switch on olis.Month__c {
                    // If the opportunity line item schedule's Month field was January
                    when 'January' {
                        // Then assign the opportunity line item's January field to the opportunity line item schedule's quantity
                        oli.January__c = olis.Quantity;
                    }
                    when 'February' {
                        oli.February__c = olis.Quantity;
                    }
                    when 'March' {
                        oli.March__c = olis.Quantity;
                    }
                    when 'Apr' {
                        oli.April__c = olis.Quantity;
                    }
                    when 'May' {
                        oli.May__c = olis.Quantity;
                    }
                    when 'June' {
                        oli.June__c = olis.Quantity;
                    }
                    when 'July' {
                        oli.July__c = olis.Quantity;
                    }
                    when 'August' {
                        oli.August__c = olis.Quantity;
                    }
                    when 'September' {
                        oli.September__c = olis.Quantity;
                    }
                    when 'October' {
                        oli.October__c = olis.Quantity;
                    }
                    when 'November' {
                        oli.November__c = olis.Quantity;
                    }
                    when 'December' {
                        oli.December__c = olis.Quantity;
                    }
                    // If it did not find any matches
                    when else {
                        // EDIT: do not put anything here for now
                        // You can leave this empty or put something in here to handle cases where the Month field is NOT the name of the month
                        // e.g. Month field was for some reason populated with 'Apple'
                    }
                }
            }
            OppLineItemList.add(oli);
        }
    }
    update OppLineItemList;
}
Best Answer chosen by Nevin O'Regan 3
Boss CoffeeBoss Coffee
Try the following.
@isTest
public class MapMontsOnScheduleTest {
	
    // Set up the records we need prior to testing the method
    @testSetup
    static void setupOpportunity() {
        
        // Set up five products
        List<Product2> products = new List<Product2>();
        for(Integer i = 0; i < 5; i++) {
            products.add(new Product2(
            	Name='prod'+i,
                CanUseQuantitySchedule=true
            ));
        }
        insert products;
                
        // Set up standard price book
        Pricebook2 pb = new Pricebook2(
        	Id = Test.getStandardPricebookId(),
            IsActive = true
        );
        update pb;

        // Insert the price list items for the products
        List<PricebookEntry> pbEntries = new List<PricebookEntry>();
        for(Integer i = 0; i < 5; i++) {
            pbEntries.add(new PricebookEntry(
            	Pricebook2Id=pb.Id,
                Product2Id=products[i].Id,
                UnitPrice=500,
                IsActive=true
            ));
        }
        insert pbEntries;
        
        // Set up five opportunities
        List<Opportunity> opportunities = new List<Opportunity>();
        for(Integer i = 0; i < 5; i++) {
            opportunities.add(new Opportunity(
            	Name='opp'+i,
                CloseDate=Date.today(),
                StageName='Closed Won',
                Pricebook2Id=pb.Id
            ));
        }
        insert opportunities;
        
        // Set up opportunity line items
        List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
        for(Integer i = 0; i < opportunities.size(); i++) {
            oliList.add(new OpportunityLineItem(
            	OpportunityId=opportunities[i].Id,
                Product2Id=products[i].Id,
                Quantity=i+1,
                TotalPrice=1500
            ));
        }
        insert oliList;
    }
    
    // Test the method that updates the respective month field in Opportunity Line Item
    @isTest
    static void testMonthUpdate() {
           
        test.startTest();

        // Create the opportunity line item schedules to be inserted    
        List<OpportunityLineItemSchedule> olisList = new List<OpportunityLineItemSchedule>();
        for(OpportunityLineItem oli : [SELECT Id FROM OpportunityLineItem]) {
            for(Integer i = 1; i <= 12; i++) {
                olisList.add(new OpportunityLineItemSchedule(
                    OpportunityLineItemId = oli.Id,
                    Type = 'Quantity',
                    ScheduleDate = Date.newInstance(2019, i, i),
                    Quantity = i
                ));
            }
        }
        insert olisList;
        
        test.stopTest();
        
        // Now, check that the updates were done correctly
        for(OpportunityLineItem newOli : [
            SELECT Id, January__c, February__c, March__c, April__c, May__c, June__c, 
            July__c, August__c, September__c, October__c, November__c, December__c
            FROM OpportunityLineItem
        ]) {
            for(Integer i = 1; i <= 12; i++) {
                System.assertEquals(1, newOli.January__c);
                System.assertEquals(2, newOli.February__c);
                System.assertEquals(3, newOli.March__c);
                System.assertEquals(4, newOli.April__c);
                System.assertEquals(5, newOli.May__c);
                System.assertEquals(6, newOli.June__c);
                System.assertEquals(7, newOli.July__c);
                System.assertEquals(8, newOli.August__c);
                System.assertEquals(9, newOli.September__c);
                System.assertEquals(10, newOli.October__c);
                System.assertEquals(11, newOli.November__c);
                System.assertEquals(12, newOli.December__c);   
            }
        }
        
    }
}