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');

       // update opplist;

global void finish(database.BatchableContext bc) {}
  • Your requirement can be achievable via time based workflow. No need to write a batch class May 8, 2018 at 9:40
  • but i need in batch class can you help me
    – srinivas
    May 8, 2018 at 10:47

2 Answers 2

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

Replace your setPlainTextBody with setHTMLBody


Your use case can be implemented by time based workflow rule.

So, create a workflow rule on Opportunity object and define the rule firing criteria as 1 day before Opportunity Closed Date.

As an action for this rule, create a email template and send email to the user.

In the email template, you can add the record link.

  • but i need in batch class can you help me
    – srinivas
    May 8, 2018 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.