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
pavan kumar 1864pavan kumar 1864 

i have written a batch class but when i try to execute the error is - SObject row was retrieved via SOQL without querying the requested field: Opportunity.AccountId---showing up -please guide

public class batchjobassignment implements Database.batchable<sObject>
{
    
    public  Database.querylocator start(Database.batchablecontext bc)
    {
        string s= 'select id from opportunity where StageName = \'Closed Won\'';
        return (Database.getquerylocator(s));
        // system.debug(s);
    }
    public  void execute(Database.batchablecontext bc,list<opportunity>opp)
    {    set<id>oppid = new set<id>();
     for(opportunity op : opp)
     {
         oppid.add(op.accountid);            
     }
     list<account> acc = [select id, name, Latest_owner__c,(select id, OwnerId from Opportunities) from account where id in :oppid];
     
     list<account>acountnew = new list<account>();
     
     for(account aa : acc)
     {
         for(opportunity oo : aa.Opportunities){
             
             aa.Latest_owner__c = oo.OwnerId;
             acountnew.add(aa);
         }
     }
     update acountnew;
    }
    public  void finish(Database.batchablecontext bc){
        
    }
}
Best Answer chosen by pavan kumar 1864
CharuDuttCharuDutt
Hii Pavan Kumar
Try Below Code Added  AccountId  in Query Which Is In Bold 
public class batchjobassignment implements Database.batchable<sObject>
{
    
    public  Database.querylocator start(Database.batchablecontext bc)
    {
        string s= 'select id,AccountId from opportunity where StageName = \'Closed Won\'';
        return (Database.getquerylocator(s));
        // system.debug(s);
    }
    public  void execute(Database.batchablecontext bc,list<opportunity>opp)
    {    set<id>oppid = new set<id>();
     for(opportunity op : opp)
     {
         oppid.add(op.accountid);            
     }
     list<account> acc = [select id, name, Latest_owner__c,(select id, OwnerId from Opportunities) from account where id in :oppid];
     
     list<account>acountnew = new list<account>();
     
     for(account aa : acc)
     {
         for(opportunity oo : aa.Opportunities){
             
             aa.Latest_owner__c = oo.OwnerId;
             acountnew.add(aa);
         }
     }
     update acountnew;
    }
    public  void finish(Database.batchablecontext bc){
        
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

 

All Answers

AbhinavAbhinav (Salesforce Developers) 
Hi 

You are using oppid.add(op.accountid);
Please inculde accountId in your query as field.

Something similar
 string s= 'select id,accountId from opportunity where StageName = \'Closed Won\'';


If it helps please mark it as best answer


Thank
CharuDuttCharuDutt
Hii Pavan Kumar
Try Below Code Added  AccountId  in Query Which Is In Bold 
public class batchjobassignment implements Database.batchable<sObject>
{
    
    public  Database.querylocator start(Database.batchablecontext bc)
    {
        string s= 'select id,AccountId from opportunity where StageName = \'Closed Won\'';
        return (Database.getquerylocator(s));
        // system.debug(s);
    }
    public  void execute(Database.batchablecontext bc,list<opportunity>opp)
    {    set<id>oppid = new set<id>();
     for(opportunity op : opp)
     {
         oppid.add(op.accountid);            
     }
     list<account> acc = [select id, name, Latest_owner__c,(select id, OwnerId from Opportunities) from account where id in :oppid];
     
     list<account>acountnew = new list<account>();
     
     for(account aa : acc)
     {
         for(opportunity oo : aa.Opportunities){
             
             aa.Latest_owner__c = oo.OwnerId;
             acountnew.add(aa);
         }
     }
     update acountnew;
    }
    public  void finish(Database.batchablecontext bc){
        
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

 
This was selected as the best answer
Suraj Tripathi 47Suraj Tripathi 47
Hi Pavan,
Check this code it will help you. 
public class batchjobassignment implements Database.batchable<sObject>
{
    
    public  Database.querylocator start(Database.batchablecontext bc)
    {
        string s= 'select id,AccountId from opportunity where StageName = \'Closed Won\'';
        return (Database.getquerylocator(s));
        // system.debug(s);
    }
    public  void execute(Database.batchablecontext bc,list<opportunity>opp)
    {    set<id>oppid = new set<id>();
     for(opportunity op : opp)
     {
         oppid.add(op.accountid);            
     }
     list<account> acc = [select id, name, Latest_owner__c,(select id, OwnerId from Opportunities) from account where id in :oppid];
     
     list<account>acountnew = new list<account>();
     
     for(account aa : acc)
     {
         for(opportunity oo : aa.Opportunities){
             
             aa.Latest_owner__c = oo.OwnerId;
             acountnew.add(aa);
         }
     }
     update acountnew;
    }
    public  void finish(Database.batchablecontext bc){
        
    }
}
If you find your Solution then mark this as the best answer. 

Thank you!

Regards 
Suraj Tripathi