I have a variable which stores the base 64 image in string. I am trying to send that variable in email body using apex code. Below is code snippet:
Public void submitNewForm()
{
msg='';
try
{
//List<Object> screenImageObject =(List<Object>)JSON.deserializeUntyped(screenImage);
system.debug('ImageString'+string.valueOf(screenImage));
system.debug('<img src='+string.valueOf(screenImage)+'/>');
//insert Evaluation details
Evaluation__C eval= new Evaluation__c();
//5004B000001iP6i,5004B000000fqt6,5004B000000fr2S,5004B000000fr2m,5004B000000fr4O,5004B000000fr4s,5004B000000fr78,
//5004B000000gJ3O,5004B000000gJ3T,5004B000000gJ3Y,5004B000000gJ3d,
//5004B000000gYYT,5004B000000gYYY,5004B000000gYYd,5004B000000gYYi,5004B000000gYYn
eval.Case__c='5004B000000gYYn';
eval.Score__c=0;
eval.Max_Points__c=0;
eval.ResultSnapShot__c='<img src='+string.valueOf(screenImage)+'/>';
insert eval;
//get Evaluation id
String evalId=eval.Id;
Document doc = new Document(Name='sampleImage');
doc.Url=string.valueOf(screenImage);
//mydoc.folderid = UserInfo.getUserId();
update doc;
System.debug('@@@@@@@@@@@@@@' + doc);
//List of all question and answers id's from VF page
List<Object> allQuesAns =(List<Object>)JSON.deserializeUntyped(questionsAnswersArray);
//variable to calculate score and max points
decimal totalScore =0;
decimal maxPoints =0;
// loop of all questions
for (Object quest:allQuesAns)
{
Map<String,Object> singleQuesAns=(Map<String, Object>)quest;
//insert each answer
Answer__c ans= new Answer__c();
ans.Question__c=String.valueOf(singleQuesAns.get('ques'));
ans.Evaluation__c=evalId;
ans.Answer_Option__c=String.valueOf(singleQuesAns.get('ans'));
insert ans;
totalScore=totalScore+decimal.valueOf(String.valueOf(singleQuesAns.get('points')));
maxPoints=maxPoints+decimal.valueOf(String.valueOf(singleQuesAns.get('maxPoints')));
}
//update Score and max points to evaluation
eval.Score__c=totalScore;
eval.Max_Points__c=maxPoints;
update eval;
sendMail();
msg= 'success';
}
catch(Exception ex)
{
msg= ex.getMessage();
}
//redirect page to other page
//PageReference page = ApexPages.currentPage();
//page.setRedirect(true);
//return page;
}
Public void sendMail()
{
// First, reserve email capacity for the current Apex transaction to ensure
// that we won't exceed our daily email limits when sending email after
// the current transaction is committed.
Messaging.reserveSingleEmailCapacity(2);
// Processes and actions involved in the Apex transaction occur next,
// which conclude with sending a single email.
// Now create a new single email message object
// that will send out a single email to the addresses in the To, CC & BCC list.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// Strings to hold the email addresses to which you are sending the email.
String[] toAddresses = new String[] {'[email protected]'};
String[] ccAddresses = new String[] {'[email protected]'};
// Assign the addresses for the To and CC lists to the mail object.
mail.setToAddresses(toAddresses);
mail.setCcAddresses(ccAddresses);
// Specify the address used when the recipients reply to the email.
mail.setReplyTo('[email protected]');
// Specify the name used as the display name.
mail.setSenderDisplayName('Company Support');
// Specify the subject line for your email address.
mail.setSubject('New Evaluation Created : ' + Evaluation__c.name);
// Set to True if you want to BCC yourself on the email.
mail.setBccSender(false);
// Optionally append the salesforce.com email signature to the email.
// The email address of the user executing the Apex Code will be used.
mail.setUseSignature(false);
// Specify the text content of the email.
//mail.setPlainTextBody('Your Case: ' + case.Id +' has been created.');
// screenImage is the variable that comes from a different method in the form of string, which I will use in the setHtmlBody method as below:
mail.setHtmlBody('Hello, Below is the image: <img src='+string.valueOf(screenImage)+'/>');
// Send the email you have created.
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
And I'm calling this method sendMail() in different method where it should fire. I'm doing anything wrong here? Your help would be aprreciate. Thanks.
Messaging.sendEmail()
from sending emails.Messaging.reserveSingleEmailCapacity()
.) My guess is that the email not being sent is just a symptom, and not the root cause. Your issue probably lies within this 'different method' that is supposed to be calling yoursendEmail()
method. If you edit your question to include more code, we'll be better able to work towards the root cause.sendMail()
method. To figure out if that's the case, you could re-throw an exception after handling it withthrow e;
so that it causes execution to stop and print the exception (assuming it wouldn't be caught by some other code up the line). You can also put asystem.debug('msg = ' + msg);
after you handle the exception insubmitNewForm()
, outside of the catch block, and look through the logs via the dev console to see whether or not you see 'success' or an exception message. At any rate, knowing whatmsg
contains is important.23:05:01:881 USER_DEBUG [160]|DEBUG|msg = Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []