Once the new user is created in Salesforce I want to send welcome email to the user automatically.

for the above process, i have created custom check box"Welcome_Mail__c" in user detail page. If i checked that checkbox below trigger will get invoked.

(Below one is the updated code: Sorry for the confusion)


trigger TestUserTrigger on User(before insert)
  User[] SendEmail=Trigger.new;


global class UserClass{

public static void sendEmailMethod(User[] SendEmail)
  String subject;
  String body;

  for (User U: sendemail)


    String UserEmail=u.email;
    subject = 'Test Email- Welcome to Salesforce';
    body = 'Hi Team, \n\n Test email- welcome to salesforce\n\n\n Thanks,\n\n Sabeena';
    List<String> sendTo = new List<String>();
    Messaging.singleEmailMessage Emailwithattch = new      Messaging.singleEmailMessage();
    // Sends the email

    Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.Email[] { Emailwithattch });



the above trigger will invoke the class"UseClass" and then mail will be sending to the corresponding user.

My Problem is, even if i dont check the check box"Welcome_Mail__c" and save the user record, the mail is sending to the corresponding user and i can see the check box has enabled in user detailpage after saving the user record. Kindly Note: while creating the custom check box "Welcome_Mail__c" I have given unchecked as default, Still it is not working.

Could you please help me to fix this issue. It would be very helpful for me. Thanks in Advance!

  • I have few doubts here a) How the classobj understands the current user b) where is the If pair of the else. Am I missing some part of the code here ? Feb 9 '16 at 19:09
  • 1
    how does classObj know what User to send email too? Plus, you are sending email within a for loop that can lead to other issues. This kind of problem is best handled by workflows or process builder
    – cropredy
    Feb 9 '16 at 19:09
  • Looks like there's an errant else block in your provided code, which would prevent you from saving this trigger in the first place. Could you copy your trigger again, and edit your post to reflect the correct code? crop1645's suggestion of accomplishing this through workflow / process builder is a great solution (to use instead of your trigger). That said, accurate code will help in providing an accurate answer to the text of your question.
    – Derek F
    Feb 9 '16 at 19:16
  • HI All, Thank you so much for your responses. I have updated my code above. Sorry for the confusion. As i mentioned above. Even if i dont enable the checkbox and saves the new user record, it generating the email by enabling the check box automatically.
    – Sab__Dev
    Feb 10 '16 at 22:19
  • @karthikselva i have updated my now, Kindly look into the updated one.
    – Sab__Dev
    Feb 10 '16 at 22:28

I believe there is a miss in the following line.

if(U.Welcome_Mail__c=true). // Use == or the checkbox itself instead of assignment.

Since you are assigning the value of the variable instead of comparing it, it will always return true.


Boolean check = false;
System.debug(check=true); //Returns true
System.debug(check==true); // Returns true Since the assignment happened in previous statement
System.debug(check=null); //Returns null

It should be




The assignment assigns the value and returns the assigned value. I cannot find SFDC Documentation but there is a Java doc stating this

At run time, the result of the assignment expression is the value of the variable after the assignment has occurred. The result of an assignment expression is not itself a variable.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.