function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RajanRajan 

System.EmailException: SendEmail failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, Cannot convert the value of '{!dataId}' to the expected type.: []

Hi Friend,
I'm getting above error for sending email using apex class. I have a custom table and I have to design a method for button. In table their are so many records but whenever I'll select and record and click on the button then the detail should go via email with same format. I've created VF template using component. Codes are as below:
COMPONENT
*********
<apex:component controller="NavikRecommendationsControllerHelper" access="global"> <apex:attribute name="RecID" type="Integer" description="Selected Recommendation" assignTo="{!dataId}"/> <html lang="en"> <head> <title>NAVIK SalesAI</title> <style> body{font-family: arial;} table{ font-size: 13px; text-align: left;} table tr td{padding:2px;} p{padding: 7px;} </style> </head> <body> <!-- <b>Dear {!recipient.name}</b>,<br/><br/> --> Details of your recommendation are: <br/><br/><br/> <table style="border:1px solid #ddd; border-radius:4px;" align="center" width="900" cellpadding="0" cellspacing="0"> <tr> <td colspan="2"> <table align="left" width="900" cellpadding="0" cellspacing="0" style=" width:900px; font-size: 13px; background-color: #dceef4 !important; font-family:arial; border:1px solid #ddd; border-radius:4px; "> <tbody> <tr> <th align="center" width="100" bgcolor="#C8E1E9" style="padding:7px 0px;">Account</th> <th align="center" width="110" bgcolor="#C8E1E9" style="padding:7px 0px;">Contact</th> <th align="center" width="100" bgcolor="#C8E1E9" style="padding:7px 0px;">Title</th> <th align="center" width="100" bgcolor="#C8E1E9" style="padding:7px 0px;">Type</th> <th align="center" width="80" bgcolor="#C8E1E9" style="padding:7px 0px;">Contact YTD Sales ($)</th> <th align="center" width="120" bgcolor="#C8E1E9" style="padding:7px 0px;">Primary SKU</th> <th align="center" width="80" bgcolor="#DCEEF4" style="padding:7px 0px;">Lead Score</th> <th align="center" width="100" bgcolor="#DCEEF4" style="padding:7px 0px;">Next Best Action</th> <th align="center" width="110" bgcolor="#DCEEF4" style="padding:7px 0px;">Product Recommendation</th> </tr> </tbody> </table> </td> </tr> <apex:repeat var="rc" value="{!recList}"> <tr> <td colspan="2"> <table style="font-size: 13px; font-family:arial;" align="left" width="900" cellpadding="0" cellspacing="0"> <tbody> <tr style="background:#EFF0F2;"> <!-- <td> <a href="https://cs6.salesforce.com/{!rc.id}"> {!rc.Account__r.Name}</a> </td> --> <td align="center" width="100" style="padding:7px 0px;"> {!rc.Account__r.Name}</td> <td align="center" width="110" style="padding:7px 0px;"> {!rc.Recommendation_Contact__c}</td> <td align="center" width="100" style="padding:7px 0px;"> {!rc.Recommendation_Contact__r.Title}</td> <td align="center" width="100" style="padding:7px 0px;">{!rc.Type__c}</td> <td align="center" width="80" style="padding:7px 0px;"> {!rc.Recommendation_Contact__r.Contact_YTD_Sales__c}</td> <td align="center" width="120" style="padding:7px 0px;">{!rc.Account__r.primarySKU__c}</td> <td align="center" width="80" style="padding:7px 0px;"> {!rc.Opportunity_Score__c}</td> <td align="center" width="100" style="padding:7px 0px;">{!rc.Next_Best_Action__c}</td> <td align="center" width="110" style="padding:7px 0px;">{!rc.Product_Recommendation__c}</td> </tr> </tbody> </table> </td> </tr> </apex:repeat> </table> </body> </html> </apex:component>


EMAIL Template:
****************
<messaging:emailTemplate subject="Recommendation to contact{name} of Account{name} for {product Name}" recipientType="User" relatedToType="Account">
<messaging:htmlEmailBody >
     <b>Dear {!recipient.name}</b>,<br/><br/> 
        Details of your recommendation are: <br/><br/><br/>
  <c:SalesAI_SendToInbox_Component RecID="{!relatedTo.Id}">
  </c:SalesAI_SendToInbox_Component>
</messaging:htmlEmailBody>
</messaging:emailTemplate>


Method:
*************************
global static string SendToInbox(Integer dataId){
        
         // Picking a dummy Contact
            //User u =[select id, name, email from user where email <> null limit 1];
            Contact c = [select id, Email from Contact where email <> null limit 1];
         
         //List<Recommendation__c> recList; 
        List<Recommendation__c> dataList = new List<Recommendation__c>();
        Recommendation__c recList;   
        recList = [Select id, Account__r.Id, Account__r.Name, Recommendation_Contact__r.Name, Recommendation_Contact__r.Title, 
                                 Recommendation_Contact__r.Contact_YTD_Sales__c, Type__c, Primary_SKU__c, Opportunity_Score__c, Next_Best_Action__c, 
                                 Product_Recommendation__c, Account__r.BillingState, Account__r.Customer_Since__c, Recommendation_Contact__r.Phone ,
                                 Recommendation_Contact__r.Assistant_Phone__c, Recommendation_Contact__r.email , Account__r.Industry, Account__r.Business_Unit__c, 
                                 Est_Value__c, Recom_External_Id__c from Recommendation__c where Recom_External_Id__c =: dataId];
           dataList.add(recList);
           system.debug('Recom#########:' +recList);
           
        
           Recommendation_Rationale__c rrList;
                rrList = [SELECT Id, Rationale1__c, Rationale2__c, Rationale3__c, Rationale4__c, Rationale5__c, 
                                 Rationale6__c, Recommendation__c FROM Recommendation_Rationale__c where Recommendation__c =:recList.id];
        
        
           system.debug('Rationale#####:' + rrList);
        
           Cheatsheet__c chtList;
           chtList= [SELECT Id, Pitch1__c, Pitch2__c, Pitch3__c, Pitch4__c, Pitch5__c, Pitch6__c, Discount_Guidance__c, RelatedProducts1__c, RelatedProducts2__c, RelatedProducts3__c,
                                 RelatedProducts4__c, RelatedProducts5__c, RelatedProducts6__c, Recommendation__c FROM Cheatsheet__c where Recommendation__c =:recList.id]; 
           system.debug('Cheatsheet########:' +chtList);
 
                  List<Messaging.SingleEmailMessage> lstMsgs = new List<Messaging.SingleEmailMessage>();
            Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
         
             msg.setTemplateId( [select id from EmailTemplate where DeveloperName='SalesAI_SendToInbox_VF_EmailTemplate'].id );
                  msg.setWhatId( [select id from Account limit 1].id );
                msg.setTargetObjectId(c.id);
                //msg.setToAddresses(new List<String>{'Azam.Khan@absolutdata.com'});
                msg.setToAddresses(new List<String>{'rajan.mishra@absolutdata.com'});
                msg.setSaveAsActivity(false);
                lstMsgs.add(msg);
                system.debug('############:'+lstMsgs);
        // Send the emails in a transaction, then roll it back
                Savepoint sp = Database.setSavepoint();
                Messaging.sendEmail(lstMsgs);
                Database.rollback(sp);

                List<Messaging.SingleEmailMessage> lstMsgsToSend = new List<Messaging.SingleEmailMessage>();
                for (Messaging.SingleEmailMessage email : lstMsgs) {
                Messaging.SingleEmailMessage emailToSend = new Messaging.SingleEmailMessage();
                emailToSend.setToAddresses(email.getToAddresses());
                emailToSend.setSaveAsActivity(false);
                emailToSend.setPlainTextBody(email.getPlainTextBody());
                emailToSend.setHTMLBody(email.getHTMLBody());
                emailToSend.setSubject(email.getSubject());
                lstMsgsToSend.add(emailToSend);
            }
            Messaging.sendEmail(lstMsgsToSend);
        
         
RajanRajan
Any Idea for the solution?
Thanks in advance
PawanKumarPawanKumar
Please  share your full controller.

Regards,
Pawan Kumar
RajanRajan
Thank Pawan for reply,
My controller is as below:

global with sharing class NavikRecommendationsControllerHelper{
public integer dataId {get;set;}
Public List<Recommendation__c> dataList{get; set;}
    
Public List<Recommendation__c> recList{get; set;}
public List<Recommendation_Rationale__c> rrList{get; set;}
public List<Cheatsheet__c> chtList{get; set;}
public PageReference save(){
   
 return null;
    }
global static string SendToInbox(Integer dataId){
        
         // Picking a dummy Contact
            //User u =[select id, name, email from user where email <> null limit 1];
            Contact c = [select id, Email from Contact where email <> null limit 1];
         
         //List<Recommendation__c> recList; 
        List<Recommendation__c> dataList = new List<Recommendation__c>();
        Recommendation__c recList;   
        recList = [Select id, Account__r.Id, Account__r.Name, Recommendation_Contact__r.Name, Recommendation_Contact__r.Title, 
                                 Recommendation_Contact__r.Contact_YTD_Sales__c, Type__c, Primary_SKU__c, Opportunity_Score__c, Next_Best_Action__c, 
                                 Product_Recommendation__c, Account__r.BillingState, Account__r.Customer_Since__c, Recommendation_Contact__r.Phone ,
                                 Recommendation_Contact__r.Assistant_Phone__c, Recommendation_Contact__r.email , Account__r.Industry, Account__r.Business_Unit__c, 
                                 Est_Value__c, Recom_External_Id__c from Recommendation__c where Recom_External_Id__c =: dataId];
           dataList.add(recList);
           system.debug('Recom#########:' +recList);
           
        
           Recommendation_Rationale__c rrList;
                rrList = [SELECT Id, Rationale1__c, Rationale2__c, Rationale3__c, Rationale4__c, Rationale5__c, 
                                 Rationale6__c, Recommendation__c FROM Recommendation_Rationale__c where Recommendation__c =:recList.id];
        
        
           system.debug('Rationale#####:' + rrList);
        
           Cheatsheet__c chtList;
           chtList= [SELECT Id, Pitch1__c, Pitch2__c, Pitch3__c, Pitch4__c, Pitch5__c, Pitch6__c, Discount_Guidance__c, RelatedProducts1__c, RelatedProducts2__c, RelatedProducts3__c,
                                 RelatedProducts4__c, RelatedProducts5__c, RelatedProducts6__c, Recommendation__c FROM Cheatsheet__c where Recommendation__c =:recList.id]; 
           system.debug('Cheatsheet########:' +chtList);
         
      
        
        //system.debug('Template Data:' +recList);
        //return null;
         // Construct the list of emails we want to send
            List<Messaging.SingleEmailMessage> lstMsgs = new List<Messaging.SingleEmailMessage>();
            Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
         
             msg.setTemplateId( [select id from EmailTemplate where DeveloperName='SalesAI_SendToInbox_VF_EmailTemplate'].id );
              // msg.setTemplateId( [select id from EmailTemplate where DeveloperName='SalesAI_SendToInbox_EmailTemplate'].id );
              //msg.setTemplateId( [select id from EmailTemplate where DeveloperName='SendToInbox_EmailTemplate'].id );
                msg.setWhatId( [select id from Account limit 1].id );
                msg.setTargetObjectId(c.id);
                //msg.setToAddresses(new List<String>{'Azam.Khan@absolutdata.com'});
                msg.setToAddresses(new List<String>{'rajan.mishra@absolutdata.com'});
                msg.setSaveAsActivity(false);
                lstMsgs.add(msg);
                system.debug('############:'+lstMsgs);
        // Send the emails in a transaction, then roll it back
                Savepoint sp = Database.setSavepoint();
                Messaging.sendEmail(lstMsgs);
                Database.rollback(sp);

        // For each SingleEmailMessage that was just populated by the sendEmail() method, copy its
        // contents to a new SingleEmailMessage. Then send those new messages.
                List<Messaging.SingleEmailMessage> lstMsgsToSend = new List<Messaging.SingleEmailMessage>();
                for (Messaging.SingleEmailMessage email : lstMsgs) {
                Messaging.SingleEmailMessage emailToSend = new Messaging.SingleEmailMessage();
                emailToSend.setToAddresses(email.getToAddresses());
                emailToSend.setSaveAsActivity(false);
                emailToSend.setPlainTextBody(email.getPlainTextBody());
                emailToSend.setHTMLBody(email.getHTMLBody());
                emailToSend.setSubject(email.getSubject());
                lstMsgsToSend.add(emailToSend);
            }
            Messaging.sendEmail(lstMsgsToSend);
        
         return null;
}