I am having difficulties getting the Communities Welcome email to send to new users added to the community. What happens is that we insert a lead via a web-to-lead form that is inserted with a guest user license. When the lead is inserted into Salesforce, if not a duplicate it will automatically convert to a contact and create an opportunity. There are more formulas in place to determine to create a community user in code. This all happens at once and is created by the guest user. As it is with Communities, if a user is created by another user who does not have the Create or Manage Communities permission, the user is still added to the community, however, no welcome email is sent. In an attempt to work around this issue, I tried creating a workflow rule on the user to send the Welcome email template on create a field is set to 'Yes' and the Profile is set to the community profile. When I tested this, no email was sent. I checked the debug logs and something resulted in a null reference exception. Since this code has been working before, I'm assuming that the null reference exception came from trying to use the {!Community.[field]} in the email template.
Is there a way to force this email template to send in apex code where I can associate it to the correct Network (Community) AND User? When looking at the send email methods that you can use in apex, it refers to a TartgetObjId, which is the id that will be receiving the email. I don't feel like that will associate the user to the corresponding community.
Thanks