You need to sign in to do that
Don't have an account?
Too many SOQL queries: 21
I have done some searching and understand why I'm getting this error, but I don't understand how to work around it.
public class CP_OpportunityClosedWon { public static void CP_createNewCPUser(Opportunity[] opps){ String cName; Contact c; OpportunityLineItem[] oli; for (Opportunity o:opps){ if (o.Order_Status__c == 'Shipped' || o.Order_Status__c == 'Definite') { ID cID = [select contactid from opportunitycontactrole where role = 'Ship To' AND opportunityid = :o.id].contactId; c = [select id, title from contact where id = :cID]; //cName = [select title from contact where id = :cID].title; oli = [select id from opportunitylineitem where opportunityid = :o.Id]; } } for (OpportunityLineItem li : oli){ ID pID = [Select o.PricebookEntryId, o.PricebookEntry.Product2Id from OpportunityLineItem o where id = :li.id].PriceBookEntry.Product2Id; if(pID != null){ Product2 p = [select id from product2 where id = :pID]; String s = [select name from product2 where id = :pID].name; Boolean pd = [select downloadable__c from product2 where id = :pID].Downloadable__c; /*if(s !=null){ o.tempName__c = s; }*/ if(pd == true){ c.Portal_User__c = true; c.Portal_URL__c = c.title; update c; } } } } }
I'm getting the error on the line: ID pID = [Select o.PricebookEntryId, o.PricebookEntry.Product2Id from OpportunityLineItem o where id = :li.id].PriceBookEntry.Product2Id;
If I can't put a SOQL query inside of a for loop then how can I accomplish what I am trying to do here?
Any code help or code examples would be appriciated. I have already read about this in the APEX manual but am not grasping what they are saying.
Thank you in advance for your assistance.
_t
Hi,
Instead of updating your contact inside for loop,
Make the contact as Contact[ ] c,
in for loop add each contact inside the Contact array c like c[i].Portal_User__c = true;
update the contact outside the for loop.
It may work..
Not sure what you're trying to do with your Contact records - but this is the sort of methodology you need to use to keep SOQL queries outside of your loops. Note the extra fields pulled from OpportunityLineItem in that 'select', removing the need altogether for that first 'select' statement in your second loop (which needs the same sort of attention as shown above, to get those other three embedded SOQL queries out of there)