• Marco Pouleyn
  • NEWBIE
  • 10 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
Hi,

In my situation I have a Custom Object (Object__c) which can be created in 2 types: TypeA or TypeB, where TypeA is always created first and on a later date TypeB will be created with the same Title and Episode. I created a Trigger that when TypeB is created the Trigger will find a matching TypeA card based on the Title and episode, and fill the new TypeB card with Date1 and Date2 from the matching TypeA card, and it will link TypeA to TypeB with a lookupfield ObjectMatch__c

--------------------------------------------------------
trigger ObjectFindMatch on Object__c (before update) {

  for (Object__c newObj : Trigger.new) {
  for (Object__c extObj : [SELECT Id, Date1__c, Date2__c FROM Object__c 
       WHERE Type__c = 'TypeA' 
       AND Titel_c = :newObj.Titel__c 
       AND Episode_c = :newObj.Episode__c]) { 
  
  IF (newObj.Type__c = 'TypeB') {
      newObj.ObjectMatch__c = extObj.Id;
      newObj.Date1__c = extObj.Date1__c;
      newObj.Date2__c = extObj.Date2__c;
   }

  }
 }
}
--------------------------------------------------------

The Trigger works fine but the cards can also be created in a batch (for example) episodes 1 to 10, then I get a: LIMIT_EXCEEDED: System.LimitException: Too many SOQL queries: 101
I am reading some best practices like:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_bestpract.htm
But I can't figure out how to optimise my code :(

Please help ?

Kind regards,
Marco