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
aressaress 

Email on order creation

I have a requirement that on order creation i want to send detail mail to the corresponding opportunity that created the order.
How to achieve that?
Best Answer chosen by aress
Raj VakatiRaj Vakati
sample code 
 
trigger SendEmailTOopp on Order (after insert) {
    
    //list of emails
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    if(Trigger.isAfter){
        if(Trigger.isInsert ){ 
            //loop
            for(Order orders : Trigger.new){
                //check for Account
                if(orders.Opportunity == null && orders.Opportunity.Owner.Email != null){
                    //initiallize messaging method
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    String[] toAddresses = new String[] {orders.Opportunity.Owner.Email};
                        mail.setToAddresses(toAddresses);
                    mail.setSubject('Order created  : ');
                    mail.setHtmlBody('Order created <b>');
                    //add mail
                    emails.add(mail);
                    
                    
                }
            }
            
            //send mail
            Messaging.sendEmail(emails);
        }
    }
}

 

All Answers

Raj VakatiRaj Vakati
You wanted to send to opportunity owner ?? If so you need to write the trigger 
Raj VakatiRaj Vakati
sample code 
 
trigger SendEmailTOopp on Order (after insert) {
    
    //list of emails
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    if(Trigger.isAfter){
        if(Trigger.isInsert ){ 
            //loop
            for(Order orders : Trigger.new){
                //check for Account
                if(orders.Opportunity == null && orders.Opportunity.Owner.Email != null){
                    //initiallize messaging method
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    String[] toAddresses = new String[] {orders.Opportunity.Owner.Email};
                        mail.setToAddresses(toAddresses);
                    mail.setSubject('Order created  : ');
                    mail.setHtmlBody('Order created <b>');
                    //add mail
                    emails.add(mail);
                    
                    
                }
            }
            
            //send mail
            Messaging.sendEmail(emails);
        }
    }
}

 
This was selected as the best answer