You need to sign in to do that
Don't have an account?
Is the SOQL Query limit organization wide? Roll Up Summary Trigger Too Many SOQL Queries
trigger AppMembershipTrigger on App_Membership__c (after delete, after insert, after update, after undelete) { Set<Id> stContactIds = new Set<Id>(); for(App_Membership__c member :Trigger.isDelete ? Trigger.Old : Trigger.New) { if(member.Contact__c != null) { stContactIds.add(member.Contact__c); } if(Trigger.isUpdate && Trigger.oldMap.get(member.id).Contact__c != null) { stContactIds.add(Trigger.oldMap.get(member.id).Contact__c); } } //APP OWNER UPDATE List<Contact> lstContactOwner = [Select Id,(Select Id from App_Memberships__r where Role__c = 'App Owner' AND App__r.Current_MAU__c > 150) FROM Contact WHERE Id IN :stContactIds]; for(Contact contact : lstContactOwner) { contact.Number_of_Apps_Owner__c = contact.App_Memberships__r.size(); } //APP MEMBER UPDATE List<Contact> lstContactMember = [Select Id,(Select Id from App_Memberships__r where Role__c <> 'App Owner' AND App__r.Current_MAU__c > 150) FROM Contact WHERE Id IN :stContactIds]; for(Contact contact : lstContactMember) { contact.Number_of_Apps_Member__c = contact.App_Memberships__r.size(); } //IOS APPS UPDATE List<Contact> lstiOSApps = [Select Id,(Select Id from App_Memberships__r where Platform__c = 'ios') FROM Contact WHERE Id IN :stContactIds]; for(Contact contact : lstiOSApps) { contact.Num_of_iOS_Apps__c = contact.App_Memberships__r.size(); } //DROID APPS UPDATE List<Contact> lstDroidApps = [Select Id,(Select Id from App_Memberships__r where Platform__c = 'android') FROM Contact WHERE Id IN :stContactIds]; for(Contact contact : lstDroidApps) { contact.Num_of_Droid_Apps__c = contact.App_Memberships__r.size(); } //UPDATE CONTACT OWNER if(!lstContactOwner.isEmpty()) { update lstContactOwner; } //UPDATE CONTACT MEMBER if(!lstContactMember.isEmpty()) { update lstContactMember; } //UPDATE IOS APPS if(!lstiOSApps.isEmpty()) { update lstiOSApps; } //UPDATE DROID APPS if(!lstDroidApps.isEmpty()) { update lstDroidApps; } }
Failure Message: "System.LimitException: Too many SOQL queries: 101", Failure Stack Trace: "Trigger.AppMembershipTrigger: line 38, column 1"
I've pasted the code and the error message above. This would be much easier as a real Roll-Up Summary, but unfortunately we can't make this a Master-Detail Relationship because the Contact__c field is sometimes blank on the child object (App_Membership__c <--itself a junction object to App__c ).
The trigger works just fine in sandbox ``updating 4 fields on the parent records (Contact). However, when I deploy to production I get the error highlighted in red.
Is there a different/better way to do this that anyone knows of?
Is the SOQL Query limit trigger specific or org-wide?
All Answers
That's helpful. Thank you.
Does anyone know if there's a way to accomplish this is 1 query instead of 4?