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
ShirinShirin 

Help on test class

Hi ,

 

I have written a code below to copy data from opportunity line item schedule to a custom object 'Forecast'. The code is as below, Can anybody help me write the test class. I have also copied the test class here. The coverage is only 30%.

 

 

trigger RevenueForecastInsertFinal on OpportunityLineItem (After Insert) {
 For (OpportunityLineItem OppLi : Trigger.New)
    {
list <OpportunityLineItemSchedule> schedule = [select Id, ScheduleDate,Revenue from 
                                               OpportunityLineItemSchedule where
                                               OpportunityLineItemId = :OppLi.Id ];

List <Forecast__c> F = New List<Forecast__c> ();
For( Integer i=0; i< (schedule.size()) ;i++)
{
             F.add(New Forecast__c (Amount__c = schedule[i].Revenue, 
                               Record_Id_BE__c = OppLi.Id, 
                               Date__c = schedule[i].ScheduleDate,
                               Status__c = OppLi.Status_Category__c,
                               Status_Category__c = OppLi.Status__c,
                               Forecast_Owner__c = OppLi.Opportunity_Owner_Id__c,
                               Business_Unit_Head__c = OppLi.Business_Unit_Head_Id__c,
                               Business_Unit__c = OppLi.Resource_Business_Unit__c,
                               Opportunity_Type__c = 'T&M',
                               Related_Account__c = OppLi.Account_Id__c,
                               Owner_Region__c = OppLi.Owner_Region__c,
                               Related_Opportunity__c = OppLi.Opportunity_IdNumber__c));

}
    
    if(schedule.size()!=0) 
    insert F;
}
}

 

 

 

Test Class:

@IsTest
private class TestRevenueForecastInsertFinal {

static testmethod void TestRevenueForecastInsertFinal(){

        Opportunity o = new Opportunity(
            name='test',
            stageName='Open',
            currencyisocode = 'INR',
            Number_of_Resources__c = 4,
            CloseDate=Date.newInstance(2006,10,10));
        insert o;
        
Pricebookentry pbe = [select Id from Pricebookentry where currencyisocode=:o.currencyisocode];  

OpportunityLineItem OppLi = new OpportunityLineItem(From_Date__c = date.today(),
                   Quantity = 1,UnitPrice = 1000, opportunityid=o.id,pricebookentryid=pbe.id);

Insert OppLi;

OpportunityLineItemSchedule OppLis = new OpportunityLineItemSchedule(OpportunityLineItemId = OppLi.Id,
                                    Type = 'Revenue',ScheduleDate = date.today(), Revenue = 1000);
Insert OppLis;



Forecast__c F = new Forecast__c(Date__c = date.today(),Status__c = 'Closed',Status_Category__c = 'Resume Identified',
                Business_Unit__c = 'Communication',
                Opportunity_Type__c = 'T&M',Owner_Region__c = 'India',Amount__c = 1000);
                
                
Insert F;
}
}

Shashikant SharmaShashikant Sharma

Hi Shirin,

 

First your trigger is not written correctly.It needed to be optimized as it has an SOQL in iteration(for loop) and it will fail in case of trigger execution for bulk records.Best practice is not to have any SOQL in loop.

 

Please see How to save limits in trigger : http://forceschool.blogspot.com/2011/05/writing-apex-trigger-save-limits-in.html

 

In case you are assure that your trigger will never run for bulk records. Now your trigger coverage can not be increased as because :

 

Your trigger works : on After Insert of OpportunityLineItem

 

You have a for loop for OpportunityLineItemSchedule

 

For( Integer i=0; i< (schedule.size()) ;i++)

 

This OpportunityLineItemSchedule has a reference to OpportunityLineItem, now how can you find this OpportunityLineItemSchedule list related to OpportunityLineItem record after insert. I think your trigger needed to be created on after update of OpportunityLineItem or on after insert of OpportunityLineItemSchedule. If you still hae any problem please ask me.