You need to sign in to do that
Don't have an account?
Alex Merwin
Help creating an APEX Test Class for Opportunity Triggers
Hey guys -
I'm struggling to develop a Test Case for three triggers I need to promote on the Opportunity object.
New to APEX and appreciate any help!
Here is where I've gotten for the Test Case:
And the three Opportunity Triggers.
I'm struggling to develop a Test Case for three triggers I need to promote on the Opportunity object.
New to APEX and appreciate any help!
Here is where I've gotten for the Test Case:
@isTest public class TestOLITriggers { static testMethod void insertNewOpportunity() { Opportunity opportunityToCreate = new Opportunity(); // Do you recognize these fields? opportunityToCreate.StageName = 'Sourcing Demand'; opportunityToCreate.CloseDate = '2016-01-01'; opportunityToCreate.Account = '0015000000tVJRS'; opportunityToCreate.Name = 'Test Opportunity Triggers'; // Don't worry about these opportunityToCreate.TimeZoneSidKey = 'America/Denver'; opportunityToCreate.LocaleSidKey = 'en_US'; opportunityToCreate.EmailEncodingKey = 'UTF-8'; opportunityToCreate.LanguageLocaleKey = 'en_US'; insert opportunityToCreate; } }
And the three Opportunity Triggers.
trigger OLIUpdate on Opportunity (after update) { Set<Id> oppIds = new Set<Id>(); RecordType rt = [ select Id from RecordType where Name = 'Strategic Audience Plan' ]; for (Opportunity opp : Trigger.new) { Opportunity oldOpp = Trigger.oldMap.get(opp.Id); if ( opp.StageName == 'Notify Demand' && oldOpp.StageName != opp.StageName && opp.RecordTypeId == rt.Id ) { oppIds.add(opp.Id); } } if (!oppIds.isEmpty()) { List<OpportunityLineItem> oliList = [ select Campaign_Status__c, INTL_Demand_Approval__c from OpportunityLineItem where OpportunityId in :oppIds ]; for (OpportunityLineItem oli : oliList) { if (oli.INTL_Demand_Approval__c != 'Local Demand must approve buyer before Launch Ticket can be deployed') { oli.Campaign_Status__c = 'Buyer Approved, Ready to Launch'; } else { oli.Campaign_Status__c = 'Buyer Approved, Pending Local Demand Approval'; } } update oliList; } }
trigger OLIUpdate_SAP_SendLaunchTickets on Opportunity (after update) { Set<Id> oppIds = new Set<Id>(); for (Opportunity opp : Trigger.new) { Opportunity oldOpp = Trigger.oldMap.get(opp.Id); if ( opp.StageName == 'Deploy Launch Tickets' && oldOpp.StageName != opp.StageName ) { oppIds.add(opp.Id); } } if (!oppIds.isEmpty()) { List<OpportunityLineItem> oliList = [ select Campaign_Status__c from OpportunityLineItem where OpportunityId in :oppIds ]; for (OpportunityLineItem oli : oliList) { oli.Campaign_Status__c = 'Buyer Approved, Ready to Launch'; } update oliList; } }
trigger OLIUpdate_SAP_BuyerApproval on Opportunity (after update) { Set<Id> oppIds = new Set<Id>(); for (Opportunity opp : Trigger.new) { Opportunity oldOpp = Trigger.oldMap.get(opp.Id); if ( opp.StageName == 'Responded to Buyer' && oldOpp.StageName != opp.StageName ) { oppIds.add(opp.Id); } } if (!oppIds.isEmpty()) { List<OpportunityLineItem> oliList = [ select Campaign_Status__c from OpportunityLineItem where OpportunityId in :oppIds ]; for (OpportunityLineItem oli : oliList) { oli.Campaign_Status__c = 'Buyer Approval Needed'; } update oliList; } }
NOTE: This code has not been tested and may contain typographical or logical errors
I would recommend that you have a test class per trigger and that you also read over this document [1] to look at the additional tests you should be writing. Take this test above as a template. If you have any specific problems with your tests please let us know.
[1] http://blog.deadlypenguin.com/blog/testing/strategies/
I think there may be a typo in the template code, I'm getting this error:
Compile Error: unexpected token: ')' at line 19 column 8
We have a lot of required fields on our page layouts that aren't really critical for testing the functionality of the Triggers, so I created a new Opportunity Record Type called 'APEX Test' with a corresponding page layout which has the bare minimum fields for testing the desired behavior.
There are two lookup fields on the Test Class that reference IDs that will be different in our Production Org, do I need to take this into account when writing the APEX Test Class?
- RecordTypeID
- Sandbox ID: 0124B0000008fEj
- Production ID: 01238000000UUj8
- DSP__c >> this is a Lookup field to a related Account. There is a validation rule on the OLI that restrict the Triggers from firing if this field isn't populated. I've created a Dummy account in our Sandbox and Production orgs for this to point to so the validation rule doesn't limit the records from being updated.
I'm getting the following compile errors. When I remove one offending field I get the subsequent errors. I've saved the test Class with all these fields removed but of course the test fails because it's missing required fields for the Opportunity object.Sandbox ID: 0014B000004lew6
Production ID: 00138000018DiwD
Can you help?
Product2
Thanks!
And of course the Compile Errors.
The "layout" based on the record type does not come into play at all when writing tests. The only fields that will be required in testing are the ones that are marked as required at the object level, not at the layout level.
Any ideas on the Compile Errors from these fields? They are still popping up with the updated code:
- Error: Compile Error: Invalid field TimeZoneSidKey for SObject Opportunity
- Error: Compile Error: Invalid field LocaleSidKey for SObject Opportunity
- Error: Compile Error: Invalid field EmailEncodingKey for SObject Opportunity
- Error: Compile Error: Invalid field LanguageLocaleKey for SObject Opportunity
- Error: Compile Error: Invalid initial expression type for field Opportunity.CloseDate, expecting: Date
Also, there is a new Compile Error now on the Product2 object:- Error: Compile Error: Field is not writeable: Product2 at line 24 column 24
When I delete all fields causing Compile Errors I can save the class but it is failing tests with the following Test Report:Here's the code that generated the above test, I know it isn't right given the missing fields just need advise on how to deal with the compile errors before I can move forward:
As for ClosedDate, use the following:
Thanks for your help with this.