1

I am sending emails(with visual force email template) to different levels of approvers present in opportunity record when every we click a button submit for approval. Our requirement is if a person 'A' is in two levels on a record we need to send a single email to A. Below is my code. When ever I am click a button submit for approval I could see my visualforce template only in debug logs.

  1. why i am not receiving any email?
  2. I don't see any opportunity details(relatedto information) in that email template. Just it showing my visualforce hardcodes.

Can some one help me on this?

Trigger:

trigger opportunityemail on Opportunity(after update, after insert) {
    List emails = new List();
    set sendtoid = new set();
    list sendtoaddress = new list();
    list useremail = new list();
    for (opportunity op: trigger.new) {
        if (op.submitfor_approval__c == true) {
            sendtoid.add(op.Level1__c);
            sendtoid.add(op.Level2__c);
            sendtoid.add(op.Level3__c);
            system.debug('size of approvers:' + sendtoid.size());
        }
    }
    sendtoaddress = [select id, email from user where id in: sendtoid];
    for (user u: sendtoaddress) {
        useremail.add(u.email);
        system.debug('u.email:' + u.email);
        for (opportunity op: trigger.new) {
            if (op.submitfor_approval__c == true) {
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                mail.setTargetObjectId(u.id);
                system.debug('u.id:' + u.id);
                mail.setToAddresses(useremail);
                mail.setTemplateId('00Xc0000000ECDf');
                mail.saveAsActivity = false;

                emails.add(mail);
            }
        }
    }
    Messaging.sendEmail(emails);
}

Visualforce template:

<messaging:emailTemplate subject="opportunity approval needed" recipientType="user" relatedToType="Opportunity">
    <messaging:htmlEmailBody>
        <html>
        <body style="background-color:#E6E6FA">
            <img src="aprovalpic.gif" alt="need aproval" width="42" height="42"> </img>
            <STYLE type="text/css">
                TH {
                    font-size: 20px;
                    font-face: arial;
                    background: #CCCCCC;
                    border-width: 5;
                    text-align: center
                }
                TD {
                    font-size: 11px;
                    font-face: verdana
                }
                TABLE {
                    border: solid #CCCCCC;
                    border-width: 1
                }
                TR {
                    border: solid #CCCCCC;
                    border-width: 1
                }
                thead {
                    color: green;
                }
                tbody {
                    color: blue;
                }
                tfoot {
                    color: red;
                }
                empty-cells: hide;
                table,
                th,
                td {
                    border: 1px solid black;
                }
            </STYLE>
            <font face="arial" size="2">
                <p>Hi {!recipient.name}, </p>
                <pre>                                              SYS, INC  <br/>
  </pre>
                <b> General Information</b><br/> ******************************************************************************************************************** <br/><br/>
                <table border="0">
                    <colgroup>
                        <col span="2" style="background-color:red"> </col>
                        <col style="background-color:yellow"> </col>
                    </colgroup>
                    <thead>
                        <tr>
                            <th> OPPORTUNITY DETAILS ARE HERE </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <th>Opportunity name {!relatedTo.name} </th>
                        </tr>
                    </tbody>
                    <tfoot>
                        <tr>
                            <th> amount {!relatedTo.amount} </th>
                        </tr>
                    </tfoot>
                </table>
                <p> <b>Below is a list of users need to approve: {!relatedTo.name}.</b></p>
                ******************************************************************************************************************** <br/><br/>
                <table border="0" class="tabel__1">
                    <tr>
                        <td>{!relatedTo.Level1__r.name}</td>
                    </tr>
                    <tr>
                        <td>{!relatedTo.level2__r.name}</td>
                    </tr>
                    <tr>
                        <td>{!relatedTo.level3__r.name}</td>
                    </tr>
                    <tr>
                        <td>{!relatedTo.PL_Level1__r.name}</td>
                    </tr>
                    <tr>
                        <td>{!relatedTo.PL_Level2__r.name}</td>
                    </tr>
                    <tr>
                        <td>{!relatedTo.Ps_Level2__r.name}</td>
                    </tr>

                    <tr>
                        <td>{!relatedTo.Level4__r.name}</td>
                    </tr>

                    <tr>
                        <td>{!relatedto.PL_Level2__r.name}</td>
                    </tr>
                </table>
                <p />
            </font>
            <p> Please go through below link to Accept/Reject this opportnunity.</p>
            <p><a href="http://cs14.force.com/ApprovalDetailscopy?id={!Relatedto.Id}&name={!recipient.name}">accept/reject here</a></p>
        </body>
        </html>
    </messaging:htmlEmailBody>
</messaging:emailTemplate>
  • I made change. But still not receiving any email. And my visualforce template not providing recipients name. example :if use user id, I could see "Hi AAABB" . If I use contact id "Hi ". Please help me how can I achieve this? – user33630 Jun 29 '16 at 4:58
  • @ Santanu Boral for(user u:sendtoaddress){ useremail.add(u.email); system.debug('u.email:' +u.email); for(opportunity op:trigger.new){ if(op.submitfor_approval__c == true){ Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setTargetObjectId('003c000000ikhk2'); system.debug('u.id:' +u.id); mail.setToAddresses(useremail); mail.setTemplateId('00Xc0000000ECDf'); mail.setWhatId(op.id); mail.saveAsActivity = false; emails.add(mail); } } } Messaging.sendEmail(emails); } still not receiving email. – user33630 Jun 29 '16 at 5:04
1
if(op.submitfor_approval__c == true) { 
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
    system.debug('u.id:' +u.id); 
    mail.setToAddresses(useremail); 
    mail.setTemplateId('00Xc0000000ECDf'); 
    mail.saveAsActivity = false; emails.add(mail); 
    mail.setWhatId(op.id); 

    //WhatId cannot be set unless target object id is contact
    //Get any contact from org and use it as you are already using User's emails all users will get email.
    mail.setTargetObjectId(contact.id); 

    //Now this method will ensure that contact will not receive any emails 
    setTreatTargetObjectAsRecipient(false);
} 
  • Added an edit for code blocks, you might want to take the comments and form an actual answer with it though so readers don't have to browse the code. – drakored Jun 29 '16 at 2:42

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.