opportunity records

Mail a Table of Opportunity Records With a Link of Detail Page | Salesforce Tutorial

In this blog, we have checked the closedDate of the whole Opportunity records and sent a mail of all opportunities whose closedDate is less than today to the Opportunity owner with the link of a Detail page for changing the closedDate of the Opportunity in a table format.

for(string key :map2.keyset()){
    htmlBody = 'Hi  ' +map2.get(key)+ '<br /> \nPlease review the below opportunities which are closing today and kindly take the required actions \n <br /> <table border="1" style="border-collapse: collapse"><tr><th>Name</th><th>StageName</th><th>CloseDate</th><th>Opportunity Link</th></tr>';
    system.debug('htmlBody+++++'+htmlBody); 
    for(Opportunity opp:map1.get(key)){
        if(!strlist.contains(opp.owner.email)){
            strlist.add(opp.Owner.email);
            system.debug('strlist====1' +strlist);
        }
        String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
        oppName=opp.Name;
        system.debug('oppName++++'+oppName);
        oppStageName=opp.StageName;                
        oppLink = opp.id;
        htmlBody += '<tr><td>' + oppName +  '</td><td>' + oppStageName +'</td><td>' + oppCloseDate + '</td><td>' +'/'+baseUrl + '/' +oppLink + '</td></tr>';
        system.debug('FinalBody++++'+htmlBody);       
    }
    system.debug('@@@@@@@@@@OppName+++'+oppName);
    htmlBody += '</table> <br /> \n Thank You <br /> \n Salesforce Automation \n';
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setToAddresses(strlist);
    //  system.debug('mailtoset++'+ mail.setToAddresses(strlist));
    mail.setSenderDisplayName(UserInfo.getName());
    mail.setSubject('Opportunity with close date today');
    mail.setHTMLBody(htmlBody);
    system.debug('mail+++++'+mail);
    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>{mail};  
    Messaging.sendEmail(mails);
}

dont miss out iconCheck out another amazing blog by Aditya here: Why You Should Consider Investing In Salesforce Lightning

If you have any questions, let me know in the comment section.        

Responses

Popular Salesforce Blogs