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
suji srinivasansuji srinivasan 

Hi, my batch is not scheduling . I got 100% code coverage and no errors.can anyone guide me?

global class UpdateAccountRating implements Database.Batchable<sObject> {
   
 global  Database.QueryLocator start(Database.BatchableContext bc) {
     set<string> strlist = new set<string>{'workInprogress','Delivered','ClosedWon'};
      string query='select id, stageName,AccountId, Account.Rating from opportunity where stageName IN :strlist AND AccountId !=Null';
     system.debug('query=='+query);
     return Database.getQueryLocator(query); 
    }
     global Void execute(Database.BatchableContext bc, List<opportunity> Scope){
      
        for(opportunity o :Scope){
            o.Account.Rating='client';  
          system.debug('Rating'+o.Account.Rating);
           
        }
         update Scope;}
         global void finish(Database.BatchableContext bc){
       
        AsyncApexJob job = [SELECT Id, Status,NumberOfErrors,JobItemsProcessed,TotalJobItems, CreatedBy.Email 
                            FROM AsyncApexJob
                            WHERE Id = :bc.getJobId()];  
    }   
     }

testclass
@isTest
public class UpdateAccountRatingTest {
    @isTest
    public static void unit_test(){
      
        Account acc =new Account();
        acc.Name = 'test';
        acc.Rating='prospect';
        insert acc;
        
        
        Opportunity opp = new opportunity();
        opp.Name = 'test opp';
        opp.Accountid = acc.Id;
        opp.stageName = 'workInprogress';
        opp.CloseDate = system.today()+5;
        insert opp;
         
       
        
        Test.startTest();
      
        UpdateAccountRating uar  = new UpdateAccountRating();
        Database.executeBatch(uar);
      String sch = '0 0 0 1 * ? *';
      string JobID = system.schedule('BatchJob',sch, new UpdateAccountRatingSchedular());
        Test.stopTest();
       

 }
    }
schedular
global class UpdateAccountRatingSchedular implements Schedulable {
global void execute(SchedulableContext sc){
        UpdateAccountRating uar = new UpdateAccountRating();
        database.executebatch(uar);
    }
}

User-added image
Best Answer chosen by suji srinivasan
suji srinivasansuji srinivasan
Hi Team ,
I resolved it by writing seperate soql for Account and Opportunity  .Thank you for your support.
global  Database.QueryLocator start(Database.BatchableContext bc) {
List<Id> accId = new List<Id>();
     for(Opportunity O:[select Id,AccountId from Opportunity where StageName ='Delivered' OR StageName ='Closed Won' OR StageName = 'Work In Progress']){
         accId.add(O.AccountId);
     }
        string query='select ID,Name,Rating from Account where ID IN:accId';

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Suji,

You need to schedule the schedule apex class then only it will run in specific time.

How to schedule:

Setup--> Development --> click on Apex classes --> click on Schedule Apex button

Refer the below screenshot.
User-added image

Choose the UpdateAccountRatingSchedular  class and specify the time frame.
User-added image

If this helps, Please mark it as best answer.

thanks!!
AnkaiahAnkaiah (Salesforce Developers) 
And another way to schedule the batch apex:

1.Open the Developer Console
2.Go to Debug / Open Execute Anonymous Window
3.Enter below code and execute it
UpdateAccountRatingSchedular sb= new UpdateAccountRatingSchedular();

 String sch = '0 0 0 1 * ? *';

System.schedule('UpdateAccountRatingSchedular Job', sch, sb);
If this helps, Please mark it as best answer.

Thanks!!

 
suji srinivasansuji srinivasan
I scheduled it for 5 mins to check . But it stays in queue showing zero batch.
suji srinivasansuji srinivasan
Hi ankaiah,
I couldnt see my debug statements.could you help me whether the batch is working fine or not? Thanks in advance.
suji srinivasansuji srinivasan
Hi Team ,
I resolved it by writing seperate soql for Account and Opportunity  .Thank you for your support.
global  Database.QueryLocator start(Database.BatchableContext bc) {
List<Id> accId = new List<Id>();
     for(Opportunity O:[select Id,AccountId from Opportunity where StageName ='Delivered' OR StageName ='Closed Won' OR StageName = 'Work In Progress']){
         accId.add(O.AccountId);
     }
        string query='select ID,Name,Rating from Account where ID IN:accId';
This was selected as the best answer