0
Global class UpdateAccountAddressBatch implements Database.Batchable<SObject>  
{
    
    Global Database.QueryLocator start(Database.BatchableContext bc)
    {
        string accquery='select id, name, 
       BillingAddress, ShippingAddress, 
        (select id, CheckBox__c, 
       lastname,othercity, othercountry, 
         otherpostalcode, otherstate, 
         otherstreet, SeasonalEndDate__c, 
       SeasonalStartDate__c, MailingAddress 
     from contacts where CheckBox__c=false) 
     from Account';
 return database.getQueryLocator(accquery);
    }


  Global void Execute(Database.BatchableContext 
         bc,List<Sobject> recordsToProcess)
    {
  if(!recordsToProcess.isEmpty()){
List<Account> acclist = new List<Account>();
      for(Sobject sobj : recordsToProcess){
             Account acc = (Account)sobj;
             For(contact con :acc.contacts){
if(con.SeasonalStartDate__c <= system.Today() && system.Today() <= con.SeasonalEndDate__c){
                    acc.BillingCity=con.OtherCity;
                    acc.BillingCountry=con.OtherCountry;
                    acc.BillingPostalCode=con.OtherPostalCode;
                    acc.BillingState=con.OtherState;
                    acc.BillingStreet=con.OtherStreet;
                    acc.ShippingCity=con.OtherCity;
                    acc.ShippingCountry=con.OtherCountry;
                    acc.ShippingPostalCode=con.OtherPostalCode;
                    acc.ShippingState=con.OtherState;
                    acc.ShippingStreet=con.OtherStreet;
                    acclist.add(acc);
                }}
                if(!acclist.isEmpty()){
                    update acclist;
                    List<contact> conlist = new List<contact>();
                    for(Account account : acclist){
                        for(contact con1:account.contacts){
                            con1.CheckBox__c=true;
                            conlist.add(con);
                        }
                    }
                   update conlist; 
                }
            }
        }
        
    }
}


    Global void Finish(Database.BatchableContext bc)
    {
        AsyncApexJob jobDetails =[SELECT id, status, jobItemsProcessed, numberOfErrors, createdBy.Email from AsyncApexJob where id=:bc.getJobId()];
        
     }

2
  • Welcome to SFSE. Please take a moment to read How to Ask and take the tour. Commented Jul 6, 2022 at 18:53
  • Please edit your post to give the specific line that is throwing the error. Also, why are you looping through accounts and contacts twice? Commented Jul 7, 2022 at 13:43

1 Answer 1

1

The error is pretty clear and is on this line:

Contact con = (Contact)sobj;

You are trying to cast an account SObject record into a contact.

To access the contacts, you need to get the list through the account using dot-notation and the relationship name

Account acc = (Account)sobj;
for (Contact con : acc.Contacts) {
    ... your code here ...
}
3
  • I tried your logic, but still, it shows the same error Commented Jul 7, 2022 at 3:36
  • Did you remove the invalid line that tries to cast the contact? Please edit your original update to include what you have tried. Commented Jul 7, 2022 at 5:14
  • Yes, I did that. Please see the updated code Commented Jul 7, 2022 at 10:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .