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
Dilip Kulkarni 6Dilip Kulkarni 6 

mail issue in trigger

Hi Experts,

I have one problem in the trigger. Here two mail notifications are going instead of one when case is opened. The trigger is given below. Only one notification should go. Please help.


         
   Apex Coding to send email to the Account insidesales rep based on case status.
           
    1.If Case Status is Open then Send email notification to the account insidesalesrep  .
   
 
    
Trigger Case_AIU_Rep_Notification  on Case (after insert,after update) {
    Set<Id> accIds = new Set<Id>();
    Set<Id> contIds=new Set<Id>();
    
    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
    if(checkRecursive_Case_AIU_Rep_Notification.runOnce())
    {
    for (Case c: trigger.new) {
     If(c.Status == 'Open'){
        accIds.add(c.AccountId);
         contIds.add(c.ContactId);
        }
    }
    Map<Id, Account> accMap = new Map<Id, Account>([SELECT Id,name,Customer_ID__c, thermage_tlr__Rep_2__r.Email,thermage_tlr__Consumable_Rep__r.name,thermage_tlr__Rep_3__r.name,thermage_tlr__Rep_1__r.name,thermage_tlr__Capital_Rep__r.name FROM Account WHERE Id In :accIds]);
    Map<Id,Contact> contMap=new Map<Id,Contact>([select id,lastname,firstname from contact where id in:contids]);
    //System.debug('accMap '+accMap );
    for (Case c : trigger.new) {
    
        string body='<h3>*** CASE OPENED NOTIFICATION ***</h3>'+' <br/>';
        body+='The following case has been opened.'+' <br/>';
        body+='Company:'+accMap.get(c.accountid).name +' <br/>';      
        body+='Customer id:'+accMap.get(c.Accountid).Customer_ID__c+' <br/>';
        body+='Contact Name: '+contMap.get(c.contactId).lastname+' <br/>';
        body+='Case #: '+c.CaseNumber+' <br/>'; 
        body+='Subject #: '+c.Subject+' <br/>'; 
        body+='Description #: '+c.Description +'<br/><br/>'; 
        body+= 'Click on the link to access the case:<a href=https://cs51.salesforce.com/>'+c.CaseNumber+'</a>'+'<br/><br/>';
        body+='Account Manager:'+accMap.get(c.AccountId).thermage_tlr__Consumable_Rep__r.name +'<br/>'; 
        body+='Capital Specialist:'+accMap.get(c.AccountId).thermage_tlr__Capital_Rep__r.name+'<br/>'; 
        body+='Surgical Specialist:'+accMap.get(c.AccountId).thermage_tlr__Rep_3__r.name+'<br/>'; 
        body+='Clinical Specialist:'+ accMap.get(c.AccountId).thermage_tlr__Rep_1__r.name+'<br/>';
      
        String Subject='Open Case # '+c.CaseNumber+','+ accMap.get(c.Accountid).Name+' | Account Manager :'+accMap.get(c.Accountid).thermage_tlr__Consumable_Rep__c;
        Account relatedCaseaccount = accMap.get(c.AccountId);
       
        Messaging.SingleEmailMessage CaseNotificationmail = new Messaging.SingleEmailMessage();  
        CaseNotificationmail.setToAddresses(new List<String> {accMap.get(c.AccountId).thermage_tlr__Rep_2__r.Email});
       
        CaseNotificationmail.setSubject(Subject);
       
        CaseNotificationmail.setHtmlBody(body);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {CaseNotificationmail});

       mails.add(CaseNotificationmail); 
    
    }
    Messaging.sendEmail(mails);
   }
}

 
Best Answer chosen by Dilip Kulkarni 6
gyani19901.3956550919266765E12gyani19901.3956550919266765E12
Hi Dilip,

Comment or delete the below line in the code
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {CaseNotificationmail});

Please let me know if you need any other help.

Regards,
Gyanender Singh

All Answers

Dilip_VDilip_V
Hi Dilip,

You can compare old and new values for status field in trigger to avoid the issue.
Trigger Case_AIU_Rep_Notification  on Case (after insert,after update) {
    Set<Id> accIds = new Set<Id>();
    Set<Id> contIds=new Set<Id>();
    
    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
    if(checkRecursive_Case_AIU_Rep_Notification.runOnce())
    {
    for (Case c: trigger.new) {
     If(trigger.oldMap.get(C.Id).status != c.Status && c.Status == 'Open'){
        accIds.add(c.AccountId);
         contIds.add(c.ContactId);
        }
    }
    Map<Id, Account> accMap = new Map<Id, Account>([SELECT Id,name,Customer_ID__c, thermage_tlr__Rep_2__r.Email,thermage_tlr__Consumable_Rep__r.name,thermage_tlr__Rep_3__r.name,thermage_tlr__Rep_1__r.name,thermage_tlr__Capital_Rep__r.name FROM Account WHERE Id In :accIds]);
    Map<Id,Contact> contMap=new Map<Id,Contact>([select id,lastname,firstname from contact where id in:contids]);
    //System.debug('accMap '+accMap );
    for (Case c : trigger.new) {
    
        string body='<h3>*** CASE OPENED NOTIFICATION ***</h3>'+' <br/>';
        body+='The following case has been opened.'+' <br/>';
        body+='Company:'+accMap.get(c.accountid).name +' <br/>';      
        body+='Customer id:'+accMap.get(c.Accountid).Customer_ID__c+' <br/>';
        body+='Contact Name: '+contMap.get(c.contactId).lastname+' <br/>';
        body+='Case #: '+c.CaseNumber+' <br/>'; 
        body+='Subject #: '+c.Subject+' <br/>'; 
        body+='Description #: '+c.Description +'<br/><br/>'; 
        body+= 'Click on the link to access the case:<a href=https://cs51.salesforce.com/>'+c.CaseNumber+'</a>'+'<br/><br/>';
        body+='Account Manager:'+accMap.get(c.AccountId).thermage_tlr__Consumable_Rep__r.name +'<br/>'; 
        body+='Capital Specialist:'+accMap.get(c.AccountId).thermage_tlr__Capital_Rep__r.name+'<br/>'; 
        body+='Surgical Specialist:'+accMap.get(c.AccountId).thermage_tlr__Rep_3__r.name+'<br/>'; 
        body+='Clinical Specialist:'+ accMap.get(c.AccountId).thermage_tlr__Rep_1__r.name+'<br/>';
      
        String Subject='Open Case # '+c.CaseNumber+','+ accMap.get(c.Accountid).Name+' | Account Manager :'+accMap.get(c.Accountid).thermage_tlr__Consumable_Rep__c;
        Account relatedCaseaccount = accMap.get(c.AccountId);
       
        Messaging.SingleEmailMessage CaseNotificationmail = new Messaging.SingleEmailMessage();  
        CaseNotificationmail.setToAddresses(new List<String> {accMap.get(c.AccountId).thermage_tlr__Rep_2__r.Email});
       
        CaseNotificationmail.setSubject(Subject);
       
        CaseNotificationmail.setHtmlBody(body);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {CaseNotificationmail});

       mails.add(CaseNotificationmail); 
    
    }
    Messaging.sendEmail(mails);
   }
}

Let us know if it helps.

Mark it as best answer if it works.

Thanks.
Dilip Kulkarni 6Dilip Kulkarni 6
Hi Dilip,
I think it's already compared in trigger. Any other solution to do it?
gyani19901.3956550919266765E12gyani19901.3956550919266765E12
Hi Dilip,

Comment or delete the below line in the code
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {CaseNotificationmail});

Please let me know if you need any other help.

Regards,
Gyanender Singh
This was selected as the best answer
Dilip Kulkarni 6Dilip Kulkarni 6
Hi Gyanender,

It worked. Thanks a lot.