You need to sign in to do that
Don't have an account?
balakrishna mandula 8
Test class for Scheduled class
Hi I am new to writing test classes. I am now writing test class for the below schdulable class.
My Req:
Parent: Incident
Child: Task
If Status of the Incident is 'RESOLVED' after 5 days this status must be changed to 'CLOSED'. condition is all the status status must also be 'CLOSED'. If task status is other than 'CLOSED' batch apex will change this to 'CLOSED'.
Below is Batch apex and schedule apex:
global class UpdateStatusToClosed implements Database.Batchable<SObject>, Schedulable{
//Variable declarations
Date dateBeforeFiveDays;
String currentStatus;
Status__c closedStatus;
List<Task__c> taskBMC;
Stopping_Manually_Closed_VR_in_Batch_Ape__c stopManuallyClosedVR; //custom setting field
//Schedule execute
global void execute(SchedulableContext sc){
Database.executeBatch(new UpdateStatusToClosed(false),10);
}
global UpdateStatusToClosed(boolean testMode){
this.testMode = testMode;
}
global boolean testMode;
global UpdateStatusToClosed(){
//Used to update the child records
taskBMC = new List<Task__c>();
//Used to query resolved status incident records
currentStatus = 'RESOLVED';
//Retrieving closed status record from status object
closedStatus = new Status__c();
closedStatus = [SELECT Id, Name FROM Status__c WHERE Name = 'CLOSED' LIMIT 1];
stopManuallyClosedVR = [SELECT Id, Bypass_Validation_Rule__c FROM Stopping_Manually_Closed_VR_in_Batch_Ape__c LIMIT 1];
system.debug('Closed Status Id '+closedStatus.Id);
//substracting days from current date
dateBeforeFiveDays = system.Today().addDays(-5);
system.debug('Present Date '+system.Today());
system.debug('Date After Sub Days '+dateBeforeFiveDays);
}
global Database.QueryLocator start(Database.BatchableContext BC){
//Quering and retrieving incident records based on current status and dateBeforeFiveDays variables
system.debug('HI START');
return Database.getQueryLocator([SELECT Id, Name, FKStatus__C,StatusChangeDate__c,
(SELECT Id, Name, FKStatus__r.Name
From Tasks__r
WHERE FKStatus__r.Name != 'CLOSED')
FROM Incident__C
WHERE FKStatus__r.Name =:currentStatus AND DAY_ONLY(StatusChangeDate__c) =:dateBeforeFiveDays]);
}
global void execute(Database.BatchableContext BC, List<SObject> scope){
system.debug('HI EXECUTE');
//Casting sobject type
List<Incident__C> incList = (List<Incident__C>)scope;
system.debug('Retrieved List '+incList+' and size is '+incList.size());
//Updating the incident status from RESOLVED to CLOSED by assigning closedStatus.Id to reference field
for(Incident__C inc : incList){
if(inc.Tasks__r.size()>0){
for(Task__c taskList : inc.Tasks__r){
taskList.FKStatus__c = closedStatus.Id;
taskBMC.add(taskList);
}
}
system.debug(' Incident Before Update '+inc);
inc.FKStatus__C = closedStatus.Id;
system.debug('Incident After Update '+inc);
}
//Updating the incidents anf tasks
if(incList.size()>0){
stopManuallyClosedVR.Bypass_Validation_Rule__c = TRUE;
update stopManuallyClosedVR;
if(taskBMC.size()>0){
update taskBMC;
system.debug('Updated Tasks '+taskBMC);
}
else{
system.debug('Nothing to update the Task records');
}
update incList;
system.debug('Updated Incidents '+incList);
}
else{
system.debug('Nothing to update the Incident records');
}
stopManuallyClosedVR.Bypass_Validation_Rule__c = FALSE;
update stopManuallyClosedVR;
}
global void finish(Database.BatchableContext BC){
}
}
Test class I tried writing is:
@isTest
public class UpdateStatusToClosed_TC{
static testMethod void batchMethod(){
Incident__c inc = new Incident__C ();
Action__c act=new Action__c();
act.Name='Client Note';
Database.insert(act,false);
inc.FKStatus__c='a23g0000000HDTO';
inc.IncidentType__c='Incident';
inc.FKCategory__c='a17g0000001Uh0k';
inc.incidentDescription__c='Test';
inc.FKImpact__c='a1Jg00000013MkH';
inc.FKUrgency__c='a2Ag0000001GUr0';
inc.StatusChangeDate__c = Date.today();
inc.Zone__c='APAC';
Database.insert(inc,false);
Task__c task = new Task__c();
task.FKIncident__c = inc.Id;
task.Client_Name__c = 'Test Task';
task.FKStatus__c = 'a23g0000000HDTO';
Database.Insert(task,false);
//Schedule the test job
Test.startTest();
BMCRemedyforce_UpdateStatusToClosed incSt = new BMCRemedyforce_UpdateStatusToClosed();
Database.executeBatch(incSt);
Test.stopTest();
}
}
I know I have to write much code here. Pls help me write this
Thanks in advance
My Req:
Parent: Incident
Child: Task
If Status of the Incident is 'RESOLVED' after 5 days this status must be changed to 'CLOSED'. condition is all the status status must also be 'CLOSED'. If task status is other than 'CLOSED' batch apex will change this to 'CLOSED'.
Below is Batch apex and schedule apex:
global class UpdateStatusToClosed implements Database.Batchable<SObject>, Schedulable{
//Variable declarations
Date dateBeforeFiveDays;
String currentStatus;
Status__c closedStatus;
List<Task__c> taskBMC;
Stopping_Manually_Closed_VR_in_Batch_Ape__c stopManuallyClosedVR; //custom setting field
//Schedule execute
global void execute(SchedulableContext sc){
Database.executeBatch(new UpdateStatusToClosed(false),10);
}
global UpdateStatusToClosed(boolean testMode){
this.testMode = testMode;
}
global boolean testMode;
global UpdateStatusToClosed(){
//Used to update the child records
taskBMC = new List<Task__c>();
//Used to query resolved status incident records
currentStatus = 'RESOLVED';
//Retrieving closed status record from status object
closedStatus = new Status__c();
closedStatus = [SELECT Id, Name FROM Status__c WHERE Name = 'CLOSED' LIMIT 1];
stopManuallyClosedVR = [SELECT Id, Bypass_Validation_Rule__c FROM Stopping_Manually_Closed_VR_in_Batch_Ape__c LIMIT 1];
system.debug('Closed Status Id '+closedStatus.Id);
//substracting days from current date
dateBeforeFiveDays = system.Today().addDays(-5);
system.debug('Present Date '+system.Today());
system.debug('Date After Sub Days '+dateBeforeFiveDays);
}
global Database.QueryLocator start(Database.BatchableContext BC){
//Quering and retrieving incident records based on current status and dateBeforeFiveDays variables
system.debug('HI START');
return Database.getQueryLocator([SELECT Id, Name, FKStatus__C,StatusChangeDate__c,
(SELECT Id, Name, FKStatus__r.Name
From Tasks__r
WHERE FKStatus__r.Name != 'CLOSED')
FROM Incident__C
WHERE FKStatus__r.Name =:currentStatus AND DAY_ONLY(StatusChangeDate__c) =:dateBeforeFiveDays]);
}
global void execute(Database.BatchableContext BC, List<SObject> scope){
system.debug('HI EXECUTE');
//Casting sobject type
List<Incident__C> incList = (List<Incident__C>)scope;
system.debug('Retrieved List '+incList+' and size is '+incList.size());
//Updating the incident status from RESOLVED to CLOSED by assigning closedStatus.Id to reference field
for(Incident__C inc : incList){
if(inc.Tasks__r.size()>0){
for(Task__c taskList : inc.Tasks__r){
taskList.FKStatus__c = closedStatus.Id;
taskBMC.add(taskList);
}
}
system.debug(' Incident Before Update '+inc);
inc.FKStatus__C = closedStatus.Id;
system.debug('Incident After Update '+inc);
}
//Updating the incidents anf tasks
if(incList.size()>0){
stopManuallyClosedVR.Bypass_Validation_Rule__c = TRUE;
update stopManuallyClosedVR;
if(taskBMC.size()>0){
update taskBMC;
system.debug('Updated Tasks '+taskBMC);
}
else{
system.debug('Nothing to update the Task records');
}
update incList;
system.debug('Updated Incidents '+incList);
}
else{
system.debug('Nothing to update the Incident records');
}
stopManuallyClosedVR.Bypass_Validation_Rule__c = FALSE;
update stopManuallyClosedVR;
}
global void finish(Database.BatchableContext BC){
}
}
Test class I tried writing is:
@isTest
public class UpdateStatusToClosed_TC{
static testMethod void batchMethod(){
Incident__c inc = new Incident__C ();
Action__c act=new Action__c();
act.Name='Client Note';
Database.insert(act,false);
inc.FKStatus__c='a23g0000000HDTO';
inc.IncidentType__c='Incident';
inc.FKCategory__c='a17g0000001Uh0k';
inc.incidentDescription__c='Test';
inc.FKImpact__c='a1Jg00000013MkH';
inc.FKUrgency__c='a2Ag0000001GUr0';
inc.StatusChangeDate__c = Date.today();
inc.Zone__c='APAC';
Database.insert(inc,false);
Task__c task = new Task__c();
task.FKIncident__c = inc.Id;
task.Client_Name__c = 'Test Task';
task.FKStatus__c = 'a23g0000000HDTO';
Database.Insert(task,false);
//Schedule the test job
Test.startTest();
BMCRemedyforce_UpdateStatusToClosed incSt = new BMCRemedyforce_UpdateStatusToClosed();
Database.executeBatch(incSt);
Test.stopTest();
}
}
I know I have to write much code here. Pls help me write this
Thanks in advance
I found another question similar to yours in the forum. Please take a look: https://developer.salesforce.com/forums/?id=906F00000008yhmIAA
Let me know if this doesn't work and we can try to take a look in deep at your code.