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
Andrew Fisher 28Andrew Fisher 28 

Trigger test problem !

Hi wonderful Community

I need help with a test class for a trigger, only at 47% !!

Here is the Trigger

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
Trigger CreatingAutoRecords on Opportunity (After Insert, After Update)
{
    
    List<MRR__c> MRRRecordsFinalListToInsert = New List<MRR__c>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate)
    {
        For(Opportunity opp : Trigger.New)
        {
            If(Opportunity.Contract_Period_Months__c != null)
            {
                List<MRR__c> fetchingAlreadyExixtsedRecords = [Select Id FROM MRR__c WHERE Opportunity__c =:opp.Id];
                
                If(fetchingAlreadyExixtsedRecords.IsEmpty())
                {
                    // We are only creating a records when there is no MRR__c records exixts.
                    For(Integer I = 1; I <= opp.Contract_Period_Months__c;I++)
                    
                        {
                        MRR__c crd = New MRR__c();
                        
                        crd.Opportunity__c = opp.Id;
                        crd.Name       = 'Month' + I;
                        crd.MRR_Amount__c = opp.MRR_Amount__c;
                        crd.MRR_Date__c = opp.Contract_Start_Date__c.addMonths(I);
                        
                        MRRRecordsFinalListToInsert.add(crd);
                    }
                }
                
            }
            
            try{
                If(!MRRRecordsFinalListToInsert.IsEmpty()){
                    insert MRRRecordsFinalListToInsert;
                }
            }
            Catch(Exception e){
                System.debug('The thrown exception for CreatingAutoRecords is:: ' + e.getMessage());
            }
        }
    }
    
    
}

And this is the Test Class

@isTest
private with sharing class CreatingAutoRecordsTest {

static testMethod void MyTestCreatingAutoRecords()
{
test.starttest(); 

    
    List<MRR__c> MRRRecordsFinalListToInsert = New List<MRR__c>();
    
    
    
    
      
   MRR__c crd = New MRR__c();
    crd.Opportunity__c =  '0062C00000AVFEwQAP';
    crd.Name = 'Testing';
    
    crd.MRR_Amount__c = 110;   
    
      MRRRecordsFinalListToInsert.add(crd);
                        
                        
     insert MRRRecordsFinalListToInsert;
    
    
    
    
    
     Database.SaveResult str = database.insert(crd , False);

        System.assertEquals(True, str.isSuccess());
     
   test.stoptest();  
    

}
}


Any help would he highly appreciated to get it to pass at least

 
sreenath reddy 21sreenath reddy 21
Hi andrew
You only insert the rercord, update the existing record it will cover, please follow below code

@isTest
private with sharing class CreatingAutoRecordsTest {

static testMethod void MyTestCreatingAutoRecords()
{
test.starttest(); 

    
    List<MRR__c> MRRRecordsFinalListToInsert = New List<MRR__c>();     
   MRR__c crd = New MRR__c();
    crd.Opportunity__c =  '0062C00000AVFEwQAP';
    crd.Name = 'Testing';
    
    crd.MRR_Amount__c = 110;   
    
      MRRRecordsFinalListToInsert.add(crd);                       
     insert MRRRecordsFinalListToInsert;    
    MRRRecordsFinalListToInsert[0].Name = 'Test';
    update MRRRecordsFinalListToInsert;
     Database.SaveResult str = database.insert(crd , False);

        System.assertEquals(True, str.isSuccess());
     
   test.stoptest();  
    

}
}
 
Andrew Fisher 28Andrew Fisher 28
Many thanks for your help, I am getting an error when I run the test:
Pass/FailFail
Error MessageSystem.AssertException: Assertion Failed: Expected: true, Actual: false
Stack TraceClass.CreatingAutoRecordsTest.MyTestCreatingAutoRecords: line 22, column 1

Any help is appreciated !


 
Andrew Fisher 28Andrew Fisher 28
My code is still at 47% :(