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
JohnPCutlerJohnPCutler 

SOQL For Loops ...

Hello. I am making some progress with Apex, but I am stumped on the following. My understanding is that you can use SOQL for loops to do batch update / delete (200 records at a time). In my example below, I looping once for each record. When I try to adapt the code based on the docs (Apex Language Reference), I get an error saying I need an SObject List object. Any help would be appreciated ...   how do I convert this SOQL for loop to do batch processing?

And yes, I realize that there is a Case Contact Roles related list, but the client requires related lists on the Account and Contact objects displaying Case roles. :smileyhappy:

Code:
// we check to see if the subject has changed
if (casenew.subject != caseold.subject) {

  // we try to batch update all case_roles with this case id
  // this seems to do one at a time, instead of 200 at a time
  for(case_role__c cr: [select id,case_subject__c from case_role__c where case__c = :casenew.id ]){
    cr.case_subject__c = casenew.subject;
    update cr;
  } 
}
Message Edited by JohnPCutler on 08-25-200703:48 PM

Message Edited by JohnPCutler on 08-26-200707:04 PM

mtbclimbermtbclimber
I think some of your code may have been cut off by the post editor. Can you re-post your code and use the SRC button on the toolbar so we are sure you posted a complete sample?

Generally speaking, yes you can use SOQL for loops to process chunks but keep in mind that in a trigger you should not have any query or DML(insert/update/etc.) statements inside of your loop otherwise you will hit governor limits during bulk operations - most commonly API-based data loading or integration tasks for the case object.
JohnPCutlerJohnPCutler
This is the section I was having a tough time with. My understanding is that I could put brackets after "case_role__c[]" and this would allow me to process all case roles at once.
sdudasduda
Here is my understanding on how to do bulk update with the for loop:


Code:
// we check to see if the subject has changed
if (casenew.subject != caseold.subject) {

  // we try to batch update all case_roles with this case id
  // this seems to do one at a time, instead of 200 at a time
  for( List<case_role__c> caseRoleList : [select id,case_subject__c from case_role__c where case__c = :casenew.id ]){
     for( case_role__c caseold : caseRoleList ) {
        caseold.case_subject__c = casenew.subject;
     } 
    update caseRoleList;
  } 
}

 

mtbclimbermtbclimber
Assuming you are inside of a loop that contains more than 20 records where the subject changed you will hit governor limits with this pattern as you are doing both a query and an update within the loop.  Remember, keep your queries and DML operations outside of loops - especially when there may be more than 20 rows.
JohnPCutlerJohnPCutler
Sorry, I should have posted the whole trigger, so you could understand the context ...

Code:
trigger updateOrDelteCaseRoles on Case (after update,before delete) {
  
  // first, we need to change the subjects of all case roles if the
  // cases subject value has changes
  if (Trigger.isUpdate) {
    for (Integer i = 0; i < Trigger.new.size();i++){
      
      Case casenew = Trigger.new[i];
      Case caseold = Trigger.old[i];

      // we check to see if the subject has changed
      if (casenew.subject != caseold.subject) {
        for(case_role__c cr: [select id,case_subject__c from case_role__c where case__c = :casenew.id ]){
          cr.case_subject__c = casenew.subject;
          update cr;
        }  
      }
    }          
  }
  if (Trigger.isDelete) {
    for (Integer i = 0; i < Trigger.old.size();i++){
      Case casenew = Trigger.old[i];
      for(case_role__c cr: [select id from case_role__c where case__c = :casenew.id ]){
       delete cr;
      }
    } 
  }
}
In plain English

When a case (or cases) are updated ...

1) Check to see if the subject has changed
2) If the subject has changed find all child Case Roles
3) Update the Case Subject field for each child Case Role

When the case is deleted

1) Delete all child Case Roles
 

Message Edited by JohnPCutler on 08-27-200709:49 AM