0

I have 12 custom number fields on the OpportunityLineItem, each representing a month of the year (Jan to Dec). In OpportunityLineItemSchedule I have a custom formula field that displays a Month based on the ScheduleDate field. The trigger below is working fine, however if I delete an OpportunityLineItemSchedule it does not update the related field in the OppLineItem to a null value.

trigger MapMontsOnSchedule on OpportunityLineItemSchedule (after insert, after update, after delete, after undelete) {

// UPDATED: Must handle multiple children for a single parent
// Instead of a single OpportunityLineItemSchedule, 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 of OpportunityLineItems to update   
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;  
}
0

You will not get Trigger.new in after delete (Trigger context variables). So, you should consider using Trigger.old in delete context.

Also, You can minimize the code as below:

  1. There is no need of tempOlisList as you can directly implement for logic for loading MapMonths

  2. Check below code:

    Account acc = new Account(Name='Test');
    insert acc;
    

This is same as:

    Account acc = new Account();
    acc.put('Name','Test');
    insert acc;

Updated code:

trigger MapMontsOnSchedule on OpportunityLineItemSchedule (after insert, after update, after delete, after undelete) {

    Map<Id,List<OpportunityLineItemSchedule>> MapMonths = new Map<Id, List<OpportunityLineItemSchedule>>();
    List<OpportunityLineItemSchedule> targetOlis = Trigger.isDelete ? Trigger.old : Trigger.new;

    for(OpportunityLineItemSchedule sch : targetOlis) {
        if(!MapMonths.containsKey(sch.OpportunityLineItemId)) {
            MapMonths.put(sch.OpportunityLineItemId, new List<OpportunityLineItemSchedule>());
        }
        MapMonths.get(sch.OpportunityLineItemId).add(sch);
    }

    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()
    ]) {
        for(OpportunityLineItemSchedule olis : MapMonths.get(oli.id)) {
            if(Trigger.isDelete) oli.put(olis.Month__c+'__c', null);
            else oli.put(olis.Month__c+'__c', olis.Quantity);
        }
        OppLineItemList.add(oli);
    }
    update OppLineItemList;  
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.