You need to sign in to do that
Don't have an account?

Salesforce SOQL Error Too many SOQL 001
Hi All,
I'm running one time update using Execute Anonymous and getting error "Salesforce SOQL Error Too many SOQL 001" for the following code, I tried few options to change the code but couldn't run the update, anyone can help with this code please: (Opportuntiy is master for custom object Sales Team, 1 : M)
List <Opportunity> OpptySalesRep = [Select Id,Sales_Rep__c From Opportunity Where Sales_Rep__c !=null and (CreatedDate > 2014-11-01T00:00:00Z and CreatedDate <= 2014-11-30T00:00:00Z)];
set<Id> opportunityIdSet = new set<Id>();
for (Opportunity Oppty : OpptySalesRep) {
list<Sales_Team__c> newGSTmember = new list<Sales_Team__c>();
opportunityIdSet.add(Oppty.Id); //Needed for following SOQL for GST
//Query to get all Sales team memeber for the opportunity
List <Sales_Team__c> gst = [Select Sales_Team_Member__c,Primary__c,Producer__c,Opportunity__c,Id
From Sales_Team__c
Where Opportunity__c IN: opportunityIdSet and Producer__c = null and Opportunity__c <> null];
system.debug('Number of Sales Team Member# ' + gst.size());
//Reset opportunityIdSet for avoid duplicate iteration
opportunityIdSet.remove(Oppty.Id);
boolean IsnewSalesRep = true; //to check if Sales Rep already exists in GST
// Loop through the list and update GST record that matches to Sales Rep
for (Sales_Team__c OpptyGST : gst){
system.debug('GST Memebr# : ' + OpptyGST.Sales_Team_Member__c);
//Update Sales Rep to Primary
if (OpptyGST.Sales_Team_Member__c == Oppty.Sales_Rep__c){
system.debug('INSIDE If');
OpptyGST.Primary__c = true;
IsnewSalesRep = false;
}
// Update Sales reps to non-primary
else if (OpptyGST.Sales_Team_Member__c != Oppty.Sales_Rep__c && OpptyGST.Primary__c == true){
system.debug('INSIDE else If');
OpptyGST.Primary__c = false;
}
}
//Add Sales Rep to GST
if (IsnewSalesRep == true)
{
Sales_Team__c newGST = new Sales_Team__c (); //instantiate the GST object to put values
newGST.Sales_Team_Member__c = Oppty.Sales_Rep__c;
newGST.Primary__c = true;
newGST.Opportunity__c = Oppty.Id;
newGSTmember.add(newGST);
insert newGSTmember;
}
update gst;
}
I'm running one time update using Execute Anonymous and getting error "Salesforce SOQL Error Too many SOQL 001" for the following code, I tried few options to change the code but couldn't run the update, anyone can help with this code please: (Opportuntiy is master for custom object Sales Team, 1 : M)
List <Opportunity> OpptySalesRep = [Select Id,Sales_Rep__c From Opportunity Where Sales_Rep__c !=null and (CreatedDate > 2014-11-01T00:00:00Z and CreatedDate <= 2014-11-30T00:00:00Z)];
set<Id> opportunityIdSet = new set<Id>();
for (Opportunity Oppty : OpptySalesRep) {
list<Sales_Team__c> newGSTmember = new list<Sales_Team__c>();
opportunityIdSet.add(Oppty.Id); //Needed for following SOQL for GST
//Query to get all Sales team memeber for the opportunity
List <Sales_Team__c> gst = [Select Sales_Team_Member__c,Primary__c,Producer__c,Opportunity__c,Id
From Sales_Team__c
Where Opportunity__c IN: opportunityIdSet and Producer__c = null and Opportunity__c <> null];
system.debug('Number of Sales Team Member# ' + gst.size());
//Reset opportunityIdSet for avoid duplicate iteration
opportunityIdSet.remove(Oppty.Id);
boolean IsnewSalesRep = true; //to check if Sales Rep already exists in GST
// Loop through the list and update GST record that matches to Sales Rep
for (Sales_Team__c OpptyGST : gst){
system.debug('GST Memebr# : ' + OpptyGST.Sales_Team_Member__c);
//Update Sales Rep to Primary
if (OpptyGST.Sales_Team_Member__c == Oppty.Sales_Rep__c){
system.debug('INSIDE If');
OpptyGST.Primary__c = true;
IsnewSalesRep = false;
}
// Update Sales reps to non-primary
else if (OpptyGST.Sales_Team_Member__c != Oppty.Sales_Rep__c && OpptyGST.Primary__c == true){
system.debug('INSIDE else If');
OpptyGST.Primary__c = false;
}
}
//Add Sales Rep to GST
if (IsnewSalesRep == true)
{
Sales_Team__c newGST = new Sales_Team__c (); //instantiate the GST object to put values
newGST.Sales_Team_Member__c = Oppty.Sales_Rep__c;
newGST.Primary__c = true;
newGST.Opportunity__c = Oppty.Id;
newGSTmember.add(newGST);
insert newGSTmember;
}
update gst;
}
//Query to get all Sales team memeber for the opportunity
List <Sales_Team__c> gst = [Select Sales_Team_Member__c,Primary__c,Producer__c,Opportunity__c,Id
From Sales_Team__c
Where Opportunity__c IN: opportunityIdSet and Producer__c = null and Opportunity__c <> null];
--- should be outside the for loop.
The query is getting fored each time the loop iterates. Rather query the related child records
Select Sales_Team_Member__c,Primary__c,Producer__c,Opportunity__c,Id
From Sales_Team__c
Where Opportunity__c IN: OpptySalesRep and Producer__c = null and Opportunity__c <> null
Then use a map to store the opportunity to the child records.
Thanks,
Kaustav
List <Opportunity> OpptySalesRep = [Select Id,Sales_Rep__c,
(Select Sales_Team_Member__c,Primary__c,
Producer__c,Opportunity__c,Id
From Sales_Team__r) // Relationship Object Name
From Opportunity
Where Sales_Rep__c !=null
and (
CreatedDate > 2014-11-01T00:00:00Z
and
CreatedDate <= 2014-11-30T00:00:00Z
)
];
Please check Relationship name in your Object.
(Select Sales_Team_Member__c,Primary__c,
Producer__c,Opportunity__c,Id
From Sales_Team__r) // Relationship Object Name
From Opportunity
Where Sales_Rep__c !=null
and (
CreatedDate > 2014-11-01T00:00:00Z
and
CreatedDate <= 2014-11-30T00:00:00Z
)
];
===========================
List<Sales_Team__c> TeamList = new List<sales_Team__c>();
for(Opportunity obj : OpptySalesRep){
for(Sales_Team__c Team : obj.Sales_Team__r){
Team.Primary__c = true;
TeamList.add(Team);
}
}
Database.SaveResult TeamUpdate = Database.update(TeamList);
=========================
Take Reference from above code.