You need to sign in to do that
Don't have an account?

Writing test case for simple trigger
I am brand new to Apex and need a litle help writing my first test case. The trigger to be tested is below (areas in red are what my test coverage is missing):
trigger OpportunityUpdateAccount on Opportunity(after insert, after update) { List<Account> accsToUpdate = new List<Account>(); for(Opportunity opp : trigger.new) {if (opp.StageName == '4 - Closed Won' && opp.Hosting__c > 0) accsToUpdate.add(new Account(Id=opp.AccountID, Type = 'Customer')); } if (accsToUpdate != null && !accsToUpdate.isEmpty()) Database.update(accsToUpdate); }
Test case below:
@isTest private class OpportunityUpdateAccountTest{ static testMethod void testOpportunityUpdate(){ //Setup the Account record Account a = new Account(Name = 'Test Account'); a.Type = 'Customer'; insert a; // Verify that the initial state is as expected a = [SELECT Name, Type FROM Account Where Id = :a.Id]; System.assertEquals ('Customer', a.Type); // setup the Opportunity record String opportunityName = 'Test Opportunity'; Opportunity o = new Opportunity(AccountId=a.Id, name=opportunityName, StageName='4 - Closed Won', CloseDate = Date.today() ); //Cause the Trigger to execute insert o; //Verify the results a = [SELECT Name, Type FROM Account WHERE Id = :a.Id]; } }
A key thing that I am missing from my trigger is adding a Product to the Opportunity. This effects the field 'Hosting__c'. This is a rollup summary field of the total price of the Products under the Product Family "Hosting", that are or course added to the Opportunity, How would I go about adding in a Product line item in my test case so that I can make my test meet the criteria of 'Hosting__c > 45'?
Finally figured this out. I forgot to specify the Product2Id on my Opportunity...Hence the error I was getting. Final code:
Thanks for all the help, everyone!
All Answers
Hey again !
This bit of code should do it :
OpportunityLineItem optLI = new OpportunityLineItem(OpportunityId = o.Id, Quantity=1, TotalPrice = 1, PriceBookEntryId = [Select Id from PriceBookEntry LIMIT 1].Id);
insert optLI;
Try adding the highligthed lines to your code and see if it works for you.
Hope this helps you/
That definitely has me on the rigth track, but I think I am misunderstanding the use of the PriceBookEntryId. We only have the default Standard Price Book. I have seen examples of this used in other triggers, but how do I go about finding the appropriate API name to call it?
The best way is through eclipse IDE
Finally figured this out. I forgot to specify the Product2Id on my Opportunity...Hence the error I was getting. Final code:
Thanks for all the help, everyone!
Hi Stollmeyera
Could you please tell me from where you are fetching the PricebookentryId and Pricebook2ID....The ids u have used in these two fields in the test case above is coming from Products object?
I need to implement the same in my Org...
Thanks
Shv
Hi Stollmeyera
Could you please tell me from where you are fetching the PricebookentryId and Pricebook2ID....The ids u have used in these two fields in the test case above is coming from Products object?
I need to implement the same in my Org...
Thanks
Shv
@backup
I believe I used the Excel Connector, which allows you to access the pricebook table directly and view the ids
More info here: http://www.salesforce.com/community/crm-best-practices/administrators/data-management/data-tools/excel-connector.jsp