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
TilluTillu 

Sending Email Missfire?

I have created a field "userassigne" to account object .this field is lookup to User. And i have writtened a workflow that if "Userassigne" equals Null. then email alert to send an email to selected user from lookup.

I have added all users in the Selected List in workflow email alert.

while creating new record i have given 1 user in Userassigne field. But the email sent to all users   why.
So i have removed all users and given only one user in selected list in workflow. Again when i am trying to send a mail even what ever the user that i have selected the email sent to only one person which i have added in selected list in workflow.

How to solve. what ever the user that i have selected in lookup, the email should be sent to that user only.?
Ashish_SFDCAshish_SFDC
Hi Tillu, 


Workflow does not have the feature to Dynamically Check the User which is assigned and Send email only to  that user. 

You have to use a Trigger. 


Regards,
Ashish
Nirmal ChristopherNirmal Christopher
Yes Tulu you cannot dynamically send email using workflow. the bst option is to write  a trigger . if you need help let me know.
TilluTillu
Yes nirmal i need your help. Can you tell me the trigger? And hw we can send email through trigger when record is saved?
Nirmal ChristopherNirmal Christopher
This is a sample code. try modifying this code as per ur requirement. Can you tell me how the system will determine which user it has to send email. The below code is a sample to send mail when team owner is changed. It will send mail to current owner and new owner. If you need any help let  me know..
/************* 
*******Created by:Nirmal Christopher 
*******Created Date:02.05.2013 
*******Description:Email will be sent if the owner is changed to new owner and old owner of the respective lead record 
*************/ 
trigger SendEmailOnOwnerChange on Lead (before update,After Update) { 
   if (trigger.old[0].OwnerId != trigger.new[0].OwnerId ) { 
     Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
     //Select the Email Address of the old owner 
     String emailAddrnew = [select Email from User where Id = :trigger.old[0].OwnerId].Email; 
     String emailAddrold = [select Email from User where Id = :trigger.new[0].OwnerId].Email; 
     //Select the new owner and old owner from user 
     String newOwnerName = [select Name from User where Id = :trigger.new[0].OwnerId].Name; 
     String oldownerName=[select name from User where Id =:trigger.old[0].OwnerId].Name; 
     //Store the email address 
     String[] emailAddrnew1 = new String[] {emailAddrnew}; 
     String[] emailAddrold1 = new string[]{emailAddrold}; 
     mail.setToAddresses(emailAddrnew1 ); 
     mail.setCcAddresses(emailAddrold1 ); 
     mail.setSubject('Owner Changed for Lead : ' + trigger.new[0].Name); 
     mail.setPlainTextBody('Owner of lead: ' + oldownerName + ' Changed to ' + newOwnerName); 
     mail.setHtmlBody('This is to notify that the Owner of lead: https://ap1.salesforce.com/ <b>' + trigger.new[0].Id+'<b>&nbsp;&nbsp;Changed from</b>&nbsp;&nbsp;'+oldownerName + '</b>&nbsp;&nbsp; Changed to <b>' + newOwnerName + '</b>'); 
     Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); 
   } 
}
PeaceMakerPeaceMaker
This can be done without Trigger, just by using Workflow.

If the userassigne field is a lookup(User) field.

Firsr remove all the Users selected in the work flow Email alert . then follow the below steps.
 
then you can choose the same field in Workflow Email Alert "Choose Related User" in Recepient Type. you will be seeing the "userassigne" listed in the available recepients section.  move the field to the Selected Recepients section and you are done. now you can test.  you will see the email sent to the user selected in the "userassigne" only.

if you are facing any difficulties let me know. if it works then mark this as solution.

Thanks