+ Start a Discussion
The FinnsterThe Finnster 

Help with Test Class to automate opportunity product end date

Hi,

I have a trigger that automates the opportunity end date based on the start date and term on the opportunity product.

I need help with the test class to achive 75% coverage to push it to production please.

Any help would be appreciated.

trigger OpportunityProductEndDate on OpportunityLineItem (before insert, before update, before delete) {
    Set<Id> opportunityProduct = new Set<Id>();
    Set<id> idSet = new Set<id>();
    Set<id> OppidSet = new Set<id>();
    List<Revenue__c> revenueList = new List<Revenue__c>();
    if((Trigger.isInsert || Trigger.isUpdate) && Trigger.isBefore){
    for(OpportunityLineItem opli: Trigger.New){
            idSet.add(opli.product2Id); 
            OppidSet.add(opli.OpportunityId);
            System.debug('-------OpportunityRecordType__c '+opli.OpportunityRecordType__c);
            System.debug('-------- contract start date'+ opli.Contract_Start_Date__c);
            System.debug('-------- contract start date'+ opli.Contract_Term__c);
            
            if(opli.OpportunityRecordType__c == 'Display-Direct' || opli.OpportunityRecordType__c == 'Display-Agency'){      
           if( opli.Start_Date__c != null && opli.Product_Family__c == 'Display-Developer Listings' ){
                    opli.End_Date__c = opli.Start_Date__c .addMonths(Integer.valueOf(opli.Term__c))-1;
            }
            
            }
            //if(opli.OpportunityRecordType__c != 'Display-Direct' && opli.OpportunityRecordType__c != 'Display-Agency' && opli.OpportunityRecordType__c != 'Jobs Opportunities' && opli.OpportunityRecordType__c != 'Jobs Dubai' && opli.OpportunityRecordType__c != 'Job Amend'){
            if(opli.OpportunityRecordType__c != 'Display-Direct' && opli.OpportunityRecordType__c != 'Display-Agency'){
                System.debug('recordType check');
                if(opli.Term__c == null && opli.OpportunityRecordType__c != 'Jobs Opportunities' && opli.OpportunityRecordType__c != 'Jobs Dubai' && opli.OpportunityRecordType__c != 'Jobs Amend'){
                    opli.Term__c = String.ValueOf(opli.Contract_Term__c); 
                }
                /*if(opli.Term__c == null && (opli.OpportunityRecordType__c == 'Jobs Opportunities' || opli.OpportunityRecordType__c == 'Jobs Dubai' || opli.OpportunityRecordType__c == 'Jobs Amend')){
                    opli.addError('Please select Term.');   
                }*/
                if( opli.Start_Date__c == null && opli.Contract_start_date__c != null){
                    opli.Start_Date__c = opli.Contract_start_date__c;
                }
                if( opli.Start_Date__c != null && opli.Term__c != null){
                    opli.End_Date__c = opli.Start_Date__c.addMonths(Integer.valueOf(opli.Term__c)).addDays(-1);
                    System.debug('-------'+ opli.End_Date__c); 
                }
          
          }  
        }
    } 
    if((Trigger.isInsert || Trigger.isUpdate) && Trigger.isBefore){
    if(idSet != null && idSet.size()> 0){
        //Calculation for jobs Opportunities Product's End Date.
        map<id,Product2> ProductIdVSDecimalMap = new map<id,Product2>();    
        map<id,String> OpportunityIdVSPaymentTypeMap = new map<id,String>();    
        for(product2 pro : [SELECT id,Duration__c,Duration_in__c from product2 where id IN: idSet]){
            ProductIdVSDecimalMap.put(pro.id,pro);    
        }
        system.debug('**ProductIdVSDecimalMap**' +ProductIdVSDecimalMap);
        for(Opportunity opp : [SELECT id,payment_type__c FROM Opportunity WHERE id IN : OppidSet]){
            OpportunityIdVSPaymentTypeMap.put(opp.id,opp.Payment_Type__c);      
        }
        system.debug('**OpportunityIdVSPaymentTypeMap**' +OpportunityIdVSPaymentTypeMap);
        for(OpportunityLineItem opli: Trigger.New){
            system.debug(opli.start_date__c);
            system.debug('opli.opportunityid' +opli.opportunityid);
            system.debug('OpportunityIdVSPaymentTypeMap-PaymentType' +OpportunityIdVSPaymentTypeMap.get(opli.opportunityid));
            if(OpportunityIdVSPaymentTypeMap.get(opli.opportunityid) != 'Offline' && (opli.OpportunityRecordType__c == 'Jobs Opportunities' || opli.OpportunityRecordType__c == 'Job Amend') && opli.start_date__c != null && ProductIdVSDecimalMap.containsKey(opli.Product2Id) != null){
                if(ProductIdVSDecimalMap.get(opli.Product2Id).Duration_in__c == 'M'){
                    opli.End_date__c = opli.start_date__c.addMonths(ProductIdVSDecimalMap.get(opli.Product2Id).Duration__c.intValue()) - 1; 
                    system.debug('**opli.End_date__c**' +opli.End_date__c);  
                    String str = String.valueOf(ProductIdVSDecimalMap.get(opli.Product2Id).Duration__c);
                    opli.Term__c = str;
                    system.debug('**opli.Term__c**' +opli.Term__c); 
                }
                else if(ProductIdVSDecimalMap.get(opli.Product2Id).Duration_in__c == 'D'){
                    opli.End_date__c = opli.start_date__c.addDays(ProductIdVSDecimalMap.get(opli.Product2Id).Duration__c.intValue()); 
                    system.debug('**opli.End_date__c===**' +opli.End_date__c);   
                    opli.Term__c = '1';
                    system.debug('**opli.Term__c===**' +opli.Term__c); 
                }
                
            }      
        }    
    } 
}

    if(Trigger.isBefore && Trigger.isDelete ){
        for(OpportunityLineItem OLI:Trigger.Old){
                opportunityProduct.add(OLI.id);
         }   
        revenueList=[SELECT id FROM Revenue__c WHERE OpportunityLineItemID__c in :opportunityProduct];
        system.debug('revenueList-->'+revenueList.size());    
        if(revenueList.size()>0){
        delete revenueList;    
        }   
    }            
}
 
Suraj Tripathi 47Suraj Tripathi 47

Hi,

Please find the solution.

@isTest
public class OpportunityProductEndDateTest {

    @isTest
    public static void OpportunityProductEndTest()
    {
	    // fill all the fields accordingly
Product2 p = new Product2();
p.Name = ' Test Product ';
p.Description='Test Product Entry For Product';
p.productCode = 'SFDCPanther-123';
p.isActive = true;
p.Duration__c='M';
p.Duration_in__c ='D';
insert p;

// insert pricebook entry for the product
PricebookEntry standardPrice = new PricebookEntry();
standardPrice.Pricebook2Id = Test.getStandardPricebookId();
standardPrice.Product2Id = p.Id;
standardPrice.UnitPrice = 100;
standardPrice.IsActive = true;
standardPrice.UseStandardPrice = false;
insert standardPrice ;

// insert account
Account acc = new Account(
Name = 'SFDCPanther.com',
Rating = 'Hot',
Industry = 'Banking',
Phone = '9087654321'
);
insert acc;

// Create Opportunity
Opportunity opp = new Opportunity();
opp.AccountId = acc.Id;
opp.Name = 'Test Opportunity';
opp.CloseDate= System.Today();
opp.StageName='Prospecting';
opp.Pricebook2Id = Test.getStandardPricebookId();
opp.payment_type__c=
insert opp;

// Add product and Pricebook to the particular opportunity using OpportunityLineItem 
OpportunityLineItem oppLineItem = new OpportunityLineItem();
oppLineItem.OpportunityId = opp.Id;
oppLineItem.PricebookEntryId = standardPrice.Id;
oppLineItem.UnitPrice = 7000;
oppLineItem.Quantity = 5;
oppLineItem.OpportunityRecordType__c='Display-Direct';
oppLineItem.OpportunityRecordType__c='Display-Agency';
oppLineItem.Start_Date__c =Date.Today();
oppLineItem.Product_Family__c ='Display-Developer Listings';
oppLineItem.Term__c='1';//fill this field accordingly
oppLineItem.Contract_start_date__c=Date.Today()+1; 
insert oppLineItem;


OpportunityLineItem oppLineItem1 = new OpportunityLineItem();
oppLineItem1.OpportunityId = opp.Id;
oppLineItem1.PricebookEntryId = standardPrice.Id;
oppLineItem1.UnitPrice = 7000;
oppLineItem1.Quantity = 5;
oppLineItem1.OpportunityRecordType__c='Job Amend';
oppLineItem1.OpportunityRecordType__c='Display-Agency';
oppLineItem1.Start_Date__c =Date.Today();
oppLineItem1.Product_Family__c ='Display-Developer Listings';
oppLineItem1.Term__c='1';//fill this field accordingly
oppLineItem1.Contract_start_date__c=Date.Today()+1; 
insert oppLineItem;
	
	
	oppLineItem.OpportunityRecordType__c='Job Amend';
	update oppLineItem;
	
	delete oppLineItem;
	
	}
	
	}

Please do the needful changes according to your code.

Please mark it as the Best Answer so that other people would take references from it.

Thank You