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
David Wilcox 8David Wilcox 8 

How to populate ForecastingItem in Test Class

I'm working on a Test Class for an Apex class that inserts ForecastingItem records into a target object (very similar to a reporting snapshot, but with more flexibility). My issue is I'm getting zero code coverage on any line related to the ForecastingItem object. ForecastingItem is read-only, so my thought process is to populate it by mimicking what we would do through the UI (set User Forecasting Allowed = True, setting the correct role, setting a quota, doing a manual adjustment, etc), but there is a disconnect somewhere. 

Anyone know of any tips to point me in the right direction?
Thank you! 

@isTest
global class testForecastSnapshot{
    static testMethod void myUnitTest(){
                         
        User usr = [SELECT Id, Name,
                    UserRole.Name,Profile.Name 
                    FROM User 
                    WHERE ForecastEnabled=TRUE 
                    and IsActive=TRUE
                    and UserRole.Name='CEEMEA_Hunters'
                    limit 1];
                 
        
        Account a = new Account(
                OwnerId=usr.id,
                Name='Account Name', 
                website='www.example.com',
                Total_Annual_Revenue__c='$2B+',
                Delivery_Model__c='SaaS'
            );
                   
        insert a;
       
        Opportunity opp1 = new Opportunity (
            OwnerID=usr.id,
            Name='Tuxedo Party', 
            AccountId=a.ID,
            CloseDate= date.newInstance(2017, 11, 21),
            Deal_type__c ='New',
            StageName='Negotiation',
            Amount=1000,
            Term_of_Contract_Months__c=12
        );
          
        insert opp1;
          
        
        ForecastingQuota quota = new ForecastingQuota(
            QuotaAmount = 300,
            QuotaOwnerId = usr.id,            
            ForecastingTypeId = [SELECT Id FROM ForecastingType WHERE DeveloperName='OpportunityRevenue'].id,
            StartDate= Date.newinstance(2017,10,01)
        );               
        
        insert quota;  


       ForecastingOwnerAdjustment fcOwnAdj1= new ForecastingOwnerAdjustment(
            ForecastingItemCategory='BestCaseForecast',
            OwnerAdjustedAmount=100,
            OwnerAdjustmentNote='mynote',
            StartDate= Date.newinstance(2017,10,01),
            ForecastOwnerId=usr.id,
            ForecastingTypeId = [SELECT Id FROM ForecastingType WHERE DeveloperName='OpportunityRevenue'].id
                       
        );  
        
        insert fcOwnAdj1; 

        
        //this return value is empty, ideally it would contain the 1000 record from the opportunity above      
        system.debug([Select Id FROM ForecastingItem]);
    
    }

}