You need to sign in to do that
Don't have an account?

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?
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?
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
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
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
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