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

Triggers and Test Classes
Hi,
1. Is der any specific steps for writting trigger ? if it is ......please suggest me, because i am very confusing in this topic. Please help me out.
2. How to write test class for trigger and If i write trigger logic in class, again i need to write a test class for that trigger.
Thank You,
Vivek.
In trigger event you define event that you need like (before insert, after insert, before update etc...)
here I am taking after insert case
//Trigger
trigger Trigger_Opportunity on Opportunity (after insert) {
//Check for the request type
if(Trigger.isAfter) {
//Check for trigger event
if(Trigger.isInsert) {
//Here you call your helper class's method
OpportunityTriggerHelper.MethodName(Trigger.New)
}
}
}
//Helper class for that trigger
public without sharing class OpportunityTriggerHelper {
//Define your method
public static void MethodName(List<Opportunity> oppList) {
//Your logic
}
}
When you write test class for that trigger it also call helper class method so your method also cover so you only needs to
write test class ones.
For more help on triiger you go through these post
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers.htm
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_HelloWorld.htm
For the test class
Go for this post , it will definetly helps you out
http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html
Thanks,
Hit the Kudos button (star) if it post helps you
All Answers
You dont have to explicitely call your trigger. As name suggests, you will have to create instances of Object's that you are using in your trigger. Based on conditions, your trigger will be fired in test class.
In trigger event you define event that you need like (before insert, after insert, before update etc...)
here I am taking after insert case
//Trigger
trigger Trigger_Opportunity on Opportunity (after insert) {
//Check for the request type
if(Trigger.isAfter) {
//Check for trigger event
if(Trigger.isInsert) {
//Here you call your helper class's method
OpportunityTriggerHelper.MethodName(Trigger.New)
}
}
}
//Helper class for that trigger
public without sharing class OpportunityTriggerHelper {
//Define your method
public static void MethodName(List<Opportunity> oppList) {
//Your logic
}
}
When you write test class for that trigger it also call helper class method so your method also cover so you only needs to
write test class ones.
For more help on triiger you go through these post
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers.htm
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_HelloWorld.htm
For the test class
Go for this post , it will definetly helps you out
http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html
Thanks,
Hit the Kudos button (star) if it post helps you
Thank you subhash :)
Hi Vivek,
I hope this link helps you..
http://shivasoft.in/blog/salesforce/step-by-step-salesforce-tutorial-%E2%80%93-creating-trigger-and-test-cases-%E2%80%93-6-of-6/
Thanks,
Cool Sfdc