0

Apex Class :

public class CreateInvoice{

    public CreateInvoice(){

        List<Training__c> trainingList = new List<Training__c>();
        List<Facturatie__c> facturatieList = new List<Facturatie__c>();

        try {
        trainingList = [SELECT id , NAME, Startdatum__c,factuur_bedrijf__c,
                          (SELECT Cursist_Prijs__c, Training__c, Cursist__c,CreatedDate,Prijs_training__c,
                           korting__c,Id 
                           FROM Trainingen_Volgen__r ) 
                       FROM  Training__c 
                       WHERE id NOT IN (SELECT  Training__c FROM Facturatie__c)];
        }catch (Exception e) {
            System.debug('The following exception has occurred' + e.getMessage() +
                         'At line number :' + e.getLineNumber() + 'Error' +
                         e.getStackTraceString());
        }              
        if(!trainingList.IsEmpty()){

            for(Training__c trainingRecord : trainingList){

                if(!trainingRecord.Trainingen_Volgen__r.IsEmpty()){

                    for(Cursist__c cursistRecord : trainingRecord.Trainingen_Volgen__r){

                        Facturatie__c facturatieRecord = checkDate(cursistRecord,trainingRecord);
                        facturatieList.add(facturatieRecord);
                    }
                }   
            }

            if(!facturatieList.IsEmpty()){  
               try {
                   insert facturatieList;
                }catch (Exception e) {
                    System.debug('The following exception has occurred' + e.getMessage() +
                                 'At line number :' + e.getLineNumber() + 'Error' +
                                 e.getStackTraceString());
                }   

            }
        }   
    }

    public Facturatie__c checkDate(Cursist__c cursistRecord,Training__c trainingRecord){

        if(cursistRecord != null && trainingRecord != null){
            Date todaysDate = system.today();

            if( todaysDate.addDays(-21) >= trainingRecord.Startdatum__c 
                && todaysDate >= cursistRecord.CreatedDate 
                && todaysDate.addDays(-1) >= trainingRecord.Startdatum__c){

                Facturatie__c facturatieRecord = new Facturatie__c();

                facturatieRecord.Training__c = trainingRecord.Id;   
                facturatieRecord.Factuur_Datum__c = todaysDate;  
                facturatieRecord.Verval_datum__c = todaysDate.addDays(30);

                if(cursistRecord.Prijs_training__c != null &&  cursistRecord.korting__c != null){
                    facturatieRecord.Factuur_Bedrag__c = cursistRecord.Prijs_training__c - cursistRecord.korting__c;
                }else if(cursistRecord.Prijs_training__c != null && cursistRecord.korting__c == null){
                    facturatieRecord.Factuur_Bedrag__c = cursistRecord.Prijs_training__c;
                }else if(cursistRecord.Prijs_training__c == null && cursistRecord.korting__c != null){
                    facturatieRecord.Factuur_Bedrag__c = cursistRecord.korting__c ;
                }

                facturatieRecord.Korting__c =  cursistRecord.korting__c;
                facturatieRecord.cursist_prijs__c= cursistRecord.Cursist_Prijs__c ;
                facturatieRecord.Contactpersoon__c = cursistRecord.Cursist__c;
                facturatieRecord.Account__c = trainingRecord.factuur_bedrijf__c; 

                return facturatieRecord;
            }
        }

        return null;
    }
}

Test Class

@isTest
public class CreateInvoiceTestClass{

    // Creating Test Data
   @isTest public static void testData(){

        Account account = new Account(Name = 'TestAccount1');  
        insert account;

        List<Training__c> trainingList = new List<Training__c>();
        for(integer counter=0;counter<2;counter++){

            Training__c trainingRecord = new Training__c();
            trainingRecord.name = 'TestRecord'+counter;
            trainingRecord.Startdatum__c = System.today().addDays(+2);
            trainingRecord.factuur_bedrijf__c = account.Id;  
            trainingList.add(trainingRecord);
        }
        insert trainingList;

         List<Facturatie__c> facturatieList = new List<Facturatie__c>();
        for(integer counter=0;counter<2;counter++){

            Facturatie__c facturatieRecord = new Facturatie__c();
            facturatieRecord.name = 'TestRecord'+counter;
            facturatieRecord.Factuur_Bedrag__c = 1000;
            facturatieRecord.Training__c = trainingList[counter].Id;
            facturatieList.add(facturatieRecord);
        }

       for(integer counter=0;counter<2;counter++){

            Facturatie__c facturatieRecord = new Facturatie__c();
            facturatieRecord.name = 'TestRecord2'+counter;
            facturatieRecord.Factuur_Bedrag__c = 1000;
            facturatieRecord.Training__c = trainingList[counter].Id;

           system.debug('facturatieRecord.Training__c '+facturatieRecord.Training__c);


            facturatieList.add(facturatieRecord);
        }

       insert facturatieList;
system.debug('facturatieList '+facturatieList);

        List<Cursist__c> cursistList = new List<Cursist__c>();
        for(integer counter=0;counter<2;counter++){

            Cursist__c cursistRecord = new Cursist__c();           
            cursistRecord.Training__c = trainingList[counter].Id;
            cursistRecord.Prijs_training__c = 10;
            cursistRecord.korting__c = 10;
            cursistRecord.CreatedDate = System.today();
            cursistList.add(cursistRecord);
        }
        insert cursistList;

       system.debug('facturatieList[0].Account__c '+facturatieList[0].Account__c);
       Test.StartTEst();
       CreateInvoice createInv = new CreateInvoice(); 

       system.debug('createInv '+createInv);

       Test.stopTest();

       System.assertEquals(facturatieList[0].Account__c,null);

    }

}

enter image description here`

3
  • Traininglist is Empty
    – benahm
    Mar 8 '17 at 16:34
  • @Eric When I do a system debug I get following so trainingslist is not empty :17:46:33:176 USER_DEBUG [21]|DEBUG|trainingList (Training__c:{Name=TestRecord0, Startdatum__c=2017-03-10 00:00:00, Factuur_bedrijf__c=0018E00000VnAJtQAN, Id=a008E000006V442QAC}, Training__c:{Name=TestRecord1, Startdatum__c=2017-03-10 00:00:00, Factuur_bedrijf__c=0018E00000VnAJtQAN, Id=a008E000006V443QAC})
    – user42169
    Mar 8 '17 at 16:48
  • Where are you placing that debug because if it was line 21 it would be inside the uncovered If. If the list is not empty it will enter the if so regardless of what you may think the list is empty when it gets to the If statement you may just be looking at it wrong
    – Eric
    Mar 8 '17 at 17:10
2

If you look at your code coverage it is pretty clear what your issue is:

You Returned list if(!trainingList.IsEmpty()){ is Empty.

To understand why the list is empty, look at the filters on your query:

WHERE id NOT IN (SELECT  Training__c FROM Facturatie__c)

Now, look at your test data that you are creating and you will see that for each Training record you are creating you are assigning it to a Facturatie__c record

facturatieRecord.Training__c = trainingList[counter].Id;

thus there are no Training records that are not also related to a Facturatie__c record

1
  • When I do a system debug I get following so trainingslist is not empty :17:46:33:176 USER_DEBUG [21]|DEBUG|trainingList (Training__c:{Name=TestRecord0, Startdatum__c=2017-03-10 00:00:00, Factuur_bedrijf__c=0018E00000VnAJtQAN, Id=a008E000006V442QAC}, Training__c:{Name=TestRecord1, Startdatum__c=2017-03-10 00:00:00, Factuur_bedrijf__c=0018E00000VnAJtQAN, Id=a008E000006V443QAC})
    – user42169
    Mar 8 '17 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.