You need to sign in to do that
Don't have an account?
guptach45
I have to write a test class for batch class. PLease help as i am new to apex coding.
global class CSTeamCountryCaseUpdateBatch implements Database.Batchable<sObject>{
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator([SELECT ID, Status, LastModifiedDate,LastModifiedById,Last_Modified_By_Previous_Day__c,CS_Team_Country__c,Days_Since_Last_Modified_Date__c FROM Case Where Status !='Closed' AND (CS_Team_Country__c ='United States' OR CS_Team_Country__c ='Canada')]);
}
global void execute(Database.BatchableContext bc, List<Case> records){
system.debug('records--'+records.size());
List<Case> caseList = new List<Case>();
for(Case c:records){
system.debug('test--'+c.Days_Since_Last_Modified_Date__c);
if(c.Days_Since_Last_Modified_Date__c == null){
c.Days_Since_Last_Modified_Date__c =0;
}
c.Days_Since_Last_Modified_Date__c =c.Days_Since_Last_Modified_Date__c+1;
if(c.LastModifiedById != system.label.apiuserid){
c.Last_Modified_By_Previous_Day__c =c.LastModifiedById;
}
caseList.add(c);
}
system.debug('caseList--'+caseList);
if(caseList.size()>0){
update caseList;
}
system.debug('caseList--'+caseList);
}
global void finish(Database.BatchableContext bc){
}
}
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator([SELECT ID, Status, LastModifiedDate,LastModifiedById,Last_Modified_By_Previous_Day__c,CS_Team_Country__c,Days_Since_Last_Modified_Date__c FROM Case Where Status !='Closed' AND (CS_Team_Country__c ='United States' OR CS_Team_Country__c ='Canada')]);
}
global void execute(Database.BatchableContext bc, List<Case> records){
system.debug('records--'+records.size());
List<Case> caseList = new List<Case>();
for(Case c:records){
system.debug('test--'+c.Days_Since_Last_Modified_Date__c);
if(c.Days_Since_Last_Modified_Date__c == null){
c.Days_Since_Last_Modified_Date__c =0;
}
c.Days_Since_Last_Modified_Date__c =c.Days_Since_Last_Modified_Date__c+1;
if(c.LastModifiedById != system.label.apiuserid){
c.Last_Modified_By_Previous_Day__c =c.LastModifiedById;
}
caseList.add(c);
}
system.debug('caseList--'+caseList);
if(caseList.size()>0){
update caseList;
}
system.debug('caseList--'+caseList);
}
global void finish(Database.BatchableContext bc){
}
}
Try this below code.
If it works for you pls mark it as a best answer so it helps others too.
Thanks.
All Answers
I used trailhead example I hope you find this useful for your use case.
The trailhead link is: https://trailhead.salesforce.com/content/learn/modules/asynchronous_apex/async_apex_batch.
Do let me know if there are any issues or in case if this worked can you please choose this as best answer so that it can be used by others in the future.
Regards,
Anutej
Try this below code.
If it works for you pls mark it as a best answer so it helps others too.
Thanks.