You need to sign in to do that
Don't have an account?
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; }
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.
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
opportunityLineItemIDs is null,why?