You need to sign in to do that
Don't have an account?
Horacio 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:
Now, below is my test class. Can someone review and tell me what I'm missing here? Thank you.
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; } }
Please insert the OpportunityLineItems also and then update Opportunity to fire your trigger.
Thanks,
Maharajan,C
All Answers
Please insert the OpportunityLineItems also and then update Opportunity to fire your trigger.
Thanks,
Maharajan,C
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:
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
Mahajaran - thanks I competely forgot the line item! I validated your new code in Prod and it went through succesfully. Many thanks