-1

I have a trigger. And if it's an import of many records I get an error becouse SOQL Limit is 101 records:

trigger GearCreationTrigger on Tool__c (after insert) { 
    List<Gear__c> toInsert = new List<Gear__c>();

    for(Tool__c tool : Trigger.new){
        List<Detail__c> details = [SELECT Info__c FROM Detail__c
                                    WHERE MoreInfo__c =: tool.MoreInfo__c LIMIT 1];

        Gear__c gear = new Gear__c(Tool__c = tool.Id);      
        if(details.size() > 0){
            gear.Info__c = details[0].Info__c;
        }
        toInsert.add(gear);
    }
    if(toInsert.size() > 0) insert toInsert;
}

How can I avoid limits in Trigger's SOQL?

2
List<Detail__c> details = [SELECT Info__c FROM Detail__c
                                    WHERE MoreInfo__c =: tool.MoreInfo__c LIMIT 1];

this should be outside loop. First create a List<String> to hold MoreInfo__c of all tools the use it in SOQL with IN operator

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.