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
Pallavi singhPallavi singh 

Test class for the Opportunitytrigger and OpportunityTriggerHandler

Hi ,
Trying to write the test class for the Opportunity Trigger and OpportunityTriggerHandler

Here is the Opportunity Trigger:

trigger OpportunityTrigger on Opportunity (after update) {
    
    if(Trigger.isAfter && Trigger.isUpdate){
        if(OpportunityTriggerHandler.isFirstTime){
            OpportunityTriggerHandler.isFirstTime=false;
            List<opportunity> opportunityList= new List<opportunity>();
            Map<id,decimal> optyProbabilityMap=new Map<id,decimal>();
            set<id> optyIds=new set<id>();
            for(opportunity opty:Trigger.new){
                if(opty.Probability != trigger.oldMap.get(opty.id).Probability){
                    opportunityList.add(opty);
                    optyIds.add(opty.Id);
                    if(!optyProbabilityMap.containsKey(opty.Id)){
                        optyProbabilityMap.put(opty.Id,opty.Probability);
                    }
                }
            }
            if(!opportunityList.isEmpty()){
                OpportunityTriggerHandler optyTrig=new OpportunityTriggerHandler();
                optyTrig.probabilityUpdate(opportunityList,optyIds,optyProbabilityMap);
            }
        }
    }
}


And here is the OpportunityTriggerHandler class:

public class OpportunityTriggerHandler {
    
    public static Boolean isFirstTime=true;
    
    
    public void probabilityUpdate(List<Opportunity> opportunityList,set<id> optyIds,Map<id,decimal> optyProbabilityMap){
        
        Map<id,OpportunityLineItem> optyProductMap= new Map<id,OpportunityLineItem>([SELECT id,name from OpportunityLineItem where OpportunityId IN :optyIds]);
        
        Map<Id, Opportunity> oppysMap = new Map<Id, Opportunity>([SELECT Id, Probability FROM Opportunity WHERE Id IN :optyIds]);
        
        List<OpportunityLineItemSchedule> optyLnItmSchedList=new List<OpportunityLineItemSchedule>();
        
        for(OpportunityLineItemSchedule eachLnItemSchdle : [SELECT id,Expected_Amount__c,Quantity,OpportunityLineItem.OpportunityId FROM 
                                                            OpportunityLineItemSchedule where OpportunityLineItemId IN: optyProductMap.keyset()]){
            
            Opportunity oppy = oppysMap.get(eachLnItemSchdle.OpportunityLineItem.OpportunityId);
            eachLnItemSchdle.Expected_Amount__c = eachLnItemSchdle.Quantity * (oppy.Probability / 100);                                                                
            optyLnItmSchedList.add(eachLnItemSchdle);                
        }
        
        update optyLnItmSchedList;
    }
}


And this is my Test Class:

@isTest
public class OpportunityTriggerHandlerTest {
    @isTest
    static void testProbabilityUpdate() {
        TestDataFactory factory = new TestDataFactory();

        // Create test Opportunity
        Opportunity opportunity = factory.createOpportunity('Test Opportunity', 'Prospecting', Date.today().addDays(30), 50);
        
        // Create test OpportunityLineItem
        OpportunityLineItem lineItem = factory.createOpportunityLineItem(opportunity, 2);
        
        // Create test OpportunityLineItemSchedule
        OpportunityLineItemSchedule schedule = factory.createOpportunityLineItemSchedule(lineItem, 2);

        // Invoke the method being tested
        OpportunityTriggerHandler handler = new OpportunityTriggerHandler();
        Set<Id> optyIds = new Set<Id>{opportunity.Id};
        Map<Id, Decimal> optyProbabilityMap = new Map<Id, Decimal>{opportunity.Id => opportunity.Probability};
        handler.probabilityUpdate(new List<Opportunity>{opportunity}, optyIds, optyProbabilityMap);
        
        // Retrieve the updated OpportunityLineItemSchedule record
        OpportunityLineItemSchedule updatedSchedule = [SELECT Expected_Amount__c FROM OpportunityLineItemSchedule WHERE Id = :schedule.Id];
        
        // Assert the expected values
        Decimal expectedAmount = schedule.Quantity * (opportunity.Probability / 100);
        System.assertEquals(expectedAmount, updatedSchedule.Expected_Amount__c);
    }
}

i get this dml error "System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: []"
How to solve this 

Thank you in advance
Pallavi
Mahesh GorrepatiMahesh Gorrepati
for(OpportunityLineItemSchedule eachLnItemSchdle : [SELECT id,Expected_Amount__c,Quantity,OpportunityLineItem.OpportunityId FROM 
                                                            OpportunityLineItemSchedule where OpportunityLineItemId IN: optyProductMap.keyset()])


KIndly check the SOQL query here. you are using opprtunity ids (keyset method)  for the opportuntylineItemId