+ Start a Discussion
Akshay ShrivastavaAkshay Shrivastava 

Trigger for send email

This is trigger for email and it is working perfectly . But i want to Send mail like that...
Dear Name,
your car bill  details is given below....
Car name : Car_name__c
Car model: Car_Model__c
etc..
like this email should received to the recepitant..


trigger EmailBill on Create_Bill__c (After insert, after update) {
    
    if(trigger.isAfter){
        if(trigger.isInsert || trigger.isUpdate){
            set<Id> conSetId=new set<Id>();
            for(Create_Bill__c obj:trigger.new){
                if(obj.Email_ID__c != null){
                    conSetId.add(obj.id);
                }
            }
            List<Create_Bill__c> conList=new List<Create_Bill__c>();
            conList=[select id,Email_ID__c,BuyerId__c,Name,Car_Name__c,Colour__c,Contact_No__c,Address__c,Date_Time__c,Engine_No__c,Model_Name__c,Total_Price__c from Create_Bill__c where id in: conSetId];
            for(Create_Bill__c cObj: conList){
                if(cObj.Email_ID__c !=null)
                    EmailManager.sendMail(cObj.Email_ID__c,'Dear ','Your Car bill details is below' + cObj.Contact_No__c);
            }
        }
        
    }
}
Suraj Tripathi 47Suraj Tripathi 47
Hi Akshay,
You can take reference from this below code.
trigger EmailBill on Create_Bill__c (After insert, after update) {
    
    if(trigger.isAfter){
        if(trigger.isInsert || trigger.isUpdate){
            set<Id> conSetId=new set<Id>();
            for(Create_Bill__c obj:trigger.new){
                if(obj.Email_ID__c != null){
                    conSetId.add(obj.id);
                }
            }
            List<Create_Bill__c> conList=new List<Create_Bill__c>();
            conList=[select id,Email_ID__c,BuyerId__c,Name,Car_Name__c,Colour__c,Contact_No__c,Address__c,Date_Time__c,Engine_No__c,Model_Name__c,Total_Price__c from Create_Bill__c where id in: conSetId];
            for(Create_Bill__c cObj: conList){
                if(cObj.Email_ID__c !=null)
                   Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[] {cObj.Email_ID__c};
            mail.setToAddresses(toAddresses);
            mail.setSubject('Your Car bill details is below' + cObj.Contact_No__c);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
            }
        }
        
    }
}

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

Thanks and Regards
Suraj Tripathi.
PriyaPriya (Salesforce Developers) 

Hi Akshay,

You can create a custom template in the email template Object and you can query that email template in your trigger as below :- 

EmailTemplate et=[SELECT Id, DeveloperName , body,IsActive FROM EmailTemplate WHERE DeveloperName = 'Outage_is_cleared']; 

For detail, refer this example which meets your criteria :- 
https://developer.salesforce.com/forums/?id=9060G000000MVa4QAG

Benifit of creating email template is that you can change the body in email template anytime without making any changes in trigger.

Please mark it as Best Answer so that it can help others in the future.

Regards,

Priya Ranjan