Trigger Senarion In salesforce:
When ever insert or update student record send mail to that student:
trigger Emailtemplatestudenttrigger on Student__c (before insert ,before update) {
for(student__c s : trigger.new ){
string str = '';
str='<html>';
str=str+'<body>';
str=str+'Hello '+s.Name+',<br/>';
str=str+'<p> Please chech your total marks below.<br/> </p>';
str=str+'<p> Your Maths marks are '+s.maths__c+' <br/> </p>';
str=str+'<p> Your Telugu marks are : '+s.Telugu__c+' <br/> </p>';
str=str+'<p> Your English marks are : '+s.English__c+' <br/> </p>';
str=str+'<p> Your Maths marks are :'+s.maths__c+' <br/> </p>';
str=str+'<p> Your Maths1 marks are :'+s.Maths1__c+' <br/> </p>';
str=str+'<p> Your Physics marks are : '+s.Phisics__c+' <br/> </p>';
str=str+'<p> Your Chemistry marks are : '+s.Chemistry__c +' <br/> </p>';
str=str+'<p> Your Total marks are '+s.Average__c +' <br/> </p>';
str=str+'<p> Thank you <br/> </p>';
str=str+'<p> Admin <br/> </p>';
str=str+'</body>';
str=str+'</html>';
if(s.email__c != null || s.Email__c !=''){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// EmailTemplate et = [Select Id from EmailTemplate where Name = 'Student Email Template'];
/// mail.setTargetObjectId(userinfo.getUserId());
// mail.setTemplateId(et.id);
// mail.setTreatTargetObjectAsRecipient(false);
mail.setSubject('Student Marks Details');
mail.setHtmlBody(str);
mail.setToAddresses(new List<String>{s.email__c});
// mail.setSaveAsActivity(false);
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
}
}
}
No comments:
Post a Comment