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 

Field is not updating to a blank value if I delete related OpportintyLineItemSchedule record

Hi guys,

I have created a trigger on the OpportunityLineItemSchedule record to update fields in the parent OpportunityLineItem record. However it is not updating the fields in the OpportunityLineItem to blank values if I delete a scedule. Can anyone help me with this?

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
Your trigger doesn't handle deletions, only inserts and updates. To handle each scenario, you'll have to check the trigger context when coming into the class. Ideally you'd separate the logic into a helper class of its own, but try the following.
trigger MapMontsOnSchedule on OpportunityLineItemSchedule (after insert, before update, before delete) {
    
    // This section handles logic if triggered by a delete
    if(Trigger.isDelete) {
        // Create a list of Opportunity Line Items to be updated
        List<Id> oppLineItemIds = new List<Id>();
        
        // Go through the deleted Opportunity Line Item Schedules to see which OLIs to delete
        for(OpportunityLineItemSchedule sch : trigger.old) {
            oppLineItemIds.add(sch.OpportunityLineItemId);
        }
        
        // Now grab the records for the Opportunity Line Items
        List<OpportunityLineItem> oppLineItemUpdates = [
            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,
            (SELECT Id, Month__c FROM OpportunityLineItemSchedules WHERE Id IN :Trigger.oldMap.keySet())
            FROM OpportunityLineItem 
            WHERE Id IN :oppLineItemIds
        ];
        
        // Iterate through each oli record to update the month fields to blank
        for(OpportunityLineItem oli : oppLineItemUpdates) {
            for(OpportunityLineItemSchedule olis : oli.OpportunityLineItemSchedules) {
                switch on olis.Month__c {
                    when 'January' {
                        oli.January__c = null;
                    }
                    when 'February' {
                        oli.February__c = null;
                    }
                    when 'March' {
                        oli.March__c = null;
                    }
                    when 'Apr' {
                        oli.April__c = null;
                    }
                    when 'May' {
                        oli.May__c = null;
                    }
                    when 'June' {
                        oli.June__c = null;
                    }
                    when 'July' {
                        oli.July__c = null;
                    }
                    when 'August' {
                        oli.August__c = null;
                    }
                    when 'September' {
                        oli.September__c = null;
                    }
                    when 'October' {
                        oli.October__c = null;
                    }
                    when 'November' {
                        oli.November__c = null;
                    }
                    when 'December' {
                        oli.December__c = null;
                    }
                    // If it did not find any matches
                    when else {
                        // Do nothing
                    }
                }
            }
        }
        
        // Update the oli list
        update oppLineItemUpdates;
    }

    if(Trigger.isInsert || Trigger.isUpdate) {
        // This is where your previous logic goes
    }
}