You need to sign in to do that
Don't have an account?

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;
}
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;
}

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.