You need to sign in to do that
Don't have an account?
suji 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);
}
}
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);
}
}
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
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.
Choose the UpdateAccountRatingSchedular class and specify the time frame.
If this helps, Please mark it as best answer.
thanks!!
1.Open the Developer Console
2.Go to Debug / Open Execute Anonymous Window
3.Enter below code and execute it If this helps, Please mark it as best answer.
Thanks!!
I couldnt see my debug statements.could you help me whether the batch is working fine or not? Thanks in advance.
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';