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
GhanesanGhanesan 

Getting 'Illegal assignment from String to List<Account> ' error on Batch Apex

Error: Illegal assignment from String to List<Account> 
Requirement: 
SOQL:  
1.account rating = prospect   
2.account didn't have any opportunities last 2 years
3.account didn't have any tasks last 2 years    

Action: 
get this related account details and contact details   
1.update account rating as dormant     

                                                                                                                 
Here is my Code:  
global class opptask implements Database.Batchable <sobject>{
      List <Account> acc = new List<Account>();
      
global Database.QueryLocator start(Database.BatchableContext bc) {
     List <Task> taskList = new List<Task>();
    string query = 'SELECT accountid FROM Task WHERE What.Type = Account AND CreatedDate = LAST_N_MONTHS:48';
      
     return Database.getQueryLocator(query);
      }
global void execute(Database.BatchableContext bc, List<Account> acclist) {
  acclist ='SELECT Id, Name FROM account WHERE Rating = Prospect AND Id NOT IN (SELECT accountid FROM Opportunity WHERE CreatedDate = LAST_N_MONTHS:48) AND Id NOT IN (taskList)';
         
  for (Account a : acclist) {
        a.Rating = dormant;
        }
   Update acc;
    }
    
    global void finish(Database.BatchableContext bc) {
        
    }
}
Best Answer chosen by Ghanesan
AnkaiahAnkaiah (Salesforce Developers) 
Hi Ganesan,

try with below code.
 
global class opptask implements Database.Batchable <sobject>{
    public List <Account> acclist = new List <Account>();
    public List <id> taskList = new List<id>();
    global Database.QueryLocator start(Database.BatchableContext bc) {
        string query = 'SELECT accountid FROM Task WHERE What.Type = Account AND CreatedDate = LAST_N_MONTHS:48';
        
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext bc, List<Task> scope) {
        
        for(Task tsk : scope){
            taskList.add(tsk.whatId);
        }
        
        acclist = [SELECT Id, Name FROM account WHERE Rating = 'Prospect' AND Id NOT IN (SELECT accountid FROM Opportunity WHERE CreatedDate = LAST_N_MONTHS:48) AND Id NOT IN :taskList];
        
        for (Account a : acclist) {
            a.Rating = 'dormant';
        }
        Update acclist;
    }
    
    global void finish(Database.BatchableContext bc) {
        
    }
}
If this helps, Please mark it as best answer.

Thanks!!


 

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Ganesan,

try with below code.
 
global class opptask implements Database.Batchable <sobject>{
    public List <Account> acclist = new List <Account>();
    public List <id> taskList = new List<id>();
    global Database.QueryLocator start(Database.BatchableContext bc) {
        string query = 'SELECT accountid FROM Task WHERE What.Type = Account AND CreatedDate = LAST_N_MONTHS:48';
        
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext bc, List<Task> scope) {
        
        for(Task tsk : scope){
            taskList.add(tsk.whatId);
        }
        
        acclist = [SELECT Id, Name FROM account WHERE Rating = 'Prospect' AND Id NOT IN (SELECT accountid FROM Opportunity WHERE CreatedDate = LAST_N_MONTHS:48) AND Id NOT IN :taskList];
        
        for (Account a : acclist) {
            a.Rating = 'dormant';
        }
        Update acclist;
    }
    
    global void finish(Database.BatchableContext bc) {
        
    }
}
If this helps, Please mark it as best answer.

Thanks!!


 
This was selected as the best answer
GhanesanGhanesan
the test works only 38%
here is my code for Test:


@isTest
Private Class opptasktest {

   Static testMethod void testBatchExecuteMethod()
   {
     Account a = new Account();
     a.Name = 'John Smith';
     a.Rating = 'Prospect';

     insert a;

     Test.startTest();

     opptask batchTest = new opptask ();
     Id jobid = Database.executeBatch(batchTest,5);

     Test.stopTest();

     Account acc = [Select Rating from Account where id=: a.Id];

     System.assertEquals('Dormant', acc.Rating);
    }
}