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
Dev 長尾拓磨Dev 長尾拓磨 

When send mail using template, GuestUser's Name bind User.Name

The supplier guest user can use the mail template,
When sending a mail to a specific user,
I'm planning to insert the target user's item in the merge item,
The data of the site guest user is inserted.
For example, if you write {! User.Name} in a mail template, "Site Guest User" comes in here.

I will write a concrete situation.
On the self-registration screen of the community, enter and register e-mail.
<Apex: inputField value = "{! RegistUser.Email}" />

Apex side
List <User> userQuery = [select Id from User where Email =: registUser.Email LIMIT 1];
If (userQuery! = Null && userQuery .size ()> 0) {
          //send e-mail
          SendMail (userQuery [0] .Id);
} Else {
          //registration process
}


/ *** Mail transmission processing *** /
Public void sendMail (Id userId) {
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage ();
        
        Mail.setTargetObjectId (userId);
        Mail.setSenderDisplayName ('Sender');
        Mail.setUseSignature (false);
        Mail.saveAsActivity = false;

        EmailTemplate template = [select Id from EmailTemplate where DeveloperName = 'Mail_SupplierUserRegist' LIMIT 1];
        Mail.setTemplateID (template.Id);
        
        // Send here
        Messaging.sendEmail (new Messaging.SingleEmailMessage [] {mail});
          
}

Roughly, it is such a process.
I'd like to embed the name of the destination user by putting {! User.Name} in the body of the template.
However, this is the event that the site guest user name comes in here.


Therefore, I can not determine the cause, but I would like to have a professor if you know someone.
Thank you.