Search code examples
testingtriggerssalesforcecaseapex-code

Salesforce Test Class for APEX Trigger


I'm in need of some help regarding the writing of test script that covers enough of the below trigger that I have managed to get working on my Sandbox account. The trigger is to create extra assets when certain types of Opportunities are closed. The Trigger seems to run fine but I really have no idea how to start writing test cases... In order for these Opportunities to close, the Account needs to have the following completed (I've included some example data - They are picklists so need to be specif amounts):

a.TurnoverBand__c = '<£10 million';
a.Joining_Fee__c = '£1,920';
a.Annual_Subscription__c = '£1,320';

Trigger as follows:

trigger CreateInclusiveAssetonMembershipWon on Opportunity (after insert, after update)
{
   for(Opportunity o: trigger.new)
  {
    if(o.isWon == true && o.HasOpportunityLineItem == true && ( o.Type == 'A Membership' || o.Type == 'AB Membership' || o.Type == 'A Membership Upgrade' || o.Type == 'AB Membership Upgrade' ) )
    {
     String opptyId = o.Id;
     Asset[] ast = new Asset[]{};
     Asset a = new Asset();
      {
      a = new Asset();
      a.AccountId = o.AccountId;
      a.Product2Id = '01tA0000003N1pW';
      a.Quantity = o.Inclusive_Training_Spaces_Allocated__c;
      a.Price = 300;
      a.PurchaseDate = o.CloseDate;
      a.Status = 'Purchased';
      a.Description = 'Allocated Spaces';
      a.Name = 'Membership Inclusive Training';
      ast.add(a);
      }
    insert ast;
    }
  }
}

If anyone could help me out on this I would be grateful!

Thanks

ETA test script for this trigger so far:

@isTest
 private class TrngAstOppTrigTestSuite {

      static testMethod void verifyBehaviorOnInsert_positive() {
          Account a = new Account();
      a.Name = 'New Test Account';
      a.Account_Email__c = 'testemail4trigger@test.co.uk';
          a.TurnoverBand__c = '<£10 million';
          a.Joining_Fee__c = '£1,920';
      a.Annual_Subscription__c = '£1,320';
      insert a;

          Opportunity o = new Opportunity();
          OpportunityLineItem ol = new OpportunityLineItem();
          PricebookEntry pbID = [select ID from PricebookEntry];

      o.AccountId = a.Id;
      o.Name = 'test';
          o.Type = 'A Membership';
          o.StageName = 'Needs Analysis';
          o.CloseDate = date.today();
          insert o;

      ol.OpportunityId = o.Id;
      ol.Quantity = 1;
      ol.UnitPrice = 2.00;
          ol.PricebookEntryId = pbID.Id;

          insert ol;

      o.StageName= 'Closed Won';
          update o;

          delete ol;
          delete o;
  }        
}

If anyone could say if I am going in the right direction with this I would be grateful. Attempting to iron out the errors but there is obviously no point if this is not going to work anyway. Thanks


Solution

  • Here is a link to the Apex code documentation that shows how to create a test.

    All you need to do is write a testMethod that inserts or updates an Opportunity whilst meeting the criteria you define in your trigger. A good unit test should test various sceneries and verify that the code produces the expected outputs (in this case, query the new Asset).

    Also, I should point out that your code has a serious flaw in it's design. You should almost never have DML statements (or any database statements for that matter) inside of a loop. I have provided you with a fixed version of your code but I strongly suggest you head over to developer.force.com and follow some of the getting started material to avoid future headaches.

    trigger CreateInclusiveAssetonMembershipWon on Opportunity (after insert, after update)
    {
        Asset[] assets = new Asset[0];
        for(Opportunity o: trigger.new)
        {
            if(o.isWon == true && o.HasOpportunityLineItem == true && ( o.Type == 'A Membership' || o.Type == 'AB Membership' || o.Type == 'A Membership Upgrade' || o.Type == 'AB Membership Upgrade' ) )
            {
    
                Asset a = new Asset();
                a.AccountId = o.AccountId;
                a.Product2Id = '01tA0000003N1pW';
                a.Quantity = o.Inclusive_Training_Spaces_Allocated__c;
                a.Price = 300;
                a.PurchaseDate = o.CloseDate;
                a.Status = 'Purchased';
                a.Description = 'Allocated Spaces';
                a.Name = 'Membership Inclusive Training';
                assets.add(a);
            }
        }
        insert assets;
    }