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
gyorkozgyorkoz 

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) {
            opportunityId.add(t.Id);
            for(OpportunityLineItem pbe: [SELECT PriceBookEntryId FROM OpportunityLineItem WHERE OpportunityId IN :opportunityId] ) {
                priceBookEntryIds.add(pbe.PriceBookEntryId);
            }
            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;
    }
    
    try{
        update opportunityToUpdate;
    }catch(DMLException e){
        system.debug('Opportunity was not properly updated.  Error: '+e);
    }

}

 

 

gyorkozgyorkoz

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!