You need to sign in to do that
Don't have an account?
Zander Zumbrunnen
sendEmail() not working more than once
Hello,
I am trying to send an email at the push of a button using LWC and Apex.
This is my current Apex function that sends the email, this is where I'm guessing the problem is.
Am I missing something here?
I am trying to send an email at the push of a button using LWC and Apex.
This is my current Apex function that sends the email, this is where I'm guessing the problem is.
@AuraEnabled(cacheable=true) public static Boolean sendResources(String email, Id accountId) { List<Selected_Resource__c> resources = new List<Selected_Resource__c>([SELECT Name, Name__c, Description__c, Phone__c, Street_Address__c, City__c, State__c, Zip__c FROM Selected_Resource__c WHERE Account__c = :accountId AND Active__c = True ORDER BY CreatedDate Desc LIMIT 10]); Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); message.setToAddresses(new String[] {email}); message.setReplyTo('example@example.com'); message.setSenderDisplayName('Example'); message.setSubject('Selected Resources'); String body = ''; for (Integer i = 0; i < resources.size(); i++) { body = body + '<h2>' + resources[i].Name__c + '</h2><p>' + 'Description: ' + resources[i].Description__c + '</p><p>' + 'Phone: ' + resources[i].Phone__c.substring(0, 3) + '-' + resources[i].Phone__c.substring(3, 6) + '-' + resources[i].Phone__c.substring(6, 10) + '</p><p>' + 'Address: ' + resources[i].Street_Address__c + ', ' + resources[i].City__c + ', ' + resources[i].State__c + ' ' + resources[i].Zip__c +'</p> </br>'; } message.setHtmlBody(body); Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {message}; Messaging.SendEmailResult[] results = Messaging.sendEmail(messages); if (results[0].isSuccess()) return true; else return false; }I am able to get an email the first time I call this but every time after that it will return true but not actually send the email.
Am I missing something here?
Greetings!
Have you captured the debug logs to see,if there is any issue in the next transaction.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_debugging_debug_log.htm
Kindly mark it as best answer if it helps so that it can help others in the future.
Warm Regards,
Shirisha Pathuri
Here is some more information about my project, I have a modal with an email input and a next button. When the button is clicked it creates a certain number of records depending on a list called selected resources. After the last record is created it updates the account with the email and calls that send email function if an email was inputted. I put in some toast messages for lazy debugging, every time I click the button both success toast messages are shown (first one being used to show that the right part of the if statement is being called every time and the second one being used to show that the email should have been sent). Since the second toast message was in the .then of the apex function I assumed it was being called.
Since that button updates the account and sends the email there should be 2 logs every time but after the first press only the update logs show up.
Here is the email input and button in the modal: Here is the handleNext function:
If I try to send the email right after just sending one it does not work, but if I wait a minute or 2 without refreshing and click it again it does send the email.
Is this a known issue? And is there a workaround? I'd rather not have to wait an unknown amount of time before being able to send another email.