- trigger LeadBeforeUpdate on Lead (before update)
- {
- //Prefetch email Template id
- EmailTemplate et = [SELECT id FROM EmailTemplate WHERE Name = 'Lead_Rejected'];
- //Grab the recipient as Contact
- Contact recipient = [SELECT id, firstname FROM Contact where id in :Trigger.newMap.keySet()];
- //Map the profile
- Map<Id, Profile> ProfileOwner = new Map<Id, Profile>([Select p.Id, p.Name from Profile p where Id =: userinfo.getProfileId()]);
- //Lead Object Map
- Map<Id, Lead> LeadMap = new Map<Id,Lead>([Select Id, From Lead WHERE id in :Trigger.newMap.keySet()]);
- for (lead myLead : Trigger.new)
- {
- Lead testLead = LeadMap.get(myLead.id);
- if(testLead.Status__c=='Approved') {
- /*---Mail to the Lead Owner **********/
- Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); // new mail object
- //Depending on Schema your organization have
- recipient.AccountId = opp.AccountId; // receipient would be owner of Lead
- mail.setSenderDisplayName(ProfileOwner.get(userInfo.getUserId).Name);//Sender would be owner of Profile
- //Notice : How I retrieve profile by passing value of current user id (using UserInfo Method)
- mail.setTargetObjectId(recipient.id); //target to receipient
- mail.setTemplateId(et.id); // pull the email template through id
- Messaging.sendEmail(new Messaging.Singleemailmessage[] {mail}); //send single email
- } // end if
- } // end for
Thursday, 6 November 2014
Salesforce Apex Code for Email Template | How to send
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment