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
Susan Stevenson 19Susan Stevenson 19 

Test Class Assistance needed

Hi,

I have the following trigger.

When a child record (Opportunity Product Lines) on the opportunity gets updated, it queries all child records of the same type on the opportunity and stamps a sum of one of the fields up to the opportunity.

I understand that because of the structure of the trigger I cannpot just write a simple, create product lines trigger so this one is proving a little difficult for me. 

Is there anyone that would be able to help me write a test class for the below trigger. I am now to APEX.

trigger RollUpIncpasulaCountOnOpp on Opportunity_Product_Line__c (after insert){
List<Opportunity> OppList = new List<Opportunity>();
   
   Set<Id> OppIds = new Set<Id>();

     for(Opportunity_Product_Line__c OpptoUpdate:Trigger.New) {
      
        OppIds.add(OpptoUpdate.Opportunity__c);   

     }
   
    
    AggregateResult[] groupedResults = [SELECT Opportunity__c, sum(Requires_Incapsula_ID__c) IncapProducts FROM Opportunity_Product_Line__c where Opportunity__c IN :OppIds GROUP BY Opportunity__c ];

    for(AggregateResult ar:groupedResults) {
        
        Id Oppid = (ID)ar.get('Opportunity__c');
        Opportunity Opp1 = new Opportunity(Id=Oppid);   
        Opp1.Count_Incapsula_Products__c = (double.valueOf(ar.get('IncapProducts')));  
        OppList.add(Opp1);

   }      
      
   update OppList;

    
Best Answer chosen by Susan Stevenson 19
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi Susan,
write test with a test method
1)Create an opportunity .
2)Create a Opportunity_Product_Line__c record with the above opporuntunity tagged and also give Requires_Incapsula_ID__c  field value .


Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards

All Answers

Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi Susan,
write test with a test method
1)Create an opportunity .
2)Create a Opportunity_Product_Line__c record with the above opporuntunity tagged and also give Requires_Incapsula_ID__c  field value .


Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards
This was selected as the best answer
Susan Stevenson 19Susan Stevenson 19
Hi Devi,

Thanks. I have tried that - here is my test code. When I run the test, the trigger still reports 0 lines coverage. I thought it was because I wasn't passing anything to the OpptoUpdate ID set? 



@isTest
public class RollUpIncpasulaCountOnOppTest {
    static testMethod void IncapsulaTest() {
        
        Account acc = new Account();   
        acc.Name ='The Best Test Company';   
        insert acc;
       
        Opportunity opp = new Opportunity();   
        opp.Name ='The Best Test Company Opportunity';  
        opp.AccountId = acc.id;
        insert opp;
        
        Product2 prod = new Product2();   
        prod.Name ='The Best Test Company Product'; 
        prod.Requires_Incapsula_ID__c= true;
        insert prod;

        Test.startTest();
        
        Opportunity_Product_Line__c OppLine = new Opportunity_Product_Line__c();   
        OppLine.Opportunity__c =opp.Id; 
        OppLine.Product__c=prod.Id;
        insert OppLine;
        
        Test.stopTest();

    }
    
    
    static testMethod void NonIncapsulaTest() {
        
        Account acc = new Account();   
        acc.Name ='The Best Test Company';   
        insert acc;
       
        Opportunity opp = new Opportunity();   
        opp.Name ='The Best Test Company Opportunity';  
        opp.AccountId = acc.id;
        insert opp;
        
        Product2 prod = new Product2();   
        prod.Name ='The Best Test Company Non Incapsula Product'; 
        insert prod;

        Test.startTest();
        
        Opportunity_Product_Line__c OppLine = new Opportunity_Product_Line__c();   
        OppLine.Opportunity__c =opp.Id; 
        OppLine.Product__c=prod.Id;
        insert OppLine;
        
        Test.stopTest();

    }
}
Susan Stevenson 19Susan Stevenson 19
OK, I got it. Apologies - some school boy errors going on here!!

Start and stop test is causing me issues and i have left out a few required firled on the opportunity (Stage and close date)