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
Horacio Casas 19Horacio Casas 19 

Simple Trigger - Test Class Help Needed

Hello everyone, I'm working on writing my first trigger and test class and am coming up short on the test class. My trigger is very simple, it takes all of the related Product names from an Opportunity and puts the names in a string on a field on the Opp separated by semicolons:
 
Trigger ProductList on Opportunity (before update, after delete) {

list<opportunity> sl = trigger.new;

list<opportunityLineItem> slnu = new list<opportunityLineItem>([select id ,product2.name, opportunityId from opportunitylineitem where opportunityId =: trigger.new[0].id]);

string productName='';

for(opportunityLineItem opp : slnu){

 productName += opp.product2.name +'; '; // + operator for concatenation.

}

for(Opportunity opp : trigger.new){

 opp.Product_Summary__c = productName;


}


}

Now, below is my test class. Can someone review and tell me what I'm missing here? Thank you.
@isTest
public class ProductListTestClass {
    
static testMethod void ProductListTestClass() {
        Account a = new Account(Name='testAccount');
        insert a; 
    
        Contact c = new Contact(LastName='testContact',
                               AccountId=a.Id,
                               Contact_Status__c='Prospect',
                               Email = 'xyz@me.com',
                               Function__c = 'Analytics (HCS)');
        insert c; 
    
        Product2 prod = new Product2(Name = 'Product');
        insert prod;
        
        Id pricebookId = Test.getStandardPricebookId();
        
        PricebookEntry standardPrice = new PricebookEntry(
            Pricebook2Id = pricebookId, Product2Id = prod.Id,
            UnitPrice = 10000, IsActive = true);
        insert standardPrice;
        
        
        Pricebook2 customPB = new Pricebook2(Name='Custom 2020', isActive=true);
        insert customPB;
        
        PricebookEntry customPrice = new PricebookEntry(
            Pricebook2Id = customPB.Id, Product2Id = prod.Id,
            UnitPrice = 12000, IsActive = true);
        insert customPrice;
        
        
        Opportunity opp = new Opportunity(Name='Test', 
                                          Account = a, 
                                          PriceBook2 = customPB,
                                          CloseDate = System.today(),
                                          Contract_Start_Date__c = System.today(),
                                          AccountId=a.Id, 
                                          Type = 'New Deal',
                                          Contact__c = c.Id,
                                          LeadSource = 'Client Referral',
                                          Function__c = 'Clinical',
                                          StageName = 'Active', 
                                          Probability = 5);
       
       insert opp; 
        
    }
}

 
Best Answer chosen by Horacio Casas 19
Maharajan CMaharajan C
Hi casas,

Please insert the OpportunityLineItems also and then update Opportunity to fire your trigger.
 
@isTest
public class ProductListTestClass {
    
static testMethod void ProductListTestClass() {
        Account a = new Account(Name='testAccount');
        insert a; 
    
        Contact c = new Contact(LastName='testContact',
                               AccountId=a.Id,
                               Contact_Status__c='Prospect',
                               Email = 'xyz@me.com',
                               Function__c = 'Analytics (HCS)');
        insert c; 
    
        Product2 prod = new Product2(Name = 'Product');
        insert prod;
        
        Id pricebookId = Test.getStandardPricebookId();
        
        PricebookEntry standardPrice = new PricebookEntry(
            Pricebook2Id = pricebookId, Product2Id = prod.Id,
            UnitPrice = 10000, IsActive = true);
        insert standardPrice;
        
        
        Pricebook2 customPB = new Pricebook2(Name='Custom 2020', isActive=true);
        insert customPB;
        
        PricebookEntry customPrice = new PricebookEntry(
            Pricebook2Id = customPB.Id, Product2Id = prod.Id,
            UnitPrice = 12000, IsActive = true);
        insert customPrice;
        
        
        Opportunity opp = new Opportunity(Name='Test', 
                                          Account = a, 
                                          PriceBook2 = customPB,
                                          CloseDate = System.today(),
                                          Contract_Start_Date__c = System.today(),
                                          AccountId=a.Id, 
                                          Type = 'New Deal',
                                          Contact__c = c.Id,
                                          LeadSource = 'Client Referral',
                                          Function__c = 'Clinical',
                                          StageName = 'Active', 
                                          Probability = 5);
       
        insert opp; 
	   
	    List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
        OpportunityLineItem oli = new OpportunityLineItem(
            OpportunityId = opp.Id,
            Quantity = 5,
            PricebookEntryId = standardPrice.Id,
            TotalPrice = 5 * standardPrice.UnitPrice
        );
        OpportunityLineItem oli1 = new OpportunityLineItem(
            OpportunityId = opp.Id,
            Quantity = 5,
            PricebookEntryId = customPrice.Id,
            TotalPrice = 5 * customPrice.UnitPrice
        );
        oliList.add(oli);
        oliList.add(oli1);

        insert oli;
        
        opp.Name = 'test 1';
        Test.startTest();
        	update opp;
        Test.stopTest();
        system.assertEquals('test 1', opp.Name);
		// Add Some More Assesrt Statements here
    }
}


Thanks,
Maharajan,C

All Answers

Maharajan CMaharajan C
Hi casas,

Please insert the OpportunityLineItems also and then update Opportunity to fire your trigger.
 
@isTest
public class ProductListTestClass {
    
static testMethod void ProductListTestClass() {
        Account a = new Account(Name='testAccount');
        insert a; 
    
        Contact c = new Contact(LastName='testContact',
                               AccountId=a.Id,
                               Contact_Status__c='Prospect',
                               Email = 'xyz@me.com',
                               Function__c = 'Analytics (HCS)');
        insert c; 
    
        Product2 prod = new Product2(Name = 'Product');
        insert prod;
        
        Id pricebookId = Test.getStandardPricebookId();
        
        PricebookEntry standardPrice = new PricebookEntry(
            Pricebook2Id = pricebookId, Product2Id = prod.Id,
            UnitPrice = 10000, IsActive = true);
        insert standardPrice;
        
        
        Pricebook2 customPB = new Pricebook2(Name='Custom 2020', isActive=true);
        insert customPB;
        
        PricebookEntry customPrice = new PricebookEntry(
            Pricebook2Id = customPB.Id, Product2Id = prod.Id,
            UnitPrice = 12000, IsActive = true);
        insert customPrice;
        
        
        Opportunity opp = new Opportunity(Name='Test', 
                                          Account = a, 
                                          PriceBook2 = customPB,
                                          CloseDate = System.today(),
                                          Contract_Start_Date__c = System.today(),
                                          AccountId=a.Id, 
                                          Type = 'New Deal',
                                          Contact__c = c.Id,
                                          LeadSource = 'Client Referral',
                                          Function__c = 'Clinical',
                                          StageName = 'Active', 
                                          Probability = 5);
       
        insert opp; 
	   
	    List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
        OpportunityLineItem oli = new OpportunityLineItem(
            OpportunityId = opp.Id,
            Quantity = 5,
            PricebookEntryId = standardPrice.Id,
            TotalPrice = 5 * standardPrice.UnitPrice
        );
        OpportunityLineItem oli1 = new OpportunityLineItem(
            OpportunityId = opp.Id,
            Quantity = 5,
            PricebookEntryId = customPrice.Id,
            TotalPrice = 5 * customPrice.UnitPrice
        );
        oliList.add(oli);
        oliList.add(oli1);

        insert oli;
        
        opp.Name = 'test 1';
        Test.startTest();
        	update opp;
        Test.stopTest();
        system.assertEquals('test 1', opp.Name);
		// Add Some More Assesrt Statements here
    }
}


Thanks,
Maharajan,C
This was selected as the best answer
Abhishek BansalAbhishek Bansal
Hi Horacio,

Your trigger will execute on before update and before delete context which you have not added in your test class. Please update the test class below:
@isTest
public class ProductListTestClass {
    
static testMethod void ProductListTestClass() {
        Account a = new Account(Name='testAccount');
        insert a; 
    
        Contact c = new Contact(LastName='testContact',
                               AccountId=a.Id,
                               Contact_Status__c='Prospect',
                               Email = 'xyz@me.com',
                               Function__c = 'Analytics (HCS)');
        insert c; 
    
        Product2 prod = new Product2(Name = 'Product');
        insert prod;
        
        Id pricebookId = Test.getStandardPricebookId();
        
        PricebookEntry standardPrice = new PricebookEntry(
            Pricebook2Id = pricebookId, Product2Id = prod.Id,
            UnitPrice = 10000, IsActive = true);
        insert standardPrice;
        
        
        Pricebook2 customPB = new Pricebook2(Name='Custom 2020', isActive=true);
        insert customPB;
        
        PricebookEntry customPrice = new PricebookEntry(
            Pricebook2Id = customPB.Id, Product2Id = prod.Id,
            UnitPrice = 12000, IsActive = true);
        insert customPrice;
        
        
        Opportunity opp = new Opportunity(Name='Test', 
                                          Account = a, 
                                          PriceBook2 = customPB,
                                          CloseDate = System.today(),
                                          Contract_Start_Date__c = System.today(),
                                          AccountId=a.Id, 
                                          Type = 'New Deal',
                                          Contact__c = c.Id,
                                          LeadSource = 'Client Referral',
                                          Function__c = 'Clinical',
                                          StageName = 'Active', 
                                          Probability = 5);
       
       insert opp; 
       
	   Test.startTest();
	   opp.Name = 'Update Name';
	   update opp;
	   Test.stopTest();
	   
    }
}

Let me know if this works for you.

Note: You are using trigger on after delete and at line 4 you are using trigger.new variable in your query so this trigger will throw an error as trigger.new is not available on delete case. Please update the trigger code accordingly.

Thanks,
Abhishek Bansal.
Gmail: abhibansal2790@gmail.com
Skype: abhishek.bansal2790
Phone: +917357512102
Horacio Casas 19Horacio Casas 19
Thank you Maharajan and Abhishek. 

Mahajaran - thanks I competely forgot the line item! I validated your new code in Prod and it went through succesfully. Many thanks