+ Start a Discussion

Getting ProductCode of PriceBookEntry directly from Opportunity

I'd like to find out, if a product, related to an opportunity is a kind of support-type product. The unique information is in the product code and I'd like to pick a checkbox on the opportunity if there's any of them. (and of course pick it false if all of the support products have been removed).

I've created a trigger but I get the "Too many SOQL queries: 21" error. I know what should I do but as a newbie I have no idea how.


thanks for the ideas!


Here's my code:


trigger subscriptionSupport on Opportunity (before insert, before update, after insert, after update)
    Boolean subAndSup=false;
    List<Id> opportunityId=new List<Id>();
    List<Id> priceBookEntryIds=new List<Id>();
    for(Opportunity t:trigger.new){
        if (t.HasOpportunityLineItem == true) {
            for(OpportunityLineItem pbe: [SELECT PriceBookEntryId FROM OpportunityLineItem WHERE OpportunityId IN :opportunityId] ) {
            for(PriceBookEntry pc:
                          [SELECT ProductCode FROM PriceBookEntry WHERE Id IN : priceBookEntryIds ] ) {
                if(pc.ProductCode.endsWith('bsu')==TRUE  ||


                 { subAndSup = true; }

    List<Opportunity> opportunityToUpdate=
            [SELECT Id,Subscription_Support__c FROM Opportunity WHERE Id IN :opportunityId];
    For (Opportunity u:opportunityToUpdate){
        u.Subscription_Support__c = subAndSup;
        update opportunityToUpdate;
    }catch(DMLException e){
        system.debug('Opportunity was not properly updated.  Error: '+e);





I have an idea. I could approach the problem from the OpportunityLineItem: I would create a trigger on LineItems and let it fire only when the related opportunity is getting closed won.

The only question is:

will the LineItems get updated when I save the related opportunity? (so the trigger could fire)


thanks for the answers!