• Horacio Casas 19
  • NEWBIE
  • 10 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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; 
        
    }
}

 
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; 
        
    }
}