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
Vivekananda ReddyVivekananda Reddy 

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.

 

Best Answer chosen by Admin (Salesforce Developers) 
Subhash GarhwalSubhash Garhwal

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

Deepa.B.AnkaliDeepa.B.Ankali
@Vivekananda,

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.
Subhash GarhwalSubhash Garhwal

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

 

 

This was selected as the best answer
Vivekananda ReddyVivekananda Reddy

Thank you subhash :)