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
ColealarsColealars 

Trigger Compile Error trying to update multiple fields on OppLineItem with data from Product2

I get the following error:

 

Error: Compile Error: Variable does not exist: oli.sbu_abbr__c at line 21 column 7 

 

When I try to save the trigger code below. 

trigger oppLineItem_prod_data_trigger on OpportunityLineItem (before insert, before update) { Set<Id> pbeIds = new Set<Id>(); for (OpportunityLineItem oli : Trigger.new) pbeIds.add(oli.pricebookentryid); Map<Id, PricebookEntry> entries = new Map<Id, PricebookEntry>( [Select product2.sbu_abbr__c, product2.bu_abbr__c From PricebookEntry where id in :pbeIds]); . for (OpportunityLineItem oli : Trigger.new) oli.bu_abbr__c = entries.get(oli.pricebookEntryId).product2.bu_abbr__c; oli.sbu_abbr__c = entries.get(oli.pricebookEntryId).product2.sbu_abbr__c; }

 

Can someone tell me what I'm doing wrong.

 

Thanks.

jinmanjinman

I beleive this is a simple syntax error: you are missing the "{" when starting your for loop...

 

It should be...

 

for (OpportunityLineItem oli : Trigger.new){
oli.bu_abbr__c = entries.get(oli.pricebookEntryId).product2.bu_abbr__c;
oli.sbu_abbr__c = entries.get(oli.pricebookEntryId).product2.sbu_abbr__c;
}

 


Colealars wrote:

I get the following error:

 

Error: Compile Error: Variable does not exist: oli.sbu_abbr__c at line 21 column 7 

 

When I try to save the trigger code below. 

trigger oppLineItem_prod_data_trigger on OpportunityLineItem (before insert, before update) {

Set<Id> pbeIds = new Set<Id>();
for (OpportunityLineItem oli : Trigger.new)
pbeIds.add(oli.pricebookentryid);

Map<Id, PricebookEntry> entries = new Map<Id, PricebookEntry>(
[Select product2.sbu_abbr__c, product2.bu_abbr__c From PricebookEntry where id in :pbeIds]);
.
for (OpportunityLineItem oli : Trigger.new)
oli.bu_abbr__c = entries.get(oli.pricebookEntryId).product2.bu_abbr__c;
oli.sbu_abbr__c = entries.get(oli.pricebookEntryId).product2.sbu_abbr__c;
}

 

Can someone tell me what I'm doing wrong.

 

Thanks.