Skip to main content
I have a case trigger to count the number of related solutions but, I am receiving a SOQL number of quiries error on an import via data loader.  Therefore, I appear to have an issue with bulkifiying this trigger;

Here is my Trigger;

trigger NumberOfSolutions on case(before update) {

if(Trigger.isUpdate)

{

List<Case> objCase= new List<Case>();

for(Case c:Trigger.New)

{

Integer intNum = 0;

intNum =[select Count() from casesolution where caseID =:c.id];

c.Number_of_Solutions__c =intNum;

objCase.add(c);

}

}

}

Any help would be greatly appreciated.

Joseph
5 个回答
  1. 2015年2月17日 05:10
    Hi Joseph,

    Refer the above links provided to bulkify the triggers.

    And for your reference

    trigger NumberOfSolutions on case(before update) {

            Map<Id,Integer> caseWithCaseSoltns = new Map<Id,Integer>();

            for(Case cas : [SELECT Id,(select Id from casesolutions) FROM Case where Id IN :Trigger.newMap.keySet()]){

                caseWithCaseSoltns.put(cas.Id,cas.casesolutions.size());    

            }

            

            for(Case c:Trigger.New){

                c.Number_of_Solutions__c = caseWithCaseSoltns.get(c.Id);

            }

    }

    Regards,

    Bhanu Mahesh