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
Amit Jadhav 13Amit Jadhav 13 

In test Class Trigger.new and trigger.old are holding same value i want different value how is possible please help

class :
public class RevertQuantityTriggerHandler {
    //public static Boolean runOnce = true;
    //RevertQuantityHelperClass helpobj = new RevertQuantityHelperClass();
    public List<Inventory__c> lstofinventory = new List<Inventory__c>([select id,Name,Inventory_Name__c,Account__c,Quantity__c,Product__c from Inventory__c]);
    Double difference;
    public double oldTotalQuantity, newTotalQuantity;
    
    public void onBeforeDelete(List<Cost_Price__c> listofcostprice)
    {
        
        for(Cost_Price__c obj : listofcostprice)
        {
            System.debug('cost Price --->'+obj);
            for(Inventory__c inventobj : lstofinventory)
            {
                
                if(obj.Product_From_Inventory__c == inventobj.id)
                {
                    inventobj.Quantity__c += obj.Quantity__c;
                }
                
            }
            update lstofinventory;
        }
    }
    
    public void onAfterUpdate(List<Cost_Price__c> listofcostprice, List<Cost_Price__c> triggerold)
    {
     	for(Cost_Price__c cp:listofcostprice )
        {
            system.debug('listofcostprice'+listofcostprice);
            newTotalQuantity=cp.Quantity_Per_Container__c*cp.Total_of_Containers__c; 
        }
        for(Cost_Price__c cp:triggerold )
        {
            system.debug('triggerold'+triggerold);
            oldTotalQuantity=cp.Quantity_Per_Container__c*cp.Total_of_Containers__c; 
        }
        
        if(triggerold[0].Product_From_Inventory__c != null)
        {
            
            if((oldTotalQuantity != null || oldTotalQuantity != 0) && (newTotalQuantity != null || newTotalQuantity != 0) )
            {
                //changed this if condition
                if(listofcostprice[0].Product_From_Inventory__c==triggerold[0].Product_From_Inventory__c)
                {
                    
                    for(Cost_Price__c obj : listofcostprice)
                    {
                        
                        if(listofcostprice[0].id ==  triggerold[0].id)
                        {
                            system.debug('newTotalQuantity'+newTotalQuantity);
                            system.debug('oldTotalQuantity'+oldTotalQuantity);
                            if(newTotalQuantity > oldTotalQuantity)
                            {
                                
                                //Calculate difference between new total quantity and old total quantity
                                difference = newTotalQuantity - oldTotalQuantity;
                                system.debug('diff --->'+difference);
                                for(Inventory__c inventobj : lstofinventory)
                                {
                                    system.debug('diff inside for --->'+difference);
                                    if(inventobj.Id == listofcostprice[0].Product_From_Inventory__c)
                                    {
                                        system.debug('diff in innerif --->'+difference);
                                        system.debug('invent id --->'+inventobj.Id);
                                        system.debug('listof cp id --->'+listofcostprice[0].Product_From_Inventory__c);
                                        inventobj.Quantity__c -= difference;
                                        system.debug('Updated invent-->'+inventobj.Quantity__c);
                                    }
                                }
                            }
                            
                            if(newTotalQuantity < oldTotalQuantity) 
                            {
                                system.debug('inside else');
                                
                                //Calculate difference between new total quantity and old total quantity
                                difference =  oldTotalQuantity - newTotalQuantity;
                                for(Inventory__c inventobj : lstofinventory)
                                {
                                    system.debug('diff inside for --->'+difference);
                                    if(inventobj.Id == listofcostprice[0].Product_From_Inventory__c)
                                    {
                                        system.debug('diff in inner if --->'+difference);
                                        system.debug('invent id --->'+inventobj.Id);
                                        system.debug('listof cp id --->'+listofcostprice[0].Product_From_Inventory__c);
                                        inventobj.Quantity__c += difference;
                                        system.debug('Updated invent-->'+inventobj.Quantity__c);
                                    }
                                }
                            }
                        }
                    }
                    update lstofinventory;
                    
                }
                
                
                
                else
                {
                    System.debug('1--->Inside Primium Else');
                    
                    system.debug('new total quantity '+newTotalQuantity);
                    system.debug('old total quantity '+oldTotalQuantity);
                    for(Cost_Price__c obj : listofcostprice)
                    {
                        
                        if(listofcostprice[0].id ==  triggerold[0].id)
                        {
                            system.debug('3---->listofcostprice[0].id ==  triggerold[0].id');
                         
                            
                            if(newTotalQuantity != oldTotalQuantity)
                            {
                             
                              
                                for(Inventory__c inventobj : lstofinventory)
                                {
                                    
                                    
                                    if(inventobj.Id == listofcostprice[0].Product_From_Inventory__c)
                                    {
                                        
                                        inventobj.Quantity__c -= newTotalQuantity;  //changes
                                        
                                        system.debug('8---->Updated invent-->'+inventobj.Quantity__c);
                                    }
                                    if(inventobj.id == triggerold[0].Product_From_Inventory__c)
                                    {
                                        inventobj.Quantity__c += oldTotalQuantity;      
                                        
                                        system.debug('9---->Updated invent old-->'+inventobj.Quantity__c);
                                    }
                                    
                                }
                                
                            }
                            
                            else if(newTotalQuantity == oldTotalQuantity)
                            {
                                for(Inventory__c inventobj : lstofinventory)
                                {
                                    if(inventobj.Id == listofcostprice[0].Product_From_Inventory__c)
                                    {
                                        
                                        inventobj.Quantity__c -= newTotalQuantity;
                                        
                                        system.debug('10---->Updated invent-->'+inventobj.Quantity__c);
                                    }
                                    if(inventobj.Id == triggerold[0].Product_From_Inventory__c)
                                    {
                                        inventobj.Quantity__c += newTotalQuantity;     
                                        
                                        system.debug('11---->Updated invent old-->'+inventobj.Quantity__c);
                                    }
                                    
                                }
                               
                            }
                        }
                    }
                    update lstofinventory; 
                   
                }  
            }
        }
    }
}


Test Class:
@isTest 
public class TestCostPriceRevertQuantityTrigger 
{
    public static testmethod void runTest()
    {
        //create object of handler class..
       
       Id RecordTypeIdacc = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Logistic').getRecordTypeId();
        system.debug('Record type-->' +RecordTypeIdacc );
        Account objacc = new Account();
        
        objacc.Account_Company_Name__c = 'Chetna';
        objacc.Type_of_Logistic__c = 'Warehousing';
        objacc.Name = 'ABC';
        objacc.Worked_By__c	= 'Chetna';
        objacc.Type ='Bank';
        objacc.CurrencyIsoCode = 'EUR';
        objacc.Business_Entity__c = 'TRADEASIA INTERNATIONAL INDIA';
        objacc.RecordTypeId = RecordTypeIdacc;
        insert objacc ;
        
        //Create Product
        Product2 objpro = new Product2();
        objpro.Name = 'Sodium';
        objpro.CurrencyIsoCode = 'EUR'; 
        insert objpro;
        
        
        // Create Port
        Port__c pt = new Port__c();
        pt.Name = 'Chetna';
        insert pt;
        
        //Create Inventory1..
        Inventory__c inv1 = new Inventory__c();
        inv1.Account__c=objacc.id;
        inv1.Quantity__c=10;
        inv1.Product__c = objpro.id;
      	insert inv1;
        system.debug('inv1-->'+inv1);
        
        //Create Inventory2..
        	Inventory__c inv2 = new Inventory__c();
			inv2.Account__c=objacc.id;
			inv2.Quantity__c=10;
			inv2.Product__c = objpro.id;
			insert inv2;
			system.debug('inv2-->'+inv2);
        
        
        /*List<Inventory__c> lstinv1 = new List<Inventory__c>();
			lstinv1.add(inv1);
			List<Inventory__c> lstinv2 = new List<Inventory__c>();
			lstinv2.add(inv2);*/
        
        
        
        //Create Opportunity...
        Opportunity opp = new Opportunity ();
        opp.Name = 'Chetna';
        opp.AccountId = objacc.Id;
        opp.CurrencyIsoCode = 'INR';
        opp.ForecastCategoryName = 'Commit';
        opp.StageName = 'Proforma Invoice';
        opp.Sub_Stage__c = 'Issued';
        opp.CloseDate = date.today();
        opp.Worked_by__c = 'Chetna';
        opp.Product_Name__c = objpro.Id;
        opp.Origin__c = 'O11';
        opp.Description_of_Goods__c = 'Good';
        opp.Packaging_Details__c = 'P11';
        opp.Quantity__c = 10;
        opp.H_S_Code__c = 'H11';
        opp.Total_of_Containers__c = 5;
        opp.Container_Size__c = 'Trucking';
        opp.Port_of_Discharge__c = pt.Id;
        opp.Business_Entity__c = 'TRADEASIA INDIA';
        insert opp;
        
        //Create Cost Price...
         List<Cost_Price__c> lstcostold = new List<Cost_Price__c>();
        Cost_Price__c cp = new Cost_Price__c();
        cp.Opportunity__c = opp.Id;
        cp.Business_Entity__c ='TRADEASIA INDIA';
        cp.Sub_Entity_Tradeasia__c = objacc.id;
        cp.Status_Deal__c = '1st Nego';
        cp.STATUS__c = 'EXECUTED';
        cp.Product_From_Inventory__c = inv1.Id;
        cp.UOM__c = 'KGs';
        cp.Quantity__c = 22.6;
        cp.Quantity_Per_Container__c = 10;
        cp.Total_of_containers__c = 8;
        cp.Worked_By__c = 'Chetna';
        cp.Country_of_Final_Destination__c = 'India';
        lstcostold.add(cp);
        insert lstcostold;
        system.debug('lstcostold'+lstcostold);
        List<Cost_Price__c> lstcostnew = new List<Cost_Price__c>();
        cp.Quantity_Per_Container__c = 10;
        cp.Total_of_containers__c = 7;
        lstcostold.add(cp);
        //update lstcostold;
        //system.debug('lstcostnew'+lstcostnew);*/
       
        //new list
        //List<Cost_Price__c> lstcostnew = new List<Cost_Price__c>();
        
        //update cost price
        cp.Quantity_Per_Container__c = 10;
        cp.Total_of_containers__c = 9;
        cp.Product_From_Inventory__c = inv2.Id;
       // update cp;
        lstcostnew.add(cp);
        
       
        Test.startTest();
        
        RevertQuantityTriggerHandler obj = new RevertQuantityTriggerHandler();
     
        obj.onBeforeDelete(lstcostold);
        system.debug('lstcostnew'+lstcostnew);
        system.debug('lstcostold'+lstcostold);
        obj.onAfterUpdate(lstcostnew,lstcostold);
        
        Test.stopTest();
        
    }
}