• Ashley Cobb 24
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
Hello!

I am trying to upload new code, and am coming up with the error "System.LimitException: Too many SOQL queries: 101 
Stack Trace: Class.RollUpSummaryUtility.rollUpTrigger: line 83, column 1".  I am a very new Developer, but do not understand why I am getting the error.  It seems to be the bolded area. Any help would be appreciated!!


public class RollUpSummaryUtility {
   

    public class fieldDefinition {
        public String operation {get;set;}
        public String childField {get;set;}
        public String parentField {get;set;}
         
        public fieldDefinition (String o, String c, String p) {
            operation = o;
            childField = c;
            parentField = p;
        }
    }
     
    public static void rollUpTrigger(list<fieldDefinition> fieldDefinitions, list<sObject> records, String childObject, String childParentLookupField, String parentObject, String queryFilter) {
         
        //Limit the size of list by using Sets which do not contain duplicate elements prevents hitting governor limits.
        set<Id> parentIds = new set<Id>();
         
        for(sObject s : records) {
            parentIds.add((Id)s.get(childParentLookupField));
        }
         
        //Populate query text strings to be used in child aggregrator and parent value assignment.
        String fieldsToAggregate = '';
        String parentFields = '';
         
        for(fieldDefinition d : fieldDefinitions) {
            fieldsToAggregate += d.operation + '(' + d.childField + ') ' + ', ';
            parentFields += d.parentField + ', ';
        }
         
        //Using dynamic SOQL with aggergate results to populate parentValueMap.
        String aggregateQuery = 'Select ' + fieldsToAggregate + 
        childParentLookupField + ' from ' + childObject + ' where  ' + 
        childParentLookupField + ' IN :parentIds ' + queryFilter + ' ' +
        ' group by ' + childParentLookupField;
         
        //Map will contain one parent record Id per one aggregate object.
        map<Id, AggregateResult> parentValueMap = new map <Id, AggregateResult>();
         
        for(AggregateResult q : Database.query(aggregateQuery)){
            parentValueMap.put((Id)q.get(childParentLookupField), q);
        }
         
        //List of parent object records to update.
        list<sObject> parentsToUpdate = new list<sObject>();
         
        String parentQuery = 'select ' + parentFields + ' Id ' + ' from ' + parentObject + ' where Id IN :parentIds';
         
        //For each affected parent object, retrieve aggregate results and for each field definition add aggregate value to parent field.
        for(sObject s : Database.query(parentQuery)) {
             
            Integer row = 0; //row counter reset for every parent record
            
            for(fieldDefinition d : fieldDefinitions) {
                String field = 'expr' + row.format();
                AggregateResult r = parentValueMap.get(s.Id);
                //r will be null if no records exist (e.g. last record deleted)
                if(r != null) { 
                    Decimal value = ((Decimal)r.get(field) == null ) ? 0 : 
                        (Decimal)r.get(field);
                    s.put(d.parentField, value);
                } else {
                    s.put(d.parentField, 0);
                }
                row += 1; //plus 1 for every field definition after first
            }
            parentsToUpdate.add(s);
        }
         

        //Ff parent records exist, perform update of all parent records with a single DML statement.
        if(parentsToUpdate.Size() > 0) {
            update parentsToUpdate;
        }
         
    }
 
}
Hello!

I am trying to upload new code, and am coming up with the error "System.LimitException: Too many SOQL queries: 101 
Stack Trace: Class.RollUpSummaryUtility.rollUpTrigger: line 83, column 1".  I am a very new Developer, but do not understand why I am getting the error.  It seems to be the bolded area. Any help would be appreciated!!


public class RollUpSummaryUtility {
   

    public class fieldDefinition {
        public String operation {get;set;}
        public String childField {get;set;}
        public String parentField {get;set;}
         
        public fieldDefinition (String o, String c, String p) {
            operation = o;
            childField = c;
            parentField = p;
        }
    }
     
    public static void rollUpTrigger(list<fieldDefinition> fieldDefinitions, list<sObject> records, String childObject, String childParentLookupField, String parentObject, String queryFilter) {
         
        //Limit the size of list by using Sets which do not contain duplicate elements prevents hitting governor limits.
        set<Id> parentIds = new set<Id>();
         
        for(sObject s : records) {
            parentIds.add((Id)s.get(childParentLookupField));
        }
         
        //Populate query text strings to be used in child aggregrator and parent value assignment.
        String fieldsToAggregate = '';
        String parentFields = '';
         
        for(fieldDefinition d : fieldDefinitions) {
            fieldsToAggregate += d.operation + '(' + d.childField + ') ' + ', ';
            parentFields += d.parentField + ', ';
        }
         
        //Using dynamic SOQL with aggergate results to populate parentValueMap.
        String aggregateQuery = 'Select ' + fieldsToAggregate + 
        childParentLookupField + ' from ' + childObject + ' where  ' + 
        childParentLookupField + ' IN :parentIds ' + queryFilter + ' ' +
        ' group by ' + childParentLookupField;
         
        //Map will contain one parent record Id per one aggregate object.
        map<Id, AggregateResult> parentValueMap = new map <Id, AggregateResult>();
         
        for(AggregateResult q : Database.query(aggregateQuery)){
            parentValueMap.put((Id)q.get(childParentLookupField), q);
        }
         
        //List of parent object records to update.
        list<sObject> parentsToUpdate = new list<sObject>();
         
        String parentQuery = 'select ' + parentFields + ' Id ' + ' from ' + parentObject + ' where Id IN :parentIds';
         
        //For each affected parent object, retrieve aggregate results and for each field definition add aggregate value to parent field.
        for(sObject s : Database.query(parentQuery)) {
             
            Integer row = 0; //row counter reset for every parent record
            
            for(fieldDefinition d : fieldDefinitions) {
                String field = 'expr' + row.format();
                AggregateResult r = parentValueMap.get(s.Id);
                //r will be null if no records exist (e.g. last record deleted)
                if(r != null) { 
                    Decimal value = ((Decimal)r.get(field) == null ) ? 0 : 
                        (Decimal)r.get(field);
                    s.put(d.parentField, value);
                } else {
                    s.put(d.parentField, 0);
                }
                row += 1; //plus 1 for every field definition after first
            }
            parentsToUpdate.add(s);
        }
         

        //Ff parent records exist, perform update of all parent records with a single DML statement.
        if(parentsToUpdate.Size() > 0) {
            update parentsToUpdate;
        }
         
    }
 
}
Hey all!

Looking for some assistance with a trigger that would be used to count the total number of cases that are listed on a custom object we've built.  Trucks_Under_Warranty__c is the custom object we have built. 

Under Cases we have the field "Truck Under Warranty__c" which is a lookup(Trucks Under Warranty) Indexed and its controlling field is Account Name.  Gets a little confusing for the "Truck/Trucks" in the field names.  
Case field TUW

The field used to hold the count is named Case Count (Case_Count__c).  I could use some direction on the trigger due to it being on this custom object.  We're wanting a way to designate if a truck is problematic or not based upon how many cases are generated under it.

I had been trying to look at a snippit of code someone posted for counting cases on an contact to understand it and translate it to our ORG
trigger tskUpdatetrigger on Case (after insert, after update,after delete) {
    List<Case> lstCase = [select id, ContactId from Case where id in: trigger.newmap.keyset()];
    set<Id> sAccId = new set<Id>();

    for(Case cs: lstCase){
        if(cs.ContactId != null){
            sAccId.add(cs.ContactId);
        }
    }
    if(sAccId != null && sAccId.size() > 0){
        List<Contact> lstContact = [select id, Cases__c, (select id from Cases) from Contact where id in: sAccId];
        if(lstContact.size() > 0){
            for(Contact acc: lstContact){
                acc.Cases__c = acc.Cases.size();
            }
                      update lstContact;
            }
    }
}

But i'm not sure what to change "ContactId" to and it work with our custom object.  I've also tried to read through sfdcfox's example ( https://developer.salesforce.com/forums/?id=906F00000008zIxIAI ) but it is still a little out of my apex knowledge base.  

TLDR: Would love some help with writing a trigger to count the number of cases on a custom object