You need to sign in to do that
Don't have an account?
Mickey Stringer 5
Trigger error - Too many query rows: 50001
Hi all,
I developed a trigger for our org that updates a checkbox based on a corresponding checkbox in related contacts. Not being a knowledgeable developer, I took an existing trigger we have working in production, stripped it down and modified it to work on this custom object. I got it working in the sandbox and after deploying to production, am unable to create new records in this object because of a query limit in the trigger.
Having read several forum posts throughout creating this new trigger, I understand the placement and scope of SOQL queries is very important in Apex. As far as placement, I mimicked what was i nthe original trigger and so trusted it wasn't being done in loops and such. As far as scope, as you see below the Person list is unqualified, but there are no Person records yet in production. The Contact list is qualified by the Person__r.Id existing in the Person list.
I thought perhaps that having no Person records, and thus no Contact records with Person__r.Id returning a value, that might cause the list to return all Contacts, but I disabled the trigger, added a Person record, and then re-enabled it to no avail.
Posting code snippet where the lists occur. Do you all see anything improper? Thanks!
FYI, firstRunBefore is set to TRUE in the helper class
I developed a trigger for our org that updates a checkbox based on a corresponding checkbox in related contacts. Not being a knowledgeable developer, I took an existing trigger we have working in production, stripped it down and modified it to work on this custom object. I got it working in the sandbox and after deploying to production, am unable to create new records in this object because of a query limit in the trigger.
Having read several forum posts throughout creating this new trigger, I understand the placement and scope of SOQL queries is very important in Apex. As far as placement, I mimicked what was i nthe original trigger and so trusted it wasn't being done in loops and such. As far as scope, as you see below the Person list is unqualified, but there are no Person records yet in production. The Contact list is qualified by the Person__r.Id existing in the Person list.
I thought perhaps that having no Person records, and thus no Contact records with Person__r.Id returning a value, that might cause the list to return all Contacts, but I disabled the trigger, added a Person record, and then re-enabled it to no avail.
Posting code snippet where the lists occur. Do you all see anything improper? Thanks!
FYI, firstRunBefore is set to TRUE in the helper class
if(PDMPRegistrationTriggerHelperClass.firstRunBefore){ PDMPRegistrationTriggerHelperClass.firstRunBefore = false; List<Id> personIds = new List<Id>(); for(Person__c p : Trigger.new){ personIds.add(p.Id); } // Get Contacts for all People // Prevents query within the loop below List<Contact> contacts = [SELECT Id, FirstName, LastName, PDMP_Registered__c, Person__r.Id FROM Contact WHERE Person__r.Id IN: personIds]; System.debug('Number of Contacts: ' + Trigger.new.size()); for(Person__c p : Trigger.new) { System.debug('Person: ' + p.Name); System.debug('PDMP Registration Status: ' + p.PDMP_Registered__c); // Get Contacts for this Person List<Contact> con = new List<Contact>(); for(Contact loopCon : contacts){ if(loopCon.Person__r.Id == p.Id){ con.add(loopCon); System.debug('Contact: ' + loopCon.FirstName + ' ' + loopCon.LastName); } } if(con.size() == 0){ System.debug('Contact: None'); }
I am not sure what and how you copy the things but presuming if you copy the body of the code only, then one thing I can think of the first line, remove that before insert becuase looking at your code, the entire thing is based on Id which not going to work on before Insert.
All Answers
after insert
before insert
etc.
What is the relationship between Contact and the Person Object? I meant it is lookup or MasterDetail ( What is what in both the case?)
FYI: There is limitation of salesforce that you can query 50,000 records using SOQL in one transaction. Which includes all SOQL in a transaction. May be you will have more SOQL Queries in your trigger.
total number of records retrieved by SOQL queries = 50,000
Are you sure you are getting issue with above query? How many records you are getting in the above query?
You can use Batch Apex, in which there is 50k limit counts per batch execution.
The trigger is before insert and update.
The error says the SOQL row limit is reached, but I can't figured out why. If I have one Person record, and two Contact records linked to it, the first query should return one row and the second query, two rows.
You mentioned that you are running this query "before insert" and "before update". Please note "before insert", the Id field is null so the following code would break on "before insert" trigger Therefore personIds will be null in that case and there is possibility that your SOQL is going out of limits because of the that.
This is what my understanding is but if you can post the complete code including the trigger line in the code so better understand the issue and also if the line number it is throwing error to you from the debug log.
I'll post the full code below, but will have to re-enable it and run a test in Production to get a line number. No line was referenced in the original SOQL limit error.
After pasting this, I'm noticing I've reused a few variable names (started writing this a while ago and didn't remember what I wrote!)... not sure that would cause the issue, but I'm going to change some of them first.
I just edited the one Person record I created while the trigger was inactive, and that saved without issue. So it seems like it might be an issue with a Befor Insert trigger running before the ID is generated for the new record. I just can't figure out why that's happening here and not with the trigger I based this on.
I am not sure what and how you copy the things but presuming if you copy the body of the code only, then one thing I can think of the first line, remove that before insert becuase looking at your code, the entire thing is based on Id which not going to work on before Insert.
So I changed it to after insert, after update (though I guess it could still run before update) and my test class fails now. Are there any common pitfalls I should be looking for to make sure this works after insert?
Thank you for your help so far. I really appreciate it
Do you have the original trigger and would like to share to see what we can understand you are doing wrong?
Here's the beginning of the other trigger. I won't post the whole thing just yet, as it's rather long.
Delegator is a custom object that links to two Contact records through lookup relationships - one is the Delegator, one is the Delegate.