You need to sign in to do that
Don't have an account?
JosephT
Bulkify Case Trigger.
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
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
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
All Answers
https://developer.salesforce.com/trailhead/force_com_programmatic_beginner/apex_triggers/apex_triggers_bulk
trigger NumberOfSolutions on case(before update) {
Integer intNum = 0;
Set<ID> ids = Trigger.newMap.keySet();
intNum=[select count() from casesolutions where caseID in :ids];
for(Case c: trigger.new)
{
c.Number_of_Solutions__c =intNum;
}
}
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
Joseph