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 

Can i use setOrgWideEmailAddressId(Id

Can i use setOrgWideEmailAddressId(Id) to send the FROM address in my case. My requirement is to send an email FROM Owner to the particular Lead when the Lead Status is changed.

I've enabled Org Wide Addressed in the Org Setup. Please 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);
}


Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
hi,

First you need to query org wid email address as
OrgWideEmailAddress owa : [select id, Address, DisplayName from OrgWideEmailAddress limit 1];

// set it in meassage as

mail.setOrgWideEmailAddressId(owa.id);

If this helps, mark this as best answer.

thanks,
N.J
Thakkar ParthThakkar Parth
But how do i use it it in my code structure ? Help appreciated .
Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
trigger UpdateLeadStatus on Lead (after update) {
    Map <Id, String> leadsOwner = new Map <Id, String> ();
	OrgWideEmailAddress owa : [select id, Address, DisplayName from OrgWideEmailAddress limit 1];
	
    // 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)};
			// set it in meassage as
			mail.setOrgWideEmailAddressId(owa.id);
            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);
}

Thanks,
N.J