+ Start a Discussion
csbaacsbaa 

send email from after Insert trigger on User

Hello Helpers

 

I have a requirement to inactivate an User right after creation  and send a notification  to the new User

This is not  aregular user bu t achatter free user who is invited to join a Group

 

The bussiness logic  is to deny  logging into the org  for Chatter free User  till they  perform some actions

 

I created a class contaning  a @future method  which  inactivates  an User  and send an email to that user

 

I call this future method  from within an after insert trigger on User object

 

The inactivation work always OK  but the mail is not always delivered.

I have no idea why  and I can not even debug

 

 

I can not  chech the debug  log  because the  user is not created manually by an admin but  by salesforce when chatter free user fill in the invitation form. 

 

any suggestion or a workaround?

 

thanks in advance

Csaba

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
csbaacsbaa

Thanks for your reply

 

I managed to send  mail  to an inactive User  from within  an update trigger

 

Meanwhile  I made progresses on the solution

 

I transferred the mail sender method  fro the future call   into the after update trigger on User

 

The after inserrt trigger  call the future method

 the future methods  inactivates the usert.  

This filre the update trigger  and from there I send the mail

 

 

for mail sending I used  yhe method  you suggested

I think the prblem was that I tried to send the mail from a future call

 

Regards

Csaba

All Answers

swatKatswatKat

I am not sure if email can be sent to an inactive user. Can you send the mail and then deactivate the user in the future method ?

MJ Kahn / OpFocusMJ Kahn / OpFocus

I don't think you can send an email to an inactive User, but you can send to the User's email address via the setToAddresses() method.

 

I haven't run this myself, but something like this should work:

 

User u = [select id, email from user where id = :newUserId];
u.IsActive = false;
update u;

Messaging.SingleEmailMessage m = new Messaging.SingleEmailMessage();
m.setSubject('The Subject');
m.setPlainTextBody('The Body');
m.setToAddresses(new String[] {u.email});
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{m});

 

Of course, if this is running in a trigger, you'll want to bulkify it. 

 

IMHO, if you can accomplish your goal without using @future, you'll be better off, since there's a limit to the number of concurrent @future methods that can run, and if your new users are being created by code, it's possible you could be creating enough Users to cause you to hit that limit.

csbaacsbaa

Thanks for your reply

 

I managed to send  mail  to an inactive User  from within  an update trigger

 

Meanwhile  I made progresses on the solution

 

I transferred the mail sender method  fro the future call   into the after update trigger on User

 

The after inserrt trigger  call the future method

 the future methods  inactivates the usert.  

This filre the update trigger  and from there I send the mail

 

 

for mail sending I used  yhe method  you suggested

I think the prblem was that I tried to send the mail from a future call

 

Regards

Csaba

This was selected as the best answer
KasiWKasiW

hi csbaa,  would you share how did you write an update trigger on user, I have a reguirments to:

- send a email "welcome to chatter" to every new user that is entered into Salesforce

- all new users that don't have profile  "Subcontractor" should receive the email

 

your feedback would be much appriciated

 

KasiW