+ Start a Discussion
ColealarsColealars 

Updating OpportunityLineItem from Product2

Can someone tell me what I'm missing here.  In the debug log it says no records found after I update an OpportunityLineItem record:

 

Trigger.oppLineItem_prod_data_trigger2: line 20, column 33: SOQL query with 0 rows finished in 3 ms

 

trigger oppLineItem_prod_data_trigger2 on OpportunityLineItem (after insert, after update) { Set<String> opportunityLineItemIDs = new Set<String>(); List<OpportunityLineItem> updtOpportunityLineItems = new List<OpportunityLineItem>(); OpportunityLineItem[] opportunityLineItems = new List<OpportunityLineItem>(); OpportunityLineItem[] oliList = [select pricebookentry.product2.bu_code__c FROM OpportunityLineItem where id in :opportunityLineItemIDs]; for(OpportunityLineItem oli : oliList) { oli.bu_code__c = oli.pricebookentry.product2.bu_name__c; } update oliList; }

 

Anand@SAASAnand@SAAS

opportunityLineItemIDs does'nt seem to have the Id that you are looking for. You'll need to populate the Ids into that list before using in the SOQL. Try this:

 

trigger oppLineItem_prod_data_trigger2 on OpportunityLineItem (before insert, before update) {Set<String> opportunityLineItemIDs = new Set<String>(); List<OpportunityLineItem> updtOpportunityLineItems = new List<OpportunityLineItem>(); OpportunityLineItem[] opportunityLineItems = new List<OpportunityLineItem>();for(OpportunityLineItem item: trigger.new){ opportunityLineItemIDs.add(item.Id);}OpportunityLineItem[] oliList = [select pricebookentry.product2.bu_code__c FROM OpportunityLineItem where id in :opportunityLineItemIDs];for(OpportunityLineItem oli : oliList) { oli.bu_code__c = oli.pricebookentry.product2.bu_name__c; } //You don't need to explicitly update in a "before" trigger //update oliList;}

 

 

 

 

On a different note, you are trying update the Opportunity Line Item again in an "after insert" trigger which will result in an "CANNOT_UPDATE_ENTITTY" (or something like that) error as the record is already saved.

ItbkannanItbkannan

Hi

 

Use Apex Class to assign some values to opportunity item

 

Sample Helloworld Apex class

 

// This class updates the Hello field on account records that are

// passed to it.

public class MyHelloWorld {

public static void addHelloWorld(Account[] accs){

for (Account a:accs){

if (a.Hello__c != 'World') {

a.Hello__c = 'World';

}

}

}

}

 

And Trigger is

 

trigger helloWorldAccountTrigger on Account (before insert) {

Account[] accs = Trigger.new;

MyHelloWorld.addHelloWorld(accs); 

}

 

Sample Hello world program assign some values to custom attribute of the Accoun object.

 

 

let me know if you have concerns

 

 

Thanks

Kannan B

 

ColealarsColealars
Thanks for the response.  I do get the error SELF_REFERENCE_FROM_TRIGGER but how do I deal with this now?
amorganamorgan
I think this has the answer http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_bulk_idioms.htm Under the title Using Maps and Sets in Bulk Triggers This worked for me.
Mouse.liuMouse.liu

opportunityLineItemIDs is null,why?