function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JakeInTheBoxSLCJakeInTheBoxSLC 

How to test my trigger?

Hi All,

I'm having difficulty figuring out how to test my trigger so that I can put it in my production environment. My trigger is simple and checks to see if a field has been changed and if so it updates another field with the current date and time like so:

trigger PIFDateUpdate on Account bulk (before update) {
for (Account an : Trigger.new) {for (Account ao : Trigger.old) {
if (an.PIF_Available_Balance__c ao.PIF_Available_Balance__c) {
an.As_of_2__c = System.Now();
}
}
}
}

However, I can't figure out how to get 75% test coverage for this. Any thoughts?
Best Answer chosen by Admin (Salesforce Developers) 
RiverChaserRiverChaser
There may be other ways, but this is how I'm doing it.

I have a separate class called TriggerTests.cls (I use the Apex Toolkit for Eclipse, but other tools should work the same general way). In it, I have a test method where I do whatever is necessary to cause the trigger to execute.

In your case, the test code should create a new Account, save it, and make a change to it, then save the changes. Grab a reference to the new/updated account, and make sure that the field was updated properly.

The nice thing is that when the test runs, the code coverage analysis tool is smart enough to see that the trigger runs because of the test method.

Does this make sense? If not, I can post some sample code. Often the hardest part is to figure out how to get the right events to occur. And if the trigger responds to multiple events (before and after insert, for example), your test method had to do all the stuff necessary to cause those events to occur.

Don

All Answers

RiverChaserRiverChaser
There may be other ways, but this is how I'm doing it.

I have a separate class called TriggerTests.cls (I use the Apex Toolkit for Eclipse, but other tools should work the same general way). In it, I have a test method where I do whatever is necessary to cause the trigger to execute.

In your case, the test code should create a new Account, save it, and make a change to it, then save the changes. Grab a reference to the new/updated account, and make sure that the field was updated properly.

The nice thing is that when the test runs, the code coverage analysis tool is smart enough to see that the trigger runs because of the test method.

Does this make sense? If not, I can post some sample code. Often the hardest part is to figure out how to get the right events to occur. And if the trigger responds to multiple events (before and after insert, for example), your test method had to do all the stuff necessary to cause those events to occur.

Don
This was selected as the best answer
JakeInTheBoxSLCJakeInTheBoxSLC
Worked like a charm! Thank you so much for your help!
Rob P12Rob P12
    I'm having the same problem that you had. I wrote this trigger that changes the OwnerId on the Child Record
   to match the OwnerID on the Parent Record. This trigger is working as expected in the SandBox. What is the process to push into production?

 

trigger ChangeOwnerInfluencer on Influencers__c (before insert, before update){

   

    For (Influencers__c I : Trigger.New){

      string Acct = I.Acct_Plan__c;

      SFDC_Acct_Plan__c AcctPlan;

      AcctPlan=[Select c.OwnerId From SFDC_Acct_Plan__c c Where   c.Id=:Acct];

     

      I.OwnerId = AcctPlan.OwnerId;

    }

}

Can I see your sample Unit Test code TestMethod sample?

Thanks for your help
RiverChaserRiverChaser
Hi Rob,

I'm not sure who's code you're asking to see, mine or Jake's, but a snippet of mine is below. (Cleaned up a bit to remove all the commented out code that didn't work! :smileyvery-happy:)

This is obviously specific to my custom Membership object, but hopefully you can get the idea.

Don

Code:
public class TriggerTests {
 static testMethod void testContactmembershipInfoTrigger() {
  Account account = new Account(name = 'wowowowowowosl');
  insert account;
  Contact contact = new Contact(LastName = 'XxXxXxX', Account = account, Email = 'xxxxx@xxxxx.org');
  insert contact;
  Product2 product = [select Id, name from Product2 where name = 'Membership']; 
  
  Membership__c membership = new Membership__c(Account__c = account.Id, Contact__c = contact.Id, Product__c = product.Id, Start_Date__c = Date.today(), Expiration_Date__c = Date.today(), Cost__c = 599);
  insert membership;
  account = null;
  contact = null;
  product = null;
  
  Contact newContact = [SELECT Id, Membership_Expiration__c, Membership_Type__c FROM Contact WHERE LastName = 'XxXxXxX'];
  
  if (newContact != null) {
   Membership__c mbr = [SELECT Id, Contact__c FROM Membership__C WHERE Contact__r.LastName = 'XxXxXxX']; 
   System.assertNotEquals(null, mbr, 'mbr object is null');
   
   newContact.LastName = 'YyYyYyYy';
   update newContact;
   
   delete newContact;
  }
  delete membership;
 }
}

 

Cthulhu4242Cthulhu4242
RoberChaser, THANK YOU for posting your trigger tests class. I'm coming to all of this from php/perl, so it's all very new, and I was scratching my head until I saw your post.