0

After adding my below code when I run test class on the whole trigger it is giving too many soql:101 error on some other lines. After removing the code it works fine. I am unable to figure out what is wrong with below code.

 If(trigger.isAfter && trigger.isUpdate){
        //RecordType rt = [select Id from RecordType where Name = 'Parent Child' and SobjectType = 'Case' limit 1];
        Set<id> ParentCases = new set<id>();
        List<case> CasetoUpdate = new List<Case>();
        for(case caseObj : Trigger.new){
                if((CaseObj.Status!=Trigger.oldMap.get(caseObj.id).status || caseObj.OwnerId!=Trigger.oldMap.get(CaseObj.id).ownerId)){
                    ParentCases.add(caseObj.Id);
                }
            system.debug('parentcases list -----------'+Parentcases);               
        }      

       if(!ParentCases.isEmpty()){
        //List<case> ChildCase = new List<case>([select id, RepeatedCase__c,Repeated_Case_owner__c, Repeated_Status__c, RepeatedCase__r.status, RepeatedCase__r.owner.name from case where RepeatedCase__c IN : Trigger.new AND RepeatedCase__c!=null AND Repeated_case_owner__c!=NULL AND Repeated_status__c!=NULL]);
            for(case CaseObj1 : [select id, RepeatedCase__c,Repeated_Case_owner__c, Repeated_Status__c, RepeatedCase__r.status, RepeatedCase__r.owner.name from case where RepeatedCase__c IN : Trigger.new AND RepeatedCase__c!=null AND Repeated_case_owner__c!=NULL AND Repeated_status__c!=NULL]){
                caseObj1.Repeated_Case_Owner__c = caseObj1.RepeatedCase__r.Owner.name;
                caseObj1.Repeated_Status__c = caseObj1.RepeatedCase__r.status;
                caseToupdate.add(caseObj1);
            }
        }
      update caseToUpdate;
     }
1

We had other utility class which had a boolean variable of parent case being open or closed. Checked the boolean variable and it worked.

If(trigger.isAfter && trigger.isUpdate){
    //RecordType rt = [select Id from RecordType where Name = 'Parent Child' and SobjectType = 'Case' limit 1];
    Set<id> ParentCases = new set<id>();
    List<case> CasetoUpdate = new List<Case>();
    for(case caseObj : Trigger.new){
            if((CaseObj.Status!=Trigger.oldMap.get(caseObj.id).status || caseObj.OwnerId!=Trigger.oldMap.get(CaseObj.id).ownerId)){
                ParentCases.add(caseObj.Id);
            }
        system.debug('parentcases list -----------'+Parentcases);               
    }      

   if(!ParentCases.isEmpty()){
    //List<case> ChildCase = new List<case>([select id, RepeatedCase__c,Repeated_Case_owner__c, Repeated_Status__c, RepeatedCase__r.status, RepeatedCase__r.owner.name from case where RepeatedCase__c IN : Trigger.new AND RepeatedCase__c!=null AND Repeated_case_owner__c!=NULL AND Repeated_status__c!=NULL]);
        for(case CaseObj1 : [select id, RepeatedCase__c,Repeated_Case_owner__c, Repeated_Status__c, RepeatedCase__r.status, RepeatedCase__r.owner.name from case where RepeatedCase__c IN : Trigger.new AND RepeatedCase__c!=null AND Repeated_case_owner__c!=NULL AND Repeated_status__c!=NULL]){
            caseObj1.Repeated_Case_Owner__c = caseObj1.RepeatedCase__r.Owner.name;
            caseObj1.Repeated_Status__c = caseObj1.RepeatedCase__r.status;
            caseToupdate.add(caseObj1);
        }
    }
  update caseToUpdate;
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.