I've tried looking around for a situation matching my own as well as a lot of experimentation but I am at a loss. I've managed to write an APEX trigger to create a new opportunity if another opportunity under the same account closes under certain scenarios, i.e., product type =x, Opportunity Type = y.

My test class yields 2/13 (15%) but I am not sure what I am doing right to get the 2/13 (after many failures) and what I am doing wrong. I've included the test class and trigger below. I know that my trigger works in the Sandbox but I cannot deploy yet. Note that I added Opportunity oppt if in the event I needed to create the qualifying record, however I am not sure how to apply an if statement in the test.


//When new mAWS Business Opportunity is Closed, creates usage opportunity
//Assumptions include 20% EST AWS Usage, Close Date 120 Days from Close Date
//70% Probability
trigger mAWSUsageOpportunity on Opportunity (after update) {
for (Opportunity oppt : Trigger.new) {
    //Only New Business opportunities for now
    If (oppt.Type == 'New Business' && oppt.Primary_Service_Interest__c == 'mAWS' && oppt.StageName == 'Customer Won') { 
    Opportunity mAWSusage      = new Opportunity();
    mAWSusage.Name          ='mAWS Usage';
    mAWSusage.Type          ='New Business - Add';
    mAWSusage.StageName        ='Negotiations';
    mAWSusage.Primary_Service_Interest__c = 'mAWS';
    mAWSusage.Original_Source__c   ='Customer';
    mAWSusage.Source__c        ='Customer';
    mAWSUsage.Amount        =oppt.AWS_EST_Spend__c*.20;
    mAWSusage.AccountId        =oppt.AccountId;
    mAWSusage.CloseDate        =oppt.CloseDate.addDays(120);
       //Creates Record
        insert mAWSusage;




public class mAWSOppTest {
static testMethod void mAWSUsageOpportunity() {
//Created record from scratch
    // Create a test account
     Account testAcct = new Account (Name = 'My Test Account');
     insert testAcct;

    // Creates first opportunity
    Opportunity oppt = new Opportunity(Name ='New mAWS Deal',
                            AccountID = testAcct.ID,
                            StageName = 'Customer Won',
                            Amount = 3000,
                            CloseDate = System.today(),
                            Original_Source__c = 'Customer',
                            AWS_Est_Spend__c = 10000);

   insert oppt;

    // Create a test opportunity
    Opportunity mAWSUsage = new Opportunity( Name = 'mAWS Usage',
                              AccountId = testAcct.Id,
                              Primary_Service_Interest__c = 'mAWS',
                              StageName = 'Negotiations',
                              CloseDate = System.today(),
                              Original_Source__c = 'Customer',
                              Source__c = 'Customer',
                              Type = 'New Business - Add',
                              Amount = 555888555);

  insert  mAWSUsage;

    // Get the opportunity, verify the stage name and close date
  mAWSUsage = [SELECT StageName, CloseDate, Name, Original_Source__c, Source__c, Type, Primary_Service_Interest__c, Amount
             FROM Opportunity 
             WHERE id = :mAWSUsage.Id];

  System.assertEquals(mAWSUsage.StageName, 'Negotiations');
  System.assertEquals(mAWSUsage.Name, 'mAWS Usage');
  System.assertEquals(mAWSUsage.Type, 'New Business - Add');
  System.assertEquals(mAWSUsage.CloseDate, System.today());
  System.assertEquals(mAWSUsage.Original_Source__c, 'Customer');
  System.assertEquals(mAWSUsage.Amount, 555888555);
  System.assertEquals(mAWSUsage.Source__c, 'Customer');
  System.assertEquals(mAWSUsage.Primary_Service_Interest__c, 'mAWS');

Thank you very much in advance!


Your trigger is an after update trigger and hence since your test only does inserts, will never execute

Either reconstruct the test to update an Opportunity if that is the only time you want to execute the trigger; or, change the trigger to also execute on after insert

BTW, kudos on doing asserts - so many think all you need to do is test coverage

Not so kudos on your trigger construction though -- you are doing inserts within a for loop; instead you 'll need to add the new Opportunities to a list and insert the list at the end of the for loop on Trigger.new

Update - sample testmethod (typed in, so may be typos)

private static mAWSUsageOpportunity() {
  List<Account> aList = new List<Account> {
       new Account (Name = 'My Test Account')
  insert aList;
  List<Opportunity> oList = new List<Opportunity> {
     new Opportunity(Name ='00New mAWS Deal',
                        AccountID =aList[0].id,
                        StageName = 'Customer Won',
                        Amount = 3000,
                        CloseDate = System.today(),
                        Original_Source__c = 'Customer',
                        AWS_Est_Spend__c = 10000),
   new Opportunity( Name = '01mAWS Usage', 
                          AccountId = aList[0].id,
                          Primary_Service_Interest__c = 'mAWS',
                          StageName = 'Negotiations',
                          CloseDate = System.today(),
                          Original_Source__c = 'Customer',
                          Source__c = 'Customer',
                          Type = 'New Business',
                          Amount = 555888555)
  insert oList;

  oList[1].stagename = 'Customer Won';   // set conditions for after update trigger
  update oList[1];
  List<Opportunity> oResList = [select id, amount, ... from Opportunity where id NOT IN :oList];
  System.assertequals(1, oResList.size(),'should create 1 new usage oppo');
  System.assertEquals(oList[1].AWS_EST_Spend__c * .20, oResList[0].amount,'amount sb 20% of prev oppo aws est spend');
 ...  other asserts added here 


While this isn't the perfect testmethod and by no means complete (it doesn't test that your trigger can handle large batch of records and doesn't do negative tests; it does allow you to set up several tests cases in the aList and oList without constantly doing inserts for every test case. Your testmethod runs more efficiently too.

| improve this answer | |
  • Here's the thing about the asserts - they don't test the functioning of the trigger. They just assert that the just inserted opportunity is there as it was inserted. The test should insert the first opportunity, then update it to closed won. Then the query and asserts should make sure a new oppty was created by the trigger. – Thomas Taylor Dec 3 '14 at 3:12
  • yes, so true -- i was a bit sloppy and didn't read the actual asserts; numbed by all the posts asking to get code coverage as if that was the be-all and end-all – cropredy Dec 3 '14 at 4:48
  • Thank you for responding. I am still pretty new at this and I've been trying to get my head around all of this. With regard to the testing the update method - how would I go about doing this? I'll take another look at the trigger itself. – Chris Dec 3 '14 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.