+ Start a Discussion
Mr.BrooksMr.Brooks 

SObject row was retrieved via SOQL without querying the requested field: Account.Opportunities

Can someone please assist. I am trying to query for ALL Opportunities and trying to make all the Opp owners sync up with the Account owner:

global class DealerDealOwnerSync implements Database.Batchable<sObject>{
   
    String jbrooks = [SELECT Alias, Id FROM User where alias = 'jbrook7'].id;
    String api = [SELECT Alias, Id FROM User where alias = 'SFAPI'].id;
    String wendy = [SELECT Alias, Id FROM User where alias = 'wkrame1'].id;
    String dwetl = [SELECT Alias, Id FROM User where alias = 'DWETL'].id;
    String ActRecTypeID = [SELECT Name, Id, DeveloperName, SobjectType FROM RecordType where SobjectType = 'Account' and Name = 'Activated'].Id;
    Integer iSize = 0;
   
   
    //TESTING FROM A BUTTON   
    public DealerDealOwnerSync(ApexPages.StandardController stdController)
    {        
      //clsContactOwnerSync();     

    }
   
    public DealerDealOwnerSync()
    {        

 
    }
   
    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 : [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 = :ActRecTypeID 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']){
            Account a = (Account)obj.getSObject('Account');
            /*Opportunity o;
            Account a;*/
            Opportunity[] oppOne = a.getSObjects('Opportunities');
            if(oppOne != null){
                for(Opportunity o: a.opportunities){
                    if(o.OwnerID != a.OwnerID){ 
                        Opportunity mOpportunity = new Opportunity();
                        mOpportunity.Id = o.Id;
                        mOpportunity.OwnerID = a.OwnerID;
                        dealsToUpdate.add(mOpportunity);
                       
                        iSize = iSize + 1;
                        system.debug('**************ISIZE:' +iSize);
                       
                        if(iSize == 1000)
                        {
                            update dealsToUpdate;
                            System.debug('********UPDATE COUNT: ' + iSize);
                            dealsToUpdate = new List<Opportunity>();
                            iSize = 0;
                        }             
                    }
                }
             }
          }
        
             if(iSize < 1000)
             {
                 update dealsToUpdate;
                 System.debug('********UPDATE COUNT: ' + iSize);
                 dealsToUpdate = new List<Opportunity>();
                 iSize = 0;
             }
       
    }

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


Mr.BrooksMr.Brooks
I changed it to this, and now it runs through every record but it does not seem to be chagning the ones with different owners. Help with this if you can please: 


global void execute(Database.BatchableContext BC, List<sObject> scope){
        List<Opportunity> dealsToUpdate = new List<Opportunity>();
        for(sObject obj : [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 = :ActRecTypeID 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' limit 20000]){
            Account a = (Account)obj.getSObject('Account');
            /*Opportunity o;
            Account a;*/
            //Opportunity[] oppOne = a.getSObjects('Opportunities');
            //if(oppOne != null){
                for(Opportunity o: a.opportunities){
                    if(o.OwnerID != a.OwnerID){ 
                        Opportunity mOpportunity = new Opportunity();
                        mOpportunity.Id = o.Id;
                        mOpportunity.OwnerID = a.OwnerID;
                        dealsToUpdate.add(mOpportunity);
                       
                        iSize = iSize + 1;
                        system.debug('**************ISIZE:' +iSize);
                       
                        if(iSize == 1000)
                        {
                            update dealsToUpdate;
                            System.debug('********UPDATE COUNT: ' + iSize);
                            dealsToUpdate = new List<Opportunity>();
                            iSize = 0;
                        }             
                    }
                }
             //}
          }
        
             if(iSize < 1000)
             {
                 update dealsToUpdate;
                 System.debug('********UPDATE COUNT: ' + iSize);
                 dealsToUpdate = new List<Opportunity>();
                 iSize = 0;
             }
       
    }