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
kailash chandrakailash chandra 

user merge fields doesn't replace by user ID

Hi,
I am trying to send email through email template to user but It is always replacing the user merge fields by org admin's user name instead of targetobjectID user.
Org admin : luke wright
OrgWideEmailAddress owa = [select id, DisplayName, Address from OrgWideEmailAddress limit 1];
List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>(); 
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
mail.setTemplateID('00X0S000000iBYB'); // Template Id
mail.setSaveAsActivity(false);
mail.setTargetObjectId('0050S0000023KS9'); // UserId  Name: John Doe
mail.setOrgWideEmailAddressId(owa.id);
allmsg.add(mail); 
Messaging.sendEmail(allmsg,false);
Email template Body :
-------------------------------------------------------------------------
Dear {!User.Name} ,
Hearty Congratulations on this beautiful occasion. May your marriage bring great joy, love and passion in your life. I wish to God to bless you with a wonderful marriage and a happy life ahead.
Thanks and Regards.
{!Organization.Name}
----------------------------------------------------------------

Issue: Dear {!User.Name}  should be replace by john doe while it is replacing by luke wright and email goes to correct user i.e.   john doe  
P.S.  User's merge fields is not replacing by that record ID


Please help me
Best Answer chosen by kailash chandra
kailash chandrakailash chandra
issue resolved!!!
I used {!receiving_user.Name} instead of {!user.Name}

All Answers

kailash chandrakailash chandra
any update?
kailash chandrakailash chandra
issue resolved!!!
I used {!receiving_user.Name} instead of {!user.Name}
This was selected as the best answer