Apex triggers are the scripts that executes before and after the DML events.
Triggers that are associated with different business objects inside the Salesforce can be found at Setup->Develop->Apex Triggers and in the respective business object details page.
Syntax of writing a Trigger
trigger triggerName on ObjectName (trigger_events)
{
// code_block
}
types of trigger_events are:
1.before insert
2.before update
3.before delete
4.after insert
5.after update
6.after delete
7.after undelete
Single trigger can be of 32000 characters in length.
Lets see an example of how to send mail from the trigger
trigger mailMyObject on myObject (after update)
{
if(Trigger.isafter)
{
for(myObject s : Trigger.new)
{
String[] toAddresses;
String[] ccAddresses;
String[] replyTo;
//Business Logic
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(toAddresses);
mail.setCcAddresses(ccAddresses);
mail.setReplyTo(replyTo;);
mail.setSenderDisplayName('SFDC');
mail.setSubject('Mail from SFDC');
mail.setBccSender(false);
mail.setUseSignature(false);
mail.setHtmlBody('Mail Body');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}
No comments:
Post a Comment