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

Update Field Values In Parent Object
Hi guys,
I have created 12 custom number fields in the OppLineItem, each labeled a month of the year. I have also created a custom formula text field in the OppLineItemSchedule object called Month which defines the month based on the scheduled date.
I am trying to create a trigger to map the Quantity value from the related OppLineItemSchedule record to the relevant OppLineItem Month field. I have created the below trigger but the issue is that it is populating every Month field on the OppLineItem with a value even if there is not a related OppLineItemSch Quantity.
trigger MapMontsOnSchedule on OpportunityLineItemSchedule (after insert, before update) {
Map<Id,Integer> MapMonths = new Map<Id, Integer>();
for(OpportunityLineItemSchedule sch:trigger.new) {
MapMonths.put(sch.OpportunityLineItemId, Integer.valueOf(sch.Quantity));
}
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()])
{
if(MapMonths.containsKey(oli.id))
{
OppLineItemList.add(new OpportunityLineItem(Id = oli.id, January__c=MapMonths.get(oli.id),
February__c=MapMonths.get(oli.id),
March__c=MapMonths.get(oli.id),
April__c=MapMonths.get(oli.id),
May__c=MapMonths.get(oli.id),
June__c=MapMonths.get(oli.id),
July__c=MapMonths.get(oli.id),
August__c=MapMonths.get(oli.id),
September__c=MapMonths.get(oli.id),
October__c=MapMonths.get(oli.id),
November__c=MapMonths.get(oli.id),
December__c=MapMonths.get(oli.id)));
}
}
update OppLineItemList;
}
I have created 12 custom number fields in the OppLineItem, each labeled a month of the year. I have also created a custom formula text field in the OppLineItemSchedule object called Month which defines the month based on the scheduled date.
I am trying to create a trigger to map the Quantity value from the related OppLineItemSchedule record to the relevant OppLineItem Month field. I have created the below trigger but the issue is that it is populating every Month field on the OppLineItem with a value even if there is not a related OppLineItemSch Quantity.
trigger MapMontsOnSchedule on OpportunityLineItemSchedule (after insert, before update) {
Map<Id,Integer> MapMonths = new Map<Id, Integer>();
for(OpportunityLineItemSchedule sch:trigger.new) {
MapMonths.put(sch.OpportunityLineItemId, Integer.valueOf(sch.Quantity));
}
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()])
{
if(MapMonths.containsKey(oli.id))
{
OppLineItemList.add(new OpportunityLineItem(Id = oli.id, January__c=MapMonths.get(oli.id),
February__c=MapMonths.get(oli.id),
March__c=MapMonths.get(oli.id),
April__c=MapMonths.get(oli.id),
May__c=MapMonths.get(oli.id),
June__c=MapMonths.get(oli.id),
July__c=MapMonths.get(oli.id),
August__c=MapMonths.get(oli.id),
September__c=MapMonths.get(oli.id),
October__c=MapMonths.get(oli.id),
November__c=MapMonths.get(oli.id),
December__c=MapMonths.get(oli.id)));
}
}
update OppLineItemList;
}
All Answers
I have applied your code but it is not firing. It's not updating the fields on the OpportunityLineItem. This is the code that I have.
trigger MapMontsOnSchedule on OpportunityLineItemSchedule (after insert, before update) {
Map<Id,OpportunityLineItemSchedule> MapMonths = new Map<Id, OpportunityLineItemSchedule>();
for(OpportunityLineItemSchedule sch : trigger.new) {
MapMonths.put(sch.OpportunityLineItemId, sch);
}
List<OpportunityLineItem> OppLineItemList = new List<OpportunityLineItem>();
// Create a temp before the for loop to hold a value while looping
OpportunityLineItemSchedule tempOLIS;
// Your for loop code that grabs all the opportuniy line items
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)) {
// Get the opportunity line item schedule record
tempOLIS = MapMonths.get(oli.id);
// Create a switch statement to check what value is the Month
switch on tempOLIS.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 = tempOLIS.Quantity;
}
when 'February' {
oli.February__c = tempOLIS.Quantity;
}
when 'March' {
oli.March__c = tempOLIS.Quantity;
}
when 'April' {
oli.April__c = tempOLIS.Quantity;
}
when 'May' {
oli.May__c = tempOLIS.Quantity;
}
when 'June' {
oli.June__c = tempOLIS.Quantity;
}
when 'July' {
oli.July__c = tempOLIS.Quantity;
}
when 'August' {
oli.August__c = tempOLIS.Quantity;
}
when 'September' {
oli.September__c = tempOLIS.Quantity;
}
when 'October' {
oli.October__c = tempOLIS.Quantity;
}
when 'November' {
oli.November__c = tempOLIS.Quantity;
}
when 'December' {
oli.December__c = tempOLIS.Quantity;
}
// If it did not find any matches
when else {
// Then do whatever logic here, for now I print a system debug
update OppLineItemList;
}
}
}
}
Below is what I have done. I think I have placed your suggested changes in the correct places but it is not working.
trigger MapMontsOnSchedule on OpportunityLineItemSchedule (after insert, before update) {
Map<Id,OpportunityLineItemSchedule> MapMonths = new Map<Id, OpportunityLineItemSchedule>();
// Create a temp before the for loop to hold a value while looping
OpportunityLineItemSchedule tempOLIS;
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)) {
// Get the opportunity line item schedule record
tempOLIS = MapMonths.get(oli.id);
// Create a switch statement to check what value is the Month
switch on tempOLIS.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 = tempOLIS.Quantity;
}
when 'February' {
oli.February__c = tempOLIS.Quantity;
}
when 'March' {
oli.March__c = tempOLIS.Quantity;
}
when 'April' {
oli.April__c = tempOLIS.Quantity;
}
when 'May' {
oli.May__c = tempOLIS.Quantity;
}
when 'June' {
oli.June__c = tempOLIS.Quantity;
}
when 'July' {
oli.July__c = tempOLIS.Quantity;
}
when 'August' {
oli.August__c = tempOLIS.Quantity;
}
when 'September' {
oli.September__c = tempOLIS.Quantity;
}
when 'October' {
oli.October__c = tempOLIS.Quantity;
}
when 'November' {
oli.November__c = tempOLIS.Quantity;
}
when 'December' {
oli.December__c = tempOLIS.Quantity;
}
// If it did not find any matches
when else {
// Your for loop code that grabs all the opportuniy line items
for(OpportunityLineItemSchedule sch : trigger.new) {
MapMonths.put(sch.OpportunityLineItemId, sch);
}
// Then do whatever logic here, for now I print a system debug
update OppLineItemList;
}
}
OppLineItemList.add(oli);
}
}
when 'January' { // Then assign the opportunity line item's January field to the opportunity line item schedule's quantity oli.January__c = tempOLIS.Quantity; OppLineItemList.add(oli); }
// same thing for the rest
The Opportunity Line Item Schedule Quanity will be mapped to the Opportunity Line Item January field right?
As for your current code, there are a few misplaced lines.
I have updated the code and the results seem a bit weird. The only field that is updating is May__c, all of the other fields remain blank.
Number Of Installments = 12
Schedule Type = Divided Amount Into Multiple Installments
So there should be a value added to each of the "Month" fields in the Opportunity Line Item.
Am I right to assume that you have twelve Opportunity Line Item Schedule records per Opportunity Line Item if that is the case?
OpportunityLineSchedule Sample - Month = Oct
OpportunityLineItem - Oct is blank but it should show the Quantity as per above. It does show the Quantity for May though
Give me a bit while I update the code to reflect the changes that need to be made.
I'm getting this error on line 20.
Expecting '>' but was: ')'
tempOlisList = new List<OpportunityLineItemSchedule>();?
I have found a bug with this code. If I delete the schedule the values in the OpportunityLineItem remains. Is there a way to show blank values in the OpportunityLineItem fields if the related schedules are deleted or removed. They are updating if I change the values in the OpporunityLineItemSchedule but the are not updating to a blank value if I delete the related schedule.