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
Thakkar ParthThakkar Parth 

Trigger help - An email from Owner to the Lead

I've written a trigger where a notification in form of an email is sent to the  Owner when the Lead Status field is changed .But the requirement was different and i did it to send email to the Owner which is not.

 A help in Trigger where the requirement is that, an email should be sent from the Owner to the particular Lead Please help me to achieve this..Below is the code.

Really appreciate the help.
trigger UpdateLeadStatus on Lead (after update) {
    Map <Id, String> leadsOwner = new Map <Id, String> ();

    // Go through every lead in the trigger
    for (Lead lead : trigger.new)
    {
        // Check if the status has been changed
        if (lead.Status != trigger.oldMap.get(lead.Id).Status)
        {
            // get the owner ID's that have been affected
            leadsOwner.put(lead.ownerId, null);
        }
    }

    // Map the owner ID to it's email address
    for (User owner : [SELECT Id, Email FROM User WHERE Id = :leadsOwner.keySet()])
    {
        leadsOwner.put(owner.Id, owner.Email);
    }

    List <Messaging.SingleEMailMessage> emails = new List <Messaging.SingleEMailMessage> ();

    // Go again through every lead in the trigger
    for (Lead lead : trigger.new)
    {   
        // Only work with leads that have owners mapped to their email addresses (only those ones have their status changed)
        if (leadsOwner.get(lead.OwnerId) != null)
        {
            // Create an email message and add it to the list
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
            List <String> toAddresses = new List <String> {leadsOwner.get(lead.OwnerId)};
            mail.setToAddresses(toAddresses); 
            mail.setSubject('Automated Email : Lead Status Updated'); 
            String body = 'The status has been changed on the lead record with ID ' + lead.Id; 
            mail.setPlainTextBody(body); 
            emails.add(mail);
        }
    }
    Messaging.sendEmail(emails);
}


AshwaniAshwani
At high level code seems working. But there are two thing:

1) Make sure you organisatin has enabled sinlge messaging 
2) Also instead of using mail.setToAddresses(toAddresses);try to use setTargetObjectId(lead.id); other you may hit sinlge messaging limit.
Thakkar ParthThakkar Parth
Thanks for the quick response . Using setTargetObjectId(lead.id); is giving me an error .

It says : Error: Compile Error: Method does not exist or incorrect signature: setTargetObjectId(Id) at line 33 column 13 .   Dont know why its giving me an error while it should work.

Also how do i enable single messaging for my Org . Could you please help me.Thanks




Boris BachovskiBoris Bachovski
There is no way to set the FROM address though, you can only use your organization wide email address that the emails will go out from.
Thakkar ParthThakkar Parth
Accepted and really appreciate . Could you please help me to know why there is NO other way to set FROM address .? 
David BermanDavid Berman
Did you try: mail.setReplyTo('info@someemail.com');