You need to sign in to do that
Don't have an account?
Ghanesan
test coverage 38%
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
Batch Class:
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) { }
}
Test Class:
@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);
}
}
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
Batch Class:
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) { }
}
Test Class:
@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);
}
}
you need to modify your query in the batch apex.
and then try with below test class.
If this helps, Please mark it as best answer.
Thanks!!