I recently wrote my very first trigger and I'm looking to deploy to production. Before deploying I need help with writing test code but I don't know what I'm doing. Basically i want to test my trigger but I don't know how to write test cold. the goal is to enter a date in the opportunity object field titled: Initial Field Visit Completed. I have copied the trigger below and if you guys can provide suggestions that would be greatly appreciated.

trigger FieldVisitCompleted on Opportunity (after insert,after update) {
Set <Id> oppIds = new Set<Id>(); 
List<Lead_Source__c> ls= new List<Lead_Source__c>();

    for (Opportunity parentObj : Trigger.new)

    Map<Id, Lead_Source__c> parentObjList = new Map<Id, Lead_Source__c>([Select Id,Effort__c,Initial_Field_Visit__c FROM Lead_Source__c WHERE Effort__c IN :oppIds]); 
           for (Lead_Source__c lsrc : parentObjList.values()) {
                   System.debug('lead source'+lsrc);
                   Opportunity opp = Trigger.newMap.get(lsrc.Effort__c);
                   Opportunity o = Trigger.oldMap.get(lsrc.Effort__c);
                   System.debug('opp field'+opp.Initial_Field_Visit_Completed__c);
                   System.debug('opp field'+ o.Initial_Field_Visit_Completed__c);

               if(opp.Initial_Field_Visit_Completed__c != o.Initial_Field_Visit_Completed__c){ 
                   lsrc.Initial_Field_Visit__c = opp.Initial_Field_Visit_Completed__c;
                   System.debug('lsrc field'+lsrc.Initial_Field_Visit__c);

         update ls;

Here is what I have so far for test code

public class FieldVisitTestClass { 

     static testMethod void myTest() { 
         Opportunity opp = new Opportunity(); 
         opp.name = 'Michael Jordan'; 
         opp.AccountId = opp.Id; 

Looking for some direction as to where to go from here.

  • Do you have any test code yet? – Jagular Jun 25 '14 at 15:12
  • 2
    If you Google this subject you will find many examples - invest some time in looking at those. If you then get stuck writing your code and can't figure out a solution (after more Googling), post a specific question including your test code. – Keith C Jun 25 '14 at 15:18
  • I do have test code of my own but I get stuck here and do not know what to type next. @isTest public class FieldVisitTestClass{ static testMethod void myTest() { Opportunity opp = new Opportunity(); opp.name = 'Michael Jordan'; opp.AccountId = opp.Id; } } – WickWack Jun 25 '14 at 15:24

Try re-creating in Apex how you would do it in Salesforce.

  1. Create a Lead Source record
  2. Create an Opportunity record with an Initial_Field_Visit_Completed__c field value
  3. Update the Lead Source Effort field with the Opportunity record Id
  4. Update the Opportunity's Initial_Field_Visit_Completed__c field value
  5. Assert whether the Lead Source's Initial_Field_Visit__c value is equal to the new Opportunity's Initial_Field_Visit_Completed__c value.
  • 2
    Very good answer as it gives direction and hints specific to the example, without just writing the code for someone. Very nice. – Chris Duncombe Jun 25 '14 at 15:24

While I won't flat out write the test class for you, I will try to help you write it yourself.

In general, if you want to test your apex code, you just need to follow 3 steps

1. Create your data

For your trigger above it looks like you just need to create an Opportunity record

Opportunity opp = new Opportunity(Name = 'Test Opp', etc, etc, etc);//add your attributes

2. Instantiate the class/Fire your trigger

If this were a class or controller, you would instantiate the class. As this is a trigger, you want to create conditions that cause that trigger to fire. As your trigger is an update trigger, you would need to insert the opportunity and also need to later update it.

insert opp; 

Lead_Source__c lSource = new Lead_Source__c(Name = 'test lead source', Effort__c = opp.Id, Initial_Field_Visit_Completed__c = system.today().addDays(-1));
insert lSource;

opp.Initial_Field_Visit_Completed__c  = system.Today();
update opp;

3. Test your logic (Most IMPORTANT, and often overlooked step)

Your trigger is updating the Lead_Source__c objects you already created, based on the insert of an opportunity. So now you want to make sure that actually happened. Query for the updated Lead_Source__c records and make sure they have the info you are looking for with system assertions.

Lead_Source__c ls = [Select Id, Name, Initial_Field_Visit_Completed__c From Lead_Source__c Where Id =: lSource.Id]; 
Opportunity newOpp = Select Id, Name, Initial_Field_Visit_Completed__c From Opportunity Where Id =: opp.Id];

//Verify the Lead Source record does not have the value that you originally set it to.
System.assertNotEquals(ls.Initial_Field_Visit_Completed__c, system.today().addDays(-1));
System.assertEquals(ls.Initial_Field_Visit_Completed__c, newOpp.Initial_Field_Visit_Completed__c);
//Etc, Etc.

That is a very basic run through of how to write test code. You would now want to check other conditions, such as bulk updates, or updates of Opportunities with unexpected data, but this is a high level that should get you started quite well.

Good luck.

  • thank you sfdc!! I greatly appreciate the information above. i would rather you not write it out for me because I wouldn't learn. thanks again for the information. – WickWack Jun 25 '14 at 17:01
  • Glad to help. Let us know how it works out for you. and come back with any specific questions. – Chris Duncombe Jun 25 '14 at 17:03

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