• Leena Wani 21
  • 0 Points
  • Member since 2019

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
  • 1
I have a opportuity trigger that sent an email before update the opporunity. Email sent properly but it send twice. I have also tried to update customer field and checked it in the condition but not solved. We have mulitiple triggers on opportunities, so may be possible it executes same time. Please help me on it.
trigger billingSubscriptionEmail on Opportunity (before update) {

List<Addon_Opportunity__c> addOppList = new List<Addon_Opportunity__c>([SELECT Name,Amount__c,Quantity__c FROM Addon_Opportunity__c where Id__c IN :Trigger.newMap.keySet()]); 

List<Contact> conList = new List<Contact>([select Email from Contact where Id In (select ContactId from OpportunityContactRole where OpportunityId IN :Trigger.newMap.keySet())]); 

 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    for(Opportunity opp : trigger.new){
            if(opp.StageName=='Closed Won' && Trigger.oldMap.get(opp.Id).StageName!='Closed Won'){
                for(Contact c:conList){
                 String[] toAddresses = new String[] {'ahmeds@webgility.com'};
                 String billingCycle;
                 String promo='';
                    mail.setToAddresses(toAddresses );   
                    mail.setSubject('New Subscription - '+opp.Accounting_Platform__c+' - '+c.Email);   
                     billingCycle = 'Monthly';
                      billingCycle = 'Annual';  
                      promo = opp.promo_code__c;
                      promo = 'None';
                    String template = 'Billing & Subscription Info:\n\n';
                     template+= '----------------------------------\n';
                     template+= billingCycle+' Recurring Amount: $'+opp.Recurring_Revenue__c+'\n';
                     template+= 'Plan: '+opp.Plan__c+'\n';
                     template+= 'Account Email: '+c.Email+'\n';
                     template+= 'Total Upfront Payment: $'+ opp.One_Time_Fee__c+'\n'; 
                     template+= '----------------------------------\n';
                     for(Addon_Opportunity__c addon :addOppList){
                         template+= addon.Quantity__c+' '+addon.Name+' : $'+addon.Amount__c;
                         template+= '\n';
                    template+= '\n';
                    template+= 'One Time Fee: $'+opp.One_Time_Fee__c+'\n';
                    template+= '----------------------------------\n';
                    template+= 'Recurring billing start date :'+opp.Purchase_Date__c+'\n';
                    template+= 'Promo Code :'+promo;
                    List<String> args = new List<String>();
               String formattedHtml = String.format(template, args);
                Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});



Thanks in advance