+ Start a Discussion
BroncoBoyBroncoBoy 

Question Regarding Casting Within Batch Apex

I am new to apex programming and have been asked to look into an issue within a batch apex class created by an senior apex developer (s.a.d.). 

 

The setup:

 

My understanding of the execute method used with batch apex is that this method takes 2 things:

-A reference to the Database.BatchableContext object.
-A list of sObjects, such as List<sObject>, which is passed to the method for each batch of records

 

You can see both being passed into the s.a.d.'s code below:

 

The code:

 

global void execute(Database.BatchableContext BC, List<sObject> scope)
    {
        Set<String> branchZips = new Set<String>();
        Set<Id> branchPIds = new Set<Id>();

        
        // First, we collect all the unique zips from the branches into a set.
        for (sObject s : scope) {   
            Account a = (Account)s;
            if (a.Branch_Office_Zip_Code__c != null && a.Branch_Office_Zip_Code__c.length() == 5) {
                branchZips.add(a.Branch_Office_Zip_Code__c);
                branchPIds.add(a.ParentId);
            }
        }

 

My question:

 

Regarding list of sObjects (List<sObject> scope) being passed to the method I'm simply trying to understand what the developer is doing within the code.  My question:  Does it appear to you that the developer was passing list of sObjects in a variable "scope", taking each record from the  "scope", storing it in an sObject "s" and then casting that "s" sObject into an account object "a" with this code:  Account a = (Account)s?  If so, I've been trying to find information regarding this casting "technique" and why it's used and, more importantly, other examples.  This whole topic of casting appears to be widly used not just in apex programming but in other programming like java,  and I would like to understand it better.  I have been unable to find resources that clearly explain what it is and why you would do it and examples.   Thanks in advance!

Best Answer chosen by Admin (Salesforce Developers) 
ForcepowerForcepower
BroncoBoy,

The execute method is a standard for your batch classes that implement Database.Batchable.

When Salesforce batch executes and sends up to 400 records at a time to process, it will create the "List<sObject> scope" and fills it with the records obtained from the query you set up. Making scope a list of SObjects means you can pass in any object in, in that list and then cast it to the right object to work with for the field details. In this case since you're dealing with Account objects, you need to cast it to Account to be able to access fields such as Branch_Office_Zip_Code__c. Hope that helps.
Ram

All Answers

ForcepowerForcepower
BroncoBoy,

The execute method is a standard for your batch classes that implement Database.Batchable.

When Salesforce batch executes and sends up to 400 records at a time to process, it will create the "List<sObject> scope" and fills it with the records obtained from the query you set up. Making scope a list of SObjects means you can pass in any object in, in that list and then cast it to the right object to work with for the field details. In this case since you're dealing with Account objects, you need to cast it to Account to be able to access fields such as Branch_Office_Zip_Code__c. Hope that helps.
Ram
This was selected as the best answer
BroncoBoyBroncoBoy

Yes that helped greatly, that's exactly the explanation I was hoping to get - the light bulb is now on.  Thanks!

ForcepowerForcepower
You're very welcome!
Ram