[SalesForce] Batch class for sending mail to users

I have wrote a batch class for opportunity object for users to send mail that your opportunity date is tommorow
now my requirment is to show the link of that opportunity record also in mail

my code :

global class sendemail implements Database.Batchable < sobject > {
global Database.QueryLocator start(Database.BatchableContext bc) {
       String Query;
       Date dt = date.today().addDays(1);
       Query = 'SELECT Name,Id From Opportunity WHERE CloseDate =: dt ';
       return Database.getquerylocator(Query);
       }

global void execute(Database.BatchableContext bc, List < Opportunity > opplist) {
       List < Messaging.SingleEmailMessage > emails = new List < Messaging.SingleEmailMessage > ();
       for (Opportunity opp: opplist) {
       // opp.CloseDate = 'createddate+1'; 
       Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
       email.setToAddresses(new String[] {'nihar.annamaneni@gmail.com'});
       email.setSubject('opportunity closed date'); 
       email.setPlainTextBody('Dear user, Your opportunity '+opp.name+' closed date is tommorow');

       emails.add(email);
       }
       Messaging.sendEmail(emails);
       // update opplist;
       }

global void finish(database.BatchableContext bc) {}
}

Best Answer

string emailBody = 'Dear user, Your opportunity <a href="'+URL.getSalesforceBaseUrl()+'/'+opp.id+'">'+opp.name+'</a> closed date is tommorow ';
email.setHTMLBody(emailBody);

Replace your setPlainTextBody with setHTMLBody

Related Topic