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
AngiB.ax1285AngiB.ax1285 

Trigger passes it's own test but fails a related test (Too many SOQL queries: 101)

I have just added a trigger to our org on a custom object called Fixed_Asset__c.

The test class I built for the new trigger PASSES.

But when running another test class that causes updates to Fixed_Asset__c objects I get the following failure.

 

Error Message    System.LimitException: AcctSol:Too many SOQL queries: 101

Stack Trace         Trigger.AcctSol.InsuranceRollUpFixedAssets: line 61, column 1

 

My trigger code can be seen below.

 

I am VERY new to Force.com so undoubtedly have done something silly that is causing this. Any assistance that you could provide would be greatly Appreciated.

 

------

My Trigger Code:

 

trigger InsuranceRollUpFixedAssets on Fixed_Asset__c (after delete, after insert, after update, after undelete) {

 

    //Limit the size of list by using Sets which do not contain duplicate elements

 

    set<Id> InsuranceIds = new set<Id>();

  

    // Adding

    if(trigger.isInsert){

        for(Fixed_Asset__c p : trigger.new){

            InsuranceIds.add(p.AcctSol__Insurance_Policy_LU__c);

        }

    }

 

    // Updating

    if (trigger.isUpdate){

            for(Fixed_Asset__c p : trigger.new){

                InsuranceIds.add(p.AcctSol__Insurance_Policy_LU__c);

            }

            for(Fixed_Asset__c p : trigger.old){

                InsuranceIds.add(p.AcctSol__Insurance_Policy_LU__c);

            }

    }

 

    // Deleting

    if(trigger.isDelete){

        for(Fixed_Asset__c p : trigger.old){

            InsuranceIds.add(p.AcctSol__Insurance_Policy_LU__c);

        }

    }

 

    // Undeleting

    if(trigger.isUnDelete){

        for(Fixed_Asset__c p : trigger.new){

            InsuranceIds.add(p.AcctSol__Insurance_Policy_LU__c);

        }

    }

 

    //Map will contain one Insurance Id to one sum value

 

    map<Id,Double> InsuranceMap1 = new map <Id,Double>();

    map<Id,Double> InsuranceMap2 = new map <Id,Double>();

 

    List<aggregateResult> results = [select AcctSol__Insurance_Policy_LU__c,sum(AcctSol__Remaining_Orginal_Cost__c),sum(AcctSol__Original_Cost__c)

                                    from Fixed_Asset__c where AcctSol__Insurance_Policy_LU__c IN :InsuranceIds group by AcctSol__Insurance_Policy_LU__c];

    system.debug('Test**************'+results);

 

    //Produce a sum of Fixed_Asset__c and add them to the map

    //use group by to have a single Insurance Id with a single sum value

 

    for(AggregateResult q : results){

        InsuranceMap1.put((Id)q.get('AcctSol__Insurance_Policy_LU__c'),(Double)q.get('expr0'));

        system.debug('Test**************'+InsuranceMap1);

        InsuranceMap2.put((Id)q.get('AcctSol__Insurance_Policy_LU__c'),(Double)q.get('expr1'));    

    }

 

    List<Insurance__c> InsurancesToUpdate = new List<Insurance__c>();

 

    //Run the for loop on Insurance using the non-duplicate set of Insurance Ids

    //Get the sum value from the map and create a list of Insurances to update

 

    List<Insurance__c> first_o = [Select Id, Total_Asset_Book_Value__c,Total_Original_Cost__c from Insurance__c where Id IN :InsuranceIds];

 

    for(Insurance__c o : first_o){

        Double BookValueSum = (InsuranceMap1.get(o.Id) == null) ? 0:InsuranceMap1.get(o.Id); // if null value is zero

        o.Total_Asset_Book_Value__c = BookValueSum;

 

        Double OrigCostSum = (InsuranceMap2.get(o.Id) == null) ? 0:InsuranceMap2.get(o.Id); // if null value is zero

        o.Total_Original_Cost__c = OrigCostSum;

 

        InsurancesToUpdate.add(o);

    }

 

    update InsurancesToUpdate;

 

}

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

Check trigger on Insurance_c for SOQL inside for loops...

 

Just because it errored on your new trigger does not mean your trigger is at fault, it just happens to make the 101st soql call.

 

I did not see any soql inside loops on yours but it was a quick glance

All Answers

Starz26Starz26

Check trigger on Insurance_c for SOQL inside for loops...

 

Just because it errored on your new trigger does not mean your trigger is at fault, it just happens to make the 101st soql call.

 

I did not see any soql inside loops on yours but it was a quick glance

This was selected as the best answer
Alex.AcostaAlex.Acosta

Your code looks fine, what is happening is that when your other test cases fire off you have a chain reaction. To explain this as simple as possible, think of this chain reaction as an instance. During this instance your code is limited to the governor limits. So if you have 3 triggers daisy chain, all of your queries will count as 1 instance. So what ever is happening, it's your total sum of everything triggering off another trigger/class.

AngiB.ax1285AngiB.ax1285

I think my trigger did have some SOQL inside a loop this morning near the bottom but I changed it to use a list.

 

I think I will have to see if I can contact the developers who wrote the rest of the code and see what the what is.



Thanks for your assistance.







carlocarlo

It might be worth reading this thread I replied to today - http://boards.developerforce.com/t5/Apex-Code-Development/Bulkifield-trigger-still-throwing-exception/td-p/411903

 

I have see this before with my own tests.  I think there is a limit to the number of SOQL queries per test.  So if one of your tests runs several individual updates / inserts then the number of SOQL queries for all of these is added up.

 

Think of it this way.  You bulkify your code.  You need to bulkify your tests too.