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
Jack OliverJack Oliver 

Trigger :- send an email to all Accoun if opportunity associted with it reaches 'closed won'

give me  solution
Andrew GAndrew G
Where is the "please"?

You don't need a trigger.  You can do it via a process builder.

Regards
Andrew
Jack OliverJack Oliver
thanks Andrew G 
but i want to do this via Trigger so if you have solution help me 
Ramesh DRamesh D
Hi Shubham,
trigger ClosedOpportunityTrigger on Opportunity (after insert,after update) {
    List<Opportunity> opps=new List<opportunity>();
    if (Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)) {
        for(opportunity opp:Trigger.New){
            if(opp.StageName=='Closed Won')
            {
                opps.add(opp);
            }
            
        }
    }
    
    if(opps.size()>0)
        //Send all opportunities to your helper class and get all accounts related 
        AccountTriggerHandler.SentEmails(opps);
}

I hope you find the above solution helpful. If it does mark as best answer to help others too.
Thanks,
Ramesh D
Deepali KulshresthaDeepali Kulshrestha
Hi Shubham,

You can obtain this with the help of process builder and you don't need to apply trigger for that

User-added image
User-added image


I hope you find the above solution helpful. If it does please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
 
Ajay K DubediAjay K Dubedi
Hi Shubham,
Try this code to send an email to all Accounts if opportunity associated with it reaches 'Closed Won'  :
Trigger:
trigger SendEmailToAccount on Opportunity (before insert, before update) {
    if(trigger.isInsert && trigger.isBefore){
        SendEmailToAccount_handler.checkAccountAccociatedWithOpportunity(trigger.new);
    }
    
    if(trigger.isUpdate && trigger.isBefore){
        SendEmailToAccount_handler.checkAccountAccociatedWithOpportunity(trigger.new);
    }
}
Trigger Handler :
public class SendEmailToAccount_handler {
    public static void checkAccountAccociatedWithOpportunity(List<Opportunity> oppList) {
        try {
            if (oppList.size() > 0) {
                Set<Id> accIdSet = new Set<Id>();
                List<Account> accList = new List<Account>();
                for (Opportunity op : oppList) {
                    if (op.AccountId != null && op.StageName == 'Closed Won') {
                        accIdSet.add(op.AccountId);
                    }
                }
                if (accIdSet.size() > 0) {
                    accList = [SELECT
                               Id,
                               Email__C
                               FROM Account
                               WHERE Id IN : accIdSet
                               LIMIT 10000];
                }
                List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
                // http://www.sfdc99.com/2014/03/01/sending-emails-using-apex/ follow this link to send Email .
            }
        } catch (Exception ex) {
                system.debug('Exception---ofLine--->' + ex.getLineNumber());

            }
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi