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
Salesforce Admin 78Salesforce Admin 78 

Want to Delete Opportunities record whose created date is 200 days ago or more

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 Salesforce Admin 78
Mahesh K 22Mahesh K 22
CLASS:
global class Deleteoppotest implements Schedulable{
global void execute(SchedulableContext SC) {
    for(List<opportunity> objoppo : [SELECT Id FROM opportunity WHERE CreatedDate <= :(Date.Today() - 200) LIMIT 10])
            delete objoppo;   
}
}

TESTCLASS:
100% code coverage

@isTest
public class Deleteoppotestcls {
    static testmethod void deleteopp(){
        opportunity opp = new opportunity();
        opp.Name = 'delete record';
        opp.CloseDate = system.today();
        opp.StageName = 'Prospecting';
            insert opp;
    
   Deleteoppotest del = new Deleteoppotest();
    string s2 ='0 02 20 * * ?';
    Test.startTest();
    system.schedule('Test1', s2, del);
    Test.stopTest();
    }
}

 

All Answers

Ankur Saini 9Ankur Saini 9
Hi Salesforce Admin 78

try this:
@isTest
private class Test1 {

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

        insert op;

        Deleteoppotest.deleteoppo();
       Test.startTest();
        Deleteoppotest c = new Deleteoppotest();
        String sch='0 0 23 * * ?';
        system.schedule('Test', sch, c);
       Test.stopTest();

    }
}

Thanks 
Ankur Saini
http://mirketa.com
Mahesh K 22Mahesh K 22
CLASS:
global class Deleteoppotest implements Schedulable{
global void execute(SchedulableContext SC) {
    for(List<opportunity> objoppo : [SELECT Id FROM opportunity WHERE CreatedDate <= :(Date.Today() - 200) LIMIT 10])
            delete objoppo;   
}
}

TESTCLASS:
100% code coverage

@isTest
public class Deleteoppotestcls {
    static testmethod void deleteopp(){
        opportunity opp = new opportunity();
        opp.Name = 'delete record';
        opp.CloseDate = system.today();
        opp.StageName = 'Prospecting';
            insert opp;
    
   Deleteoppotest del = new Deleteoppotest();
    string s2 ='0 02 20 * * ?';
    Test.startTest();
    system.schedule('Test1', s2, del);
    Test.stopTest();
    }
}

 
This was selected as the best answer
Salesforce Admin 78Salesforce Admin 78
How we can know about code coverage?please tee me,i am new developer