1

I have a trigger. It inserts imported Gears with correct fields' values. One of valu I took from Detail__c - related to MoreInfo__c object. And Tool__c is related to MoreInfo__c too. Each MoreInfo__c has only one Detail__c record, but can have many Tool__c records.

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;
}

Then I fixed it and now I have this thing:

trigger GearCreationTrigger on Tool__c (after insert) { 
List<Gear__c> toInsert = new List<Gear__c>();
List<String> moreInfo = new List<String>();
List<Detail__c> details = [SELECT Info__c FROM Detail__c
                                WHERE MoreInfo__c IN : moreInfo LIMIT 1];

for(Tool__c tool : Trigger.new){
    moreInfo.add(tool.MoreInfo__c);

    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;
}

But when I test it, I'm getting null value instead of correct MoreInfo__c.

What is wrong here and how can I fix it?

  • Now you have a SOQL inside a for loop which is against best practices since for every record affected by the trigger you will repeat the query, this of course will cause you to reach very easily the governor limits. – Alexander Aeons Torn Aug 5 at 9:26
  • But I took it out of the cycle. The code is below the first. But what is wrong again? How to fix it? – Apelsinova Aug 5 at 9:30
  • Sorry, I messed up the places. Changed. – Apelsinova Aug 5 at 9:32
  • The issue is that you are doing the query before you have put the data into the list you're checking to obtain the records. But it would be helpful if you edited the question and explain your use case so we can understand better what are you trying to achieve and help you better to understand what is wrong with the current logic. – Alexander Aeons Torn Aug 5 at 9:36
  • Thank you, Alexander. I changed my question. – Apelsinova Aug 5 at 9:42
3

As your code is right now, moreInfo is an empty List, so you shouldn't be getting any results:

List<String> moreInfo = new List<String>();   // <=== EMPTY right now!
List<Detail__c> details = [SELECT Info__c FROM Detail__c
                                WHERE MoreInfo__c IN : moreInfo LIMIT 1];

Use your for loop before the details SOQL query:

List<String> moreInfo = new List<String>();
for(Tool__c tool : Trigger.new){
    moreInfo.add(tool.MoreInfo__c);
}

List<Detail__c> details = [SELECT Info__c FROM Detail__c
                                WHERE MoreInfo__c IN : moreInfo]; // NOW getting results

You'll still want to map out each detail to it's MoreInfo__c lookup field, so that you're pulling the right data for each tool

  • Hm. So I'll get just the last element from the end of loop in my moreInfo list? I can get through import 100 or 200 records... And how can I get Gear__c gear = new Gear__c(Tool__c = tool.Id); - the tool.Id value too? – Apelsinova Aug 5 at 10:40
  • The point is I can have many importing Tools. For each of these records I have to create and insert a new Gear__c with correct fields (all fields are tied to inserting records). – Apelsinova Aug 5 at 10:45
  • @Apelsinova Your interpretation isn't quite right. The first loop over your Tools, using Trigger.new to feed it. Each iteration of the first loop adds a value to the moreInfo list, so if your trigger is working on 200 records, you'll get all 200 values of MoreInfo__c in that moreInfo list (including any duplicates). As for your other question (how to get tool.Id for your Gear__c records), well, you said that MoreInfo__c is related to Tool__c, so what's stopping you from adding that to your details query? – Derek F Aug 5 at 11:44

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.