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
SubC4i-dev1SubC4i-dev1 

Checkbox To Send New User Email

I've created a checkbox for the User page layout to send out a custom new user email.  The goal is to send out the email when the checkbox is true, deploy the email, and then uncheck the box.  I've got everything working in before update, but I cannot get it working for insert.  I need to use After Insert to deploy the email because the email template I created in the apex class needs to be passed user field info (pass the User Id, name, email, etc).  If I uncheck the box in the before insert, then the criteria to fire the after insert will no longer exist.  I even tried making the method that unchecks the box @future, but that didn't work.  I would prefer to create another component like a workflow that would fire after the triggers due to SFDC order of execution.  Want a clean all apex solution for this.

 

This seems like a simple process, but I'm stuck at the moment.  Any help would be greatly appreciated.

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
You should be able to do this in pure workflow without a trigger at all. Have the workflow rule trigger on creation or update, filtered by if the box is checked. From there, have the workflow rule take two actions: the first sends the email, the second uses a field update to uncheck the box.

All Answers

sfdcfoxsfdcfox
You should be able to do this in pure workflow without a trigger at all. Have the workflow rule trigger on creation or update, filtered by if the box is checked. From there, have the workflow rule take two actions: the first sends the email, the second uses a field update to uncheck the box.
This was selected as the best answer
SubC4i-dev1SubC4i-dev1

Thanks sfdcfox!  I overlooked the simplier approach.  But I guess this means I couldn't do it in the trigger.  I was equally curious to see if the limitation was my apex coding ability.

sfdcfoxsfdcfox

It's not that you can't use a trigger, is that the approach is a little more complicated than you'd expect:

 

trigger SendEmail on User (after insert, before update) {
   if(Trigger.isAfter && Trigger.isInsert) {
       update Trigger.new.deepClone(true);
   }
   if(Trigger.isBefore && Trigger.isUpdate) {
       // Loop through users, find checkbox, send email, uncheck box
   }
}

This recursive trigger trick is necessary because you need to be able to update the user's checkbox ("before"), and you need a user ID ("after insert"). Using deepClone prevents the "DML on Trigger.new not allowed" error, and keeps the solution simple with no need for future methods or any other elaborate schemes.

SubC4i-dev1SubC4i-dev1

When I responded I totally forgot why I had to use apex in the first place, it was the only way to auto-send an email to dynamic email recipients.  I will use your suggestion of purposely setting off a recursive update to solve this issue.  I will have to look more into what the deepClone is and does, but it sounds interesting.  Thanks for the help.

SubC4i-dev1SubC4i-dev1

For anyone else, who may be reading this thread.  Here is the link I found on deepClone()

 http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_System_List_deepClone.htm

SubC4i-dev1SubC4i-dev1

sfdcfox, that worked like a charm!

 

Final trigger code:

trigger PRM_NewUserEmail_Trigger on User (after insert, before update) {

	List<User> userList = new List<User>();

	if(trigger.isAfter && trigger.isInsert){
		update trigger.new.deepClone(true);
	}
		
	if(trigger.isBefore && trigger.isUpdate){
		For(User u: trigger.new){
			if(u.ProfileId == 'xxxxxxxxxxxxxxx' && u.Send_PRM_New_User_Email__c == true){
				userList.add(u);
			}
		}
		if(userList.size() > 0){
			PRM_NewUserEmail_Class.SendEmail(userList);
			PRM_NewUserEmail_Class.resetPRMcheckbox(userList);
		}
	}
}

 

Kiranmai YadagiriKiranmai Yadagiri
Hai Actually,
I want the PRM_NewUserEmail_Class.SendEmail(userList); PRM_NewUserEmail_Class.resetPRMcheckbox(userList); CLASSES.
I 'm bit confussed in passing them.
If you dont mind please share those two classes also.