function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JamuraiJamurai 

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?

 

Best Answer chosen by Jamurai
Bhawani SharmaBhawani Sharma
This is per transaction limit. This is standard limit from salesforce. You can't fire more than 100 SOQL queries per transaction. I think, your current trigger is causing other triggers to be get executed and you are hitting limit issue.

All Answers

Bhawani SharmaBhawani Sharma
This is per transaction limit. This is standard limit from salesforce. You can't fire more than 100 SOQL queries per transaction. I think, your current trigger is causing other triggers to be get executed and you are hitting limit issue.
This was selected as the best answer
JamuraiJamurai

That's helpful. Thank you.

 

Does anyone know if there's a way to accomplish this is 1 query instead of 4?