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
Akash Garg 2Akash Garg 2 

Want to delete opportunities record whose createddateis 365 days ago or before

I have written a apex class with test class both are running but my code coverage is only 66%.Please help to solve the problem.
Apex Class-
global class Deleteoppotest implements Schedulable{

    global void execute(SchedulableContext SC) {
        deleteoppo();
    }

    public static void deleteoppo() {

        for(List<opportunity> objoppo : [SELECT Id FROM opportunity WHERE CreatedDate <= :(Date.Today() - 200) LIMIT 10])
            {
                delete objoppo;
            }    
    }
}

Test Class
@isTest
private class Deleteoppotestcls {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test



        Opportunity op = new Opportunity();
        op.Name = 'test1';
        op.StageName = 'Qualified';
        op.CloseDate = Date.Today();
        op.Description ='testingoppotestcls';
        op.Business_Type__c = 'Domestic';


        insert op;


        Deleteoppotest.deleteoppo();

    }
}

 
Best Answer chosen by Akash Garg 2
Rupal KumarRupal Kumar
HI,
Try this Code-
@isTest
private class Deleteoppotestcls {

static testMethod void myUnitTest() {
 
    Opportunity op = new Opportunity();
    op.Name = 'test1';
    op.StageName = 'Qualified';
    op.CloseDate = Date.Today();
    op.Description ='testingoppotestcls';
    op.Type = 'Domestic';
    insert op;

    Deleteoppotest.deleteoppo();
     Test.startTest();
     Deleteoppotest sh1 = new Deleteoppotest();      
     String sch = '0  00 1 3 * ?';
       system.schedule('Test', sch, sh1);
  Test.stopTest();

}
}

Thanks
Rupal Kumar
http://Mirketa.com
 

All Answers

Rupal KumarRupal Kumar
HI,
Try this Code-
@isTest
private class Deleteoppotestcls {

static testMethod void myUnitTest() {
 
    Opportunity op = new Opportunity();
    op.Name = 'test1';
    op.StageName = 'Qualified';
    op.CloseDate = Date.Today();
    op.Description ='testingoppotestcls';
    op.Type = 'Domestic';
    insert op;

    Deleteoppotest.deleteoppo();
     Test.startTest();
     Deleteoppotest sh1 = new Deleteoppotest();      
     String sch = '0  00 1 3 * ?';
       system.schedule('Test', sch, sh1);
  Test.stopTest();

}
}

Thanks
Rupal Kumar
http://Mirketa.com
 
This was selected as the best answer
Akash Garg 2Akash Garg 2
Thank you very much Rupal.
Now my code is 100% coverage.

Can you do me a favor as i'm a beginner developer.
Can you tell me why we use 
String sch = '0  00 1 3 * ?';

Can you explain why the process from starttest to stop test??


Once again very thanks to you.
Akash Garg 2Akash Garg 2
Can you also tell me if i have to add apex class to schedule jobs??
Rupal KumarRupal Kumar
hi,
String sch = '0  00 1 3 * ?'
Use as as dummy data in test class.This is schedule time.

Test.startTest() and Test.stopTest() exist primarily to allow you to reset the governor limits within the context of your test execution and to be able to test asynchronous methods. These two statements cannot be called more than once within a testMethod. They are not required to be used but in some situations may be critical. Asynchronous (@future) calls made during the test are also finalized when you call Test.stopTest() allowing you to test the results of asynchronous behavior in your code.

However you can go and check in the Scheduled Jobs that your class has been alrady scheduled or not .
Setup -> Monitoring -> Schduled Jobs
Here you will get all the classes that has been scheduled.
If you want to reschedule it than delete it from here and schedule it again

Please mark it as the best answer if it helpful to you.

Thanks
Rupal kumar
http://mirketa.com