+ Start a Discussion
Ram SrivastavRam Srivastav 

Receiving 2 x initial mail instead of one Description

Receiving 2 x initial mail instead of one
Description Hi, I have written a trigger based workflow which will send one acknowledgment to the Customer with an template. Problem is same mail is sending twice to the customer. Anyone help me to get it solve. It will be highly appreciated.
please see the code snippet

trigger sendEmail on Case (after insert,after update) {
    Public static Boolean InitialEmail =false;
    Public Static Boolean FinalEmail =false;
   
    for(Case c:trigger.new) {
       
        system.debug('outside'+c.Send_Email_to_Contact__c);
       
        if ((trigger.isInsert || (trigger.oldMap.get(c.Id).Send_Email_to_Contact__c != c.Send_Email_to_Contact__c)) && (c.Send_Email_to_Contact__c && !c.Do_not_Send_Email__c  && c.Email_of_Complainant__c!=null && c.Status!='Completed')) {
               
                system.debug('??????'+c.Send_Email_to_Contact__c);
              
                    sendEmailMessage(c.id,c.ownerId,c.Internal_Comments__c,c.Email_of_Complainant__c,c.Site_Details__c,c.CaseNumber,c.Case_Customer_Reference__c );
        }
       
       
       
        if ((trigger.isInsert || (trigger.oldMap.get(c.Id).Status != c.Status)) && (c.Final_email_to_contact__c && !c.Do_not_Send_Email__c  && c.Email_of_Complainant__c!=null && c.Status=='Completed')) {
               
                system.debug('****************'+c.Send_Email_to_Contact__c);
               
                sendFinalEmailMessage(c.id,c.ownerId,c.Internal_Comments__c,c.Email_of_Complainant__c,c.Site_Details__c,c.CaseNumber,c.Case_Customer_Reference__c);
        }
       
   
    }
   
    public void sendEmailMessage(String CaseId,String ConId,String internalComments,String EmailOfComplaint,String SiteDetail,String CaseNumber,String CustRef){
           
            Messaging.reserveSingleEmailCapacity(2);

            // Processes and actions involved in the Apex transaction occur next,
            // which conclude with sending a single email.
           
            // Now create a new single email message object
            // that will send out a single email to the addresses in the To, CC & BCC list.
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
           
            // Strings to hold the email addresses to which you are sending the email.
           
            String[] toAddresses = new String[] {EmailOfComplaint};
          //  String[] ccAddresses = new String[] {'koushik.saha@wipro.com'};
            String[] BccAddresses = new String[] {'Opsinvestigationteam@thameswater.co.uk'}; 
           
            // Assign the addresses for the To and CC lists to the mail object.
            mail.setToAddresses(toAddresses);
          //  mail.setCcAddresses(ccAddresses);
            mail.setBccAddresses(BccAddresses);
           
            // Specify the address used when the recipients reply to the email.
            mail.setReplyTo('OnlineCustomers@thameswater.co.uk');
           
            // Specify the name used as the display name.
            mail.setSenderDisplayName('OnlineCustomers@thameswater.co.uk');
           
            // Specify the subject line for your email address.
            mail.setSubject('Case Customer reference :'+CustRef+ ': Site Address :'+ SiteDetail + ': Query Number :'+CaseNumber);
           
            // Set to True if you want to BCC yourself on the email.
            mail.setBccSender(false);
           
            // Optionally append the salesforce.com email signature to the email.
            // The email address of the user executing the Apex Code will be used.
            mail.setUseSignature(false);
           
            // Specify the text content of the email.
            //mail.setPlainTextBody('Your Case: ' + case.Id +' has been created.');

          //  mail.setTargetObjectId('003P000000ZdOsB');
            mail.setWhatId(CaseId);
          //  mail.setTemplateId('00XP0000000I3cA');
           
           
            mail.setHtmlBody('<table><tr><td><img align="right" src="https://c.cs4.content.force.com/servlet/servlet.ImageServer?'+
            'id=015P00000000x8T&oid=00DP00000006dHM"/></td></tr><tr><td><br/><p><span style="font-family: Calibri;font-size=12pt;">'+
            '<font color="#333333">Dear Sir/Madam,</font></span></p>'+
            '<p><span style="font-family: Calibri;font-size=12pt;"><font color="#333333">Thank you for your enquiry on the above '+
            'address.</font></span></p><p><span style="font-family: Calibri;font-size=12pt;"><font color="#333333">'+
            'We would like to confirm that this query is currently being investigated and we will respond within 5 working days. '+
            '</font></span></p><p><span style="font-family: Calibri;font-size=12pt;"><font color="#333333">In the meantime, if you need anything further please contact '+
            'us on 0845 070 9148 and our team will be happy to help.</font></span></p></td></tr><tr><td><br/><br/><br/><br/></td></tr>'+
            '<tr><td><img src="https://c.cs4.content.force.com/servlet/servlet.ImageServer?id=015P00000000qUj&oid=00DP00000006dHM"/></td></tr></table>');

     //            'To view your case <a href=https://na1.salesforce.com/'+case.Id+'>click here.</a>');
    
       
            // Send the email you have created.
            if(!Test.IsRunningTest())
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

       
        }
       
     public void sendFinalEmailMessage(String CaseId,String ConId,String internalComments, String EmailOfComplaint,String SiteDetail,String CaseNumber,String CustRef){
           
            if(internalComments==null){
            internalComments='';
            }
            Messaging.reserveSingleEmailCapacity(2);

            // Processes and actions involved in the Apex transaction occur next,
            // which conclude with sending a single email.
           
            // Now create a new single email message object
            // that will send out a single email to the addresses in the To, CC & BCC list.
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
           
            // Strings to hold the email addresses to which you are sending the email.
            String[] toAddresses = new String[] {EmailOfComplaint};
       //     String[] ccAddresses = new String[] {'koushik.saha@wipro.com'};
            String[] BccAddresses = new String[] {'Opsinvestigationteam@thameswater.co.uk'}; 
           
            // Assign the addresses for the To and CC lists to the mail object.
            mail.setToAddresses(toAddresses);
         //   mail.setCcAddresses(ccAddresses);
            mail.setBccAddresses(BccAddresses);
           
            // Specify the address used when the recipients reply to the email.
            mail.setReplyTo('OnlineCustomers@thameswater.co.uk');
           
            // Specify the name used as the display name.
            mail.setSenderDisplayName('OnlineCustomers@thameswater.co.uk');
           
            // Specify the subject line for your email address.
            mail.setSubject('Case Customer reference :'+CustRef+ ': Site Address :'+ SiteDetail + ': Query Number :'+CaseNumber);
           
            // Set to True if you want to BCC yourself on the email.
            mail.setBccSender(false);
           
            // Optionally append the salesforce.com email signature to the email.
            // The email address of the user executing the Apex Code will be used.
            mail.setUseSignature(false);
           
            // Specify the text content of the email.
            //mail.setPlainTextBody('Your Case: ' + case.Id +' has been created.');

       //     mail.setTargetObjectId('003P000000ZdOsB');
            mail.setWhatId(CaseId);
       //     mail.setTemplateId('00XP0000000I3cA');
           
           
       
mail.setHtmlBody('<table><tr><td><img align="right" src="https://c.cs4.content.force.com/servlet/servlet.ImageServer?'+
            'id=015P00000000x8T&oid=00DP00000006dHM"/></td></tr><tr><td><br/><p><span style="font-family: Calibri;font-size=12pt;">'+
            '<font color="#333333">Dear Sir/Madam,</font></span></p>'+
            '<p><span style="font-family: Calibri;font-size=12pt;"><font color="#333333">Further to your enquiry on the above address.'+
            '</font></span></p><p><span style="font-family: Calibri;font-size=12pt;"><font color="#333333">'+
            'We\'ve completed our investigations and can provide the following response: '+
            '</font></span></p><p><span style="font-family: Calibri;font-size=12pt;"><font color="#333333">'+internalComments+'</font>'+
            '</font></span></p><p><span style="font-family: Calibri;font-size=12pt;"><font color="#333333">If you have any further questions please contact us on 0845 070 9148.</font></span></p></td></tr><tr><td><br/><br/><br/><br/></td></tr>'+
            '<tr><td><img src="https://c.cs4.content.force.com/servlet/servlet.ImageServer?id=015P00000000qUj&oid=00DP00000006dHM"/></td></tr></table>');


          
            // Send the email you have created.
            if(!Test.IsRunningTest())
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

       
        }
}
Vishant ShahVishant Shah
HI Ram,

In your trigger where you have the if condition do you not need have a else part rather than another if. Currently if will go in both if sections of the conditions pass for both scenarios

Ta
Vish
Vishant ShahVishant Shah
meant a else if, in case the first condition doesnot pass, and will mean only one codition is evaluated.