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
Mr.BrooksMr.Brooks 

Invalid conversion from runtime type SOBJECT:Opportunity to SOBJECT:Account

I do not know why i am getting this error, here is my code, help please:

global class DealerDealOwnerSync implements Database.Batchable<sObject>{
    //TESTING FROM A BUTTON
    public DealerDealOwnerSync(ApexPages.StandardController stdController)
    {        
      //clsContactOwnerSync();
     

    }
     public DealerDealOwnerSync()
   {        

 
   }
  
    String wendy = [SELECT Alias, Id FROM User where alias = 'wkrame1'].id;
    String dwetl = [SELECT Alias, Id FROM User where alias = 'DWETL'].id;

    global Database.QueryLocator start(Database.BatchableContext BC)
    {
      return Database.getQueryLocator('Select Id, OwnerId, Name, CloseDate,Account.OwnerId, Account.Id,Account.Owner.IsActive,Account.Active__c, Account.Name, Account.GMF_ACF__c from Opportunity where stagename != \'DECLINE,WITHDRW\' and Account.recordtypeid = \'01230000000DD54AAG\' and account.owner.House_Account__c = false and account.owner.isActive = true and Account.LastModifiedDate = Last_Month and Account.LastModifiedById = :wendy and Account.Active__c = \'Y\''); // Select Id, OwnerId, Name, CloseDate,Account.OwnerId, Account.Id,Account.Owner.IsActive,Account.Active__c, Account.Name, Account.GMF_ACF__c from Opportunity where stagename != \'DECLINE,WITHDRW\' and Account.recordtypeid = \'01230000000DD54AAG\' and account.owner.House_Account__c = false and account.owner.isActive = true and Account.LastModifiedDate = Last_Month and Account.LastModifiedById = :wendy and Account.Active__c = \'Y\'
    }
   
    global void execute(Database.BatchableContext BC, List<sObject> scope){
        List<Opportunity> dealsToUpdate = new List<Opportunity>();
        for(sObject obj : scope){
            Account a = (Account)obj;
            /*Opportunity o;
            Account a;*/
            Opportunity[] oppOne = a.getSObjects('Opportunities');
            if(oppOne != null){
                for(Opportunity o: oppOne){
                    if(o.OwnerID != a.OwnerID){ 
                        Opportunity mOpportunity = new Opportunity();
                        mOpportunity.Id = o.Id;
                        mOpportunity.OwnerID = a.OwnerID;
                        dealsToUpdate.add(mOpportunity);             
                    }
                }
             }
         }
         if(dealsToUpdate.size()>1000)
         {
             update dealsToUpdate;
         }
         if(dealsToUpdate.size()>0)
         {
             update dealsToUpdate;
         }
    }

    global void finish(Database.BatchableContext BC)
    {
        //ID d = Database.executeBatch(new DealerDealOwnerSync(),100);
    }
   
   
}
Marty C.Marty C.

Hello, Mr.Brooks, it looks like the error probably occurs in the first line of the for loop, where you're trying to cast the object in scope as an Account:

Account a = (Account)obj;

The problem here is that the query you used in start() is returning Opportunities, not Accounts, as seen from the "from Opportunity" clause in your query. So, what's likely happening is:

  1. Your query returns a list of Opportunities
  2. Your for loop goes through each of the Opportunities returned
  3. Your code tries to cast each returned Opportunity as an Account, which is causing the error


Based on what I see in your query, it looks like what you really want is the related Account for each Opportunity. Try making the following modification to your code:

Account a = (Account)obj.getSObject('Account');