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
Cubs 42Cubs 42 

i broke my salesforce. need 1% code coverage on this trigger quickly and i dont know where to start

i broke my salesforce. need 1% code coverage on this trigger quickly and i dont know where to start

my trigger
 
/*
 * @description Trigger for OpportunityLineItem - this trigger has no test class 
 * @author Tristan Harley 
 * @date 9.MAR.2017
 */

trigger OpportunityLineItemsTrigger on OpportunityLineItem (before insert, before update)

{
    //create list of related object ids 
    Set<string> setOppId = new Set<String>();
    Set<string> setProdId = new Set<String>();
    
    //Populate the lists related object ids to the OpportunityLineItem 
    for( OpportunityLineItem opli : trigger.New )
    {
        setOppId.add(opli.opportunityid);
        setProdId.add(opli.product2id);
    }
    
    //Check if related object ids are populated
    if( setOppId.size() > 0 && setProdId.size() > 0)
    {
        //Map the related object from the lists 
        Map<Id,Opportunity> mapOpp = new  Map<Id,Opportunity> ([select id ,Licensed_Months__c from Opportunity where id in :setOppId ] );
        Map<Id,Product2> mapProd = new  Map<Id,Product2> ([select id ,e3_Product_License__c ,Partners_Share__c from Product2 where id in :setProdId] );
        
        //loop through the OpportunityLineItem that where trigged
        for( OpportunityLineItem opli : trigger.New )
        {
            //Set Defalts on the OpportunityLineItem that where trigged
            if(Trigger.isInsert)
            { 
                opli.Discount = 0;
            }
            
            //check if there is a opportunity related to the OpportunityLineItem
            if(Trigger.isInsert)
            {  
                if(mapOpp.containsKey(opli.opportunityid) )
                {
                    //Match the related record
                    Opportunity opp = mapOpp.get(opli.opportunityid);
                    
                    //Set fields on the OpportunityLineItem that where trigged 
                    System.debug('----------->'+opp.Licensed_Months__c); 
                    opli.Licensed_Months__c = opp.Licensed_Months__c;
                              
                }
            }
            
             //check if there is a product related to the OpportunityLineItem
            if(Trigger.isInsert)
            {
                if(mapProd.containsKey(opli.product2id) )
                {
                    //Match the related record
                    Product2 prod = mapProd.get(opli.product2id);
                    
                    //Set fields on the OpportunityLineItem that where trigged
                    System.debug('----------->'+prod.e3_Product_License__c);
                    opli.e3_Product_Licensing__c = prod.e3_Product_License__c;
                    
                    System.debug('----------->'+prod.Partners_Share__c );
                    opli.Partners_Share__c = prod.Partners_Share__c ;
                              
                }
            }
        } 
    }
 
}

 
Best Answer chosen by Cubs 42
Suraj TripathiSuraj Tripathi

Hi Cubs,

Please create apex class NAMED: (OpportunityLineItemsTrigger_Test)

And Paste this code 

@isTest
public class OpportunityLineItemsTrigger_Test {
    static testmethod void Test(){
        Test.startTest();
        Opportunity opp = new Opportunity();
        opp.Name='Test';
        opp.CloseDate=date.today()+2;
        opp.StageName='Prospecting';
        insert opp;
        
        Product2 prdct = new Product2();
        prdct.Name = 'ProductForTest' ;
        prdct.ProductCode = '0' ; 
        prdct.IsActive = true;
        insert prdct;
        
        Pricebook2 pb = new Pricebook2();
        pb.IsActive=true;
        pb.Name='pb name';
        insert pb;
        
        PricebookEntry PBEntry = new PricebookEntry();
        PBEntry.Pricebook2Id=Test.getStandardPricebookId();
        PBEntry.Product2Id=prdct.Id;
        PBEntry.IsActive=true;
        PBEntry.UnitPrice=11;
        insert PBEntry;
        
        PricebookEntry PBEntry1 = new PricebookEntry();
        PBEntry1.Pricebook2Id=pb.Id;
        PBEntry1.Product2Id=prdct.Id;
        PBEntry1.IsActive=true;
        PBEntry1.UnitPrice=11;
        insert PBEntry1;
        
        OpportunityLineItem oppItems = new OpportunityLineItem();
        oppItems.PricebookEntryId = PBEntry1.Id;
        oppItems.OpportunityId = opp.id;
        oppItems.Quantity = 5;
        oppItems.UnitPrice = 10;
        insert oppItems;
        Test.stopTest();
    }
}


It cover your trigger code.

Marks as a best if it helps you.

Regards,

Suraj

All Answers

Suraj TripathiSuraj Tripathi

Hi Cubs,

Please create apex class NAMED: (OpportunityLineItemsTrigger_Test)

And Paste this code 

@isTest
public class OpportunityLineItemsTrigger_Test {
    static testmethod void Test(){
        Test.startTest();
        Opportunity opp = new Opportunity();
        opp.Name='Test';
        opp.CloseDate=date.today()+2;
        opp.StageName='Prospecting';
        insert opp;
        
        Product2 prdct = new Product2();
        prdct.Name = 'ProductForTest' ;
        prdct.ProductCode = '0' ; 
        prdct.IsActive = true;
        insert prdct;
        
        Pricebook2 pb = new Pricebook2();
        pb.IsActive=true;
        pb.Name='pb name';
        insert pb;
        
        PricebookEntry PBEntry = new PricebookEntry();
        PBEntry.Pricebook2Id=Test.getStandardPricebookId();
        PBEntry.Product2Id=prdct.Id;
        PBEntry.IsActive=true;
        PBEntry.UnitPrice=11;
        insert PBEntry;
        
        PricebookEntry PBEntry1 = new PricebookEntry();
        PBEntry1.Pricebook2Id=pb.Id;
        PBEntry1.Product2Id=prdct.Id;
        PBEntry1.IsActive=true;
        PBEntry1.UnitPrice=11;
        insert PBEntry1;
        
        OpportunityLineItem oppItems = new OpportunityLineItem();
        oppItems.PricebookEntryId = PBEntry1.Id;
        oppItems.OpportunityId = opp.id;
        oppItems.Quantity = 5;
        oppItems.UnitPrice = 10;
        insert oppItems;
        Test.stopTest();
    }
}


It cover your trigger code.

Marks as a best if it helps you.

Regards,

Suraj

This was selected as the best answer
Cubs 42Cubs 42
holy toledo batman, 100% code coverage. brillant.

i really need to learn to make test classes.
haha
thanks so much

tristan
 
Suraj TripathiSuraj Tripathi
Thanks for selecting my answer as a best
Cubs 42Cubs 42
@suraj

thanks again this was duplicated and customised again to solve a new problem