+ Start a Discussion
Ajay Kumar 583Ajay Kumar 583 

Need to Send the Email to Opportunity Owner

 Whenever the Opportunity Amount is greater than $500,000. Send email to Opportunity owner with text 
     “A big Opportunity is created in system. Please check out with following link”. Subject of email should be “New big Opportunity for Review”. 
     Make sure when user click on “Link”, user must redirect to Opportunity record for which email was sent. 
     Email will be sent only if "Some Field” Field “Value”is check to true. We should be able to control email sending behaviour based on User/Profile.


Thanks,
Aj
Ajay K DubediAjay K Dubedi
Hi Ajay,
Try this Trigger and handler class:

Trigger:
 
trigger SendEmailOpp on Opportunity (after insert, before update) {
    if((trigger.IsInsert && trigger.IsAfter) || (trigger.IsUpdate && trigger.IsBefore)) {
        SendEmailOpp_handler.findOpportunity(trigger.new);
    }
}

Handler class:
 
public class SendEmailOpp_handler {
    public static void findOpportunity(List<Opportunity> oppList) {
        system.debug('-----' + oppList);
        Set<Id> ownerIdSet = new Set<Id>();
        Map<Id, User> userLMap = new Map<Id, User>();
        List<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();
        for(Opportunity op : oppList) {
            if(op.Amount > 500000 && op.Primary_Contact_Assigned__c == true) {
                ownerIdSet.add(op.OwnerId);
            }
        }
        if(ownerIdSet.size() > 0) {
            userLMap = new Map<Id, User>([SELECT Id, Email FROM User WHERE Id IN : ownerIdSet]);
        }
        system.debug('userLMap----' + userLMap);
        if(userLMap.size() > 0) {
            for(Opportunity op : oppList) {
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                if(userLMap.containsKey(op.OwnerId)) {
                    String[] toAddresses = new String[] {String.ValueOf(userLMap.get(op.OwnerId).Email)};
                    mail.setToAddresses(toAddresses);
                }
                mail.setSubject('Your Opportunity Amount is grater then 500000.');
                String body = 'A big Opportunity is created in system. Please check out with following link”. Subject of email should be “New big Opportunity for Review';
                body += '<br/>https://dev-ed.my.salesforce.com/' + op.Id;
                mail.setHtmlBody(body);
                mails.add(mail);
            }
            Messaging.sendEmail(mails);
        }   
    }
}

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