Skip to main content
Susan Stevenson (Leaptree) a posé une question dans #Apex
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;

    

3 réponses
  1. 21 févr. 2020, 11:51
    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
0/9000