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
hiteshwar marnihiteshwar marni 

batch apex error1

I'm writing below batch apex to transfer the accounts of particualr user to another user
bu it's throwing error as " EXCEPTION_THROWN [7]|System.QueryException: unexpected token: 'Eo3Z' ".
Thanks

global class accountownerhangebatch implements database.batchable<sobject>{
        
    global database.querylocator start(database.batchablecontext bc){
   string query = 'select Name,Ownerid from account where ownerid = 00528000001Eo3Z' ;
    return database.getquerylocator(query);
    }
    
    global void execute (database.batchablecontext bc, list<Account> scope){
    id oid = 'SELECT Id,name FROM User WHERE name = marni user1';
        for(account a : scope){
        
        a.Ownerid= oid;
        system.debug('---->'+a.Ownerid);
              
        }
          update scope;

    }
    
    global void finish(database.batchablecontext bc){
    
    }

}
Amit Chaudhary 8Amit Chaudhary 8
Try to update your Batch job like below
global class accountownerhangebatch implements database.batchable<sobject>
{
        
	global database.querylocator start(database.batchablecontext bc)
	{
			
		string query = 'select Name,Ownerid from account where ownerid = \''00528000001Eo3Z\'' ;
		return database.getquerylocator(query);
    }
    
    global void execute (database.batchablecontext bc, list<Account> scope)
	{
		id oid = '';
		
		List<User> lstUser = [ SELECT Id,name FROM User WHERE name = 'marni user1' ] ;
		
        for(account a : scope)
		{
			if(lstUser.size() > 0 )
			{
				a.Ownerid= lstUser[0].id;
			}
        }
		update scope;
    }
    
    global void finish(database.batchablecontext bc)
	{
	
    }

}

Let us know if this will help you
 
hiteshwar marnihiteshwar marni
hi Amit Chaudhary 8,

thanks for your reply

when i'm trying to use your code it's throwing  Error: Compile Error: line breaks not allowed in string literals at line 7 column -1
Amit Chaudhary 8Amit Chaudhary 8
Please try below code
global class accountownerhangebatch implements database.batchable<sobject>
{
        
	global database.querylocator start(database.batchablecontext bc)
	{
			
		string query = 'select Name,Ownerid from account where ownerid = \'00528000001Eo3Z\'' ;
		return database.getquerylocator(query);
    }
    
    global void execute (database.batchablecontext bc, list<Account> scope)
	{
		id oid = '';
		
		List<User> lstUser = [ SELECT Id,name FROM User WHERE name = 'marni user1' ] ;
		
        for(account a : scope)
		{
			if(lstUser.size() > 0 )
			{
				a.Ownerid= lstUser[0].id;
			}
        }
		update scope;
    }
    
    global void finish(database.batchablecontext bc)
	{
	
    }

}
Let us know if this will help you