• Dilip Kulkarni 6
  • NEWBIE
  • 20 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
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);
   }
}

 
Hi Experts,
I need test class for the trigger given below:

trigger Casetrigger on Case (after insert) {

 list<Account> accList = new list<Account>();

 for(case c: Trigger.new){
      if(c.account!= null)
       accList.add(c.account);
     }
     
    //query email address from account object.
     
   Map<id,String> insideSalesEmailMap = new Map<Id, String>();
     for(Account acc: [select id,thermage_tlr__Rep_2__r.email from Account where id IN :accList]){
            insideSalesEmailMap.put(acc.id, acc.thermage_tlr__Rep_2__r.email);
        }  
        
        // preparing for Email send out. 
        
         List<Messaging.SingleEmailMessage> sme = new List<Messaging.SingleEmailMessage>();
     for(case c: Trigger.new){
      Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
      //email.setTemplateId('00X500000013avG');
      email.setToAddresses(new List<String>{insideSalesEmailMap.get(c.accountId)});
      email.setPlaintextbody('Test');
      email.setSaveAsActivity(false);
      sme.add(email);
     }
     
      //send Email
      
     if(sme != null && sme.size() > 0){
      Messaging.sendEmail(sme);
  }
     
}

Please help.
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);
   }
}