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
NadiaNadia 

"maximum trigger depth exceeded" and other errors

    Hi,  I have wrote my code following documentation to adjust it to governors and limits restrictions. After deployment we get "maximum trigger depth exceeded" exception. I am pretty sure I do not have any recursive triggers, and I can't think of anything else I can do to diminish number of DML and SQL statemetns. (I do not have them inside loops). I tried to replicate the error by creating large number of records in test, and now I get System.Exception: Too many SOQL queries: 21. I dont  know if this is what is causing "maximum trigger depth exceeded"  in production. I cant replicate the original error.
I am now wondering if it might be so that it is not possible for us, since we are updating very large sets of records at a time using API - possibly hundreds of thousands. Could it be that APEX is just not the solution with such big data sets, or am I doing something wrong?

Message Edited by Nadia on 03-20-2008 10:25 AM
NadiaNadia
Code:
public static void updateOpportunityFileNameOpps(Opportunity[] opps){
System.debug('>>Call to SCIP.updateOpportunityFileNameOpps');


Set<Id> oppIds = new Set<Id>();
for (Opportunity o: opps){
oppIds.add(o.id);
}
System.debug('>>created oppIds set with size: ' + oppIds.size());

Map<ID, Opportunity> oppMap;
try {
oppMap= new Map<ID, Opportunity>(
[select opportunity.Opportunity_File_Name__c,
opportunity.Trading_Account_Type__c,
opportunity.UserName__c,
opportunity.account.Name ,
opportunity.account.Type
from opportunity where opportunity.id in :oppIds]);
}catch (Exception e) {
System.debug('>>error creating opp map: ' + e.getMessage());
}

if(Trigger.isInsert){
integer toUpdate = 0;
for (ID id:oppMap.keySet()){
try {
String s = '';
if(oppMap.get(id).account.Type != null){
s = s + oppMap.get(id).account.Type;
}
if(oppMap.get(id).Trading_Account_Type__c != null){
s = s + oppMap.get(id).Trading_Account_Type__c;
}
if(oppMap.get(id).account.Name != null){
s = s + oppMap.get(id).account.Name;
}
if(oppMap.get(id).UserName__c != null){
s = s + oppMap.get(id).UserName__c;
}
if(oppMap.get(id).Opportunity_File_Name__c != s){
oppMap.get(id).Opportunity_File_Name__c = s;
toUpdate++;
}
} catch (Exception e) {
System.debug('error at ' + id);
}
}
if (toUpdate > 0)
{
update oppMap.values();
System.debug('>>Updated ' + toUpdate + ' opportunities.' );
}
else{
System.debug('>>No opportunities to update');
}
}//end if(Trigger.isInsert)

if(Trigger.isUpdate){
for (Opportunity o: opps){

try {
String s = '';
if(oppMap.get(o.id).account.Type != null){
s = s + oppMap.get(o.id).account.Type;
}
if(o.Trading_Account_Type__c != null){
s = s + o.Trading_Account_Type__c;
}
if(oppMap.get(o.id).account.Name != null){
s = s + oppMap.get(o.id).account.Name;
}
if(o.UserName__c != null){
s = s + o.UserName__c;
}

if(o.Opportunity_File_Name__c != s){
o.Opportunity_File_Name__c = s;
}
} catch (Exception e) {
System.debug('error at ' + o.Name);
}
}
} //end Trigger.isUpdate
}
I wrote a method similar to this , and I am calling it before update and after insert. I am checking what type of operation called the trigger and updating it based on that.

When I am testing with multiple inserts (10) I see in the test log that number of SOQL queries gets incremented after each call to a trigger (update and insert), so when it reaches 21 it blows up. Does this mean I can not call insert operation more than 10 times if I have update and insert triggers on that operation with one SOQL query each?
I am quite confused of how should I proceed. Please help!



Message Edited by Nadia on 03-20-2008 10:24 AM