0

I have created an LWC with a form to create a Lead and placed it on the community site which is public. Now when a Lead is created there's a trigger that is supposed to run and send an email to the lead email address. It is working when using the community site while logged in to the org but when I'm using the site from an incognito tab the lead is being created but the email is not sending and also there's no error in the debug logs. How can I send the email from the guest user using Apex class? I have already given the permissions of the classes as well as to standard objects i.e. Lead and also checked the send email checkbox in the guest user profile.

TRIGGER FOR LEAD

trigger candidateTrigger2 on Lead (after insert) {
try{
    system.debug('test trigger running');
    CandidateTriggerHandler.leaditeration(trigger.new);
}
catch(Exception e)
{
    system.debug('error'+e.getMessage());
    system.debug('error2'+e.getCause());
    system.debug('error3'+e.getLineNumber());
}

}

CONTROLLER FOR LEAD TRIGGER

public class CandidateTriggerHandler {

public static void leaditeration( list<lead> leadList)
{
    Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage>();
    
    String ownerEmail;
    String leadid;
    for(Lead ld:leadList)
    {
        if(ld.Position__c!='Associate Software Engineer')
        {
            String email=ld.Email;
            String emailtemplate1='Submission_email_H';
            leadid=ld.id;
            ownerEmail=ld.ownerEmail__c;
            messages= CandidateTriggerHandler.sendemailtoclient(ld.id,emailtemplate1,email);
        }
        else
        {
            // Associate Software Engineer
            leadid=ld.id;
            String emailtemplate2='Asc_app_Submission_H';
            String email=ld.Email;
            ownerEmail=ld.ownerEmail__c;
            messages= CandidateTriggerHandler.sendemailtoclient(ld.id,emailtemplate2,email);
        }
        
    }
    if(ownerEmail!=null)
    {
        CandidateTriggerHandler.sendemailtohR(leadid,ownerEmail);
    }
    Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
    
    if (results[0].success)
    {
        System.debug('The email was sent successfully.'+results);
    }
    else
    {
        System.debug('The email failed to send:' + results[0].errors[0].message);
    }
}
public static List<Messaging.SingleEmailMessage> sendemailtoclient(Id Leadid,string templateName,string email){
    system.debug('inside sendemailtoclient');
    System.debug('inside ==>>  '+email);
    
    Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
    message.setTargetObjectId(Leadid);
    message.setTreatTargetObjectAsRecipient(false);
    EmailTemplate emailTemplate =[Select Id,Subject,Description,HtmlValue,DeveloperName,Body from EmailTemplate where DeveloperName=:templateName];
    message.setTemplateID(emailTemplate.Id);
    message.toAddresses = new String[] { email};
        Messaging.SingleEmailMessage[] messages = new  List<Messaging.SingleEmailMessage> {message};
            //Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
            
            
            return messages;
} 

public static void sendemailtohR(Id Leadid,string email){
    String templateName='Candidate_Creation_Email';
    Messaging.SingleEmailMessage message2 = new Messaging.SingleEmailMessage();
    message2.setTargetObjectId(Leadid);
    EmailTemplate emailTemplate =[Select Id,Subject,Description,HtmlValue,DeveloperName,Body from EmailTemplate where DeveloperName=:templateName];
    
    
    message2.setTemplateID(emailTemplate.Id);
    message2.setTreatTargetObjectAsRecipient(false);
    message2.toAddresses = new String[] { email};
        
        system.debug('message==>> '+message2.toaddresses);
    Messaging.SingleEmailMessage[] messages2 = new  List<Messaging.SingleEmailMessage> {message2};
        system.debug('message==>> '+messages2);
    Messaging.SendEmailResult[] results = Messaging.sendEmail(messages2);
    system.debug('results==>> '+results);
    
    
    
} 

}

2

1 Answer 1

1

setting org-wide email address worked for me as the guest user didn't have any email address to send the email.

1
  • Thanks this is the only thing that works for us.
    – yongabyte
    Jul 11 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy