You need to sign in to do that
Don't have an account?

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:
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
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.
Code:
Code: 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