+ Start a Discussion
Dilip SharmaDilip Sharma 

How to write a trigger on standard object?

Naveen Rahul 3Naveen Rahul 3
trigger Event_Email on Event (before insert) {

String[] toAddresses = new String[] {'naveenrahuld@srmtech.com'}; 
String[] ccAddresses = new String[] {'navin_ragul@yahoo.com'};
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
EmailTemplate et=[Select id from EmailTemplate where name=:'<b>Home page changed</b>'];

System.debug('Email template'+et);

mail.setTemplateId(et.id);
mail.setToAddresses(toAddresses);
mail.setCcAddresses(ccAddresses);
mail.setReplyTo('name@email.com');
mail.setSenderDisplayName('Name');
mail.setBccSender(false);
mail.setUseSignature(false);
Event eve = trigger.new[0];
mail.setTargetObjectId(eve.OwnerID);
mail.saveAsActivity = false;

// The actual body of the email is what I'm having trouble with. I also have no idea how to tell it to insert a URL for future events
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}

here is an link that would be helpfull.
http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=apex_triggers.htm|StartTopic=Content%2Fapex_triggers.htm|SkinName=webhelp

before insert,
after insert,
before update,
after update


 can use any one state

trigger triggername<can be anything>  on Event<Standard object> (after update)       {

your LOgic here

}
shiv@SFDCshiv@SFDC
First of all you can not directly right emil in production org. First you have write email in sandbox and dev org than you can depoly in to your production.

There are the some standard objects which allow to write trigger on them, and some of them are not. so be clear on which standard object you are going to write trigger.

syntax:
trigger triggerName on OjbectName (events){
}

triggerName = name of the trigger
OjbectName = name of the object
events = after insert, after update, after delete, before insert etc..

Please refer this link to learn from scratch.
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers.htm