It happens because we probably didn’t optimize our SOQL calls. We get this error when we structure our trigger wrong. A common reason for getting the ‘too many SOQL calls’ exception is because we are placing a SOQL query inside a loop. This is a big no-no because it can very easily get out of control. A best practice for writing triggers is to gather all the data from the database at the beginning of the trigger, sort it into data structures, then access the data structures later in the trigger (instead of making SOQL queries). We should be able to do most triggers with just a few SOQL queries.
Solution:
It happens because we probably didn’t optimize our SOQL calls. We get this error when we structure our trigger wrong. A common reason for getting the ‘too many SOQL calls’ exception is because we are placing a SOQL query inside a loop. This is a big no-no because it can very easily get out of control. A best practice for writing triggers is to gather all the data from the database at the beginning of the trigger, sort it into data structures, then access the data structures later in the trigger (instead of making SOQL queries). We should be able to do most triggers with just a few SOQL queries.