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
GreenhornGreenhorn 

Is it possible to populate test data in OpportunityHistory Object ?

To populate test data in OpportunityHistory Object I tried following code

 

        date myDate = date.newInstance(1960, 2, 17);

        Opportunity oppInst = new Opportunity(Name = 'test opp',StageName = 'Prospecting',CloseDate = myDate);

        insert oppInst;

        

        oppInst.StageName = 'Qualification';

        update oppInst;

       

        List<OpportunityHistory> oppHist = [SELECT id from OpportunityHistory];

        System.debug(' test oppHist size : ' + oppHist );

 

This works when run as anonymous code , it works.

 

When I run it from within a test class it does not work...

 

Is there any way we can populate OpportunityHistory with test data ?

 

Thanks in advance!

 


bob_buzzardbob_buzzard

Unfortunately not.  The history table is populated when the transaction containing field changes completes.  As you can't complete transactions when testing, it isn't possible to populate the table.  

GreenhornGreenhorn

Thank you bob for your reply.