+ Start a Discussion
Frank JordanFrank Jordan 

Trigger to send email on "else" statement

I have a trigger below and I have some code in the first if else statement that sends out an email but i am running into issues. Can anyone help?

trigger BMIProductUpdate on Product2 (before insert) {
// Purpose: to update fields on products created by BMI based on BMI Product Group Code and Category 
    
    set<string>groupcodeset=new set<string>{};
      set<string>categoryset=new set<string>{};
          map<string, BMI_Product_Custom__c>customsettingmap=new map<string, BMI_Product_Custom__c>();
    
        for(Product2 P: trigger.new)
    {
        
        if(P.BigMachines_Product_Group_Code__c!='' && P.BigMachines_product_Category__c!= ''){
            
            groupcodeset.add(P.BigMachines_Product_Group_Code__c);
            categoryset.add(P.BigMachines_product_Category__c);
        }  
       
    else{
            Messaging.SingleEmailMessage mail = 
             new Messaging.SingleEmailMessage();
    
     List<String> sendTo = new List<String>();
      sendTo.add('frank.jordan@mckesson.com');
      mail.setToAddresses(sendTo);
     
    mail.setReplyTo('frank.jordan@mckesson.com');
      mail.setSenderDisplayName('Frank Jordan');
    
    mail.setSubject('BMI Product with NO Match');
      String body = 'This'  ;
      body += 'Does not have a matching BMI Product Code or Category';
      
      mail.setHtmlBody(body)
          mails.add(mail);
        Messaging.sendEmail(mails);

    
    }
    
    
    
    
    
    
    }
      system.debug('setvalues'+groupcodeset+'category'+categoryset);  
      list<BMI_Product_Custom__c>BMICustomSetting=[Select Asset_Type__c, BigMachines_Product_Category__c, 
                                                   BigMachines_Product_Group_Code__c, Name, Product_Line__c, Id, 
                                                   Revenue_Category__c, SFDC_Executive_Reporting_Group__c, 
                                                   SFDC_Product_Category__c, SFDC_Product_Family__c, 
                                                   SFDC_Product_Sub_category__c, Solution_Group__c, Sub_BU__c, 
                                                   Solution_Set__c, SystemModstamp, Third_party__c 
                                                   FROM BMI_Product_Custom__c where BigMachines_Product_Group_Code__c
                                                   in : groupcodeset and BigMachines_Product_Category__c in :categoryset]; 
system.debug('customlist'+BMICustomSetting);
          for(BMI_Product_Custom__c pc: BMICustomSetting){
              customsettingmap.put(pc.BigMachines_Product_Category__c+pc.BigMachines_Product_Group_Code__c, pc);
         }
  system.debug('customsettingmap'+customsettingmap);  
    for(integer i=0; i<trigger.new.size();i++){
        BMI_Product_Custom__c pc=customsettingmap.get(trigger.new[i].BigMachines_Product_Category__c+trigger.new[i].BigMachines_Product_Group_Code__c );
        if(pc != null){
            trigger.new[i].Product_Line_values__c=pc.Product_Line__c;
            trigger.new[i].Asset_Type__c=pc.Asset_Type__c;
            trigger.new[i].Revenue_Category__c=pc.Revenue_Category__c;
            trigger.new[i].Exec_Rptg_Product_Group__c=pc.SFDC_Executive_Reporting_Group__c;
            trigger.new[i].Product_Category__c=pc.SFDC_Product_Category__c;
            trigger.new[i].Family=pc.SFDC_Product_Family__c;
            trigger.new[i].Product_Sub_Category__c=pc.SFDC_Product_Sub_category__c;
            trigger.new[i].Solution_Group__c=pc.Solution_Group__c;
            trigger.new[i].Solution_Set__c=pc.Solution_Set__c;
            trigger.new[i].Sub_BU__c=pc.Sub_BU__c;
            trigger.new[i].X3rd_Party__c=pc.Third_party__c;
            
            
        }
        //else(send an email to coe if combo was not found)
    }
}
Best Answer chosen by Frank Jordan
pconpcon
Inside your if you'll want to build up a List of Messaging.SingleEmailMessage.  Then after the for loop you'll want to call Messaging.sendEmail  I've also taken the liberty of updating your code to make it more Apex friendly.

NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.
 
trigger BMIProductUpdate on Product2 (before insert) {
    String toAddress = 'frank.jordan@mckesson.com';
    String displayName = 'Frank Jordan';
        
    Set<String> groupCodeSet = new Set<String>();
    Set<String> categorySet = new Set<String>();
    Map<String, BMI_Product_Custom__c> customSettingMap = new Map<String, BMI_Product_Custom__c>();
    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();

    for (Product2 p: trigger.new) {
        if (
            p.BigMachines_Product_Group_Code__c != '' &&
            P.BigMachines_product_Category__c != ''
        ){  
            groupCodeSet.add(p.BigMachines_Product_Group_Code__c);
            categorySet.add(p.BigMachines_product_Category__c);
        } else {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

            mail.setToAddresses(new List<String>{
                toAddress
            });

            mail.setReplyTo(toAddress);
            mail.setSenderDisplayName(displayName);
            mail.setSubject('BMI Product with NO Match');
            mail.setHtmlBody('This does not have a matching BMI Product Code or Category');
            mails.add(mail);
        }
    }

    for (BMI_Product_Custom__c setting: [
        select Asset_Type__c,
            BigMachines_Product_Category__c,
            BigMachines_Product_Group_Code__c,
            Name,
            Product_Line__c,
            Revenue_Category__c,
            SFDC_Executive_Reporting_Group__c,
            SFDC_Product_Category__c,
            SFDC_Product_Family__c,
            SFDC_Product_Sub_category__c,
            Solution_Group__c,
            Sub_BU__c,
            Solution_Set__c,
            SystemModstamp,
            Third_party__c
        from BMI_Product_Custom__c
        where BigMachines_Product_Group_Code__c in :groupcodeset and
            BigMachines_Product_Category__c in :categoryset
    ]) {
        String key = setting.BigMachines_Product_Category__c + setting.BigMachines_Product_Group_Code__c;
        customSettingMap.put(key, setting);
    }

    for (Product2 product : Trigger.new) {
        String key = product.BigMachines_Product_Category__c + product.BigMachines_Product_Group_Code__c;

        if (customSettingMap.containsKey(key)) {
            BMI_Product_Custom__c setting = customSettingMap.get(key);

            product.Product_Line_values__c = setting.Product_Line__c;
            product.Asset_Type__c = setting.Asset_Type__c;
            product.Revenue_Category__c = setting.Revenue_Category__c;
            product.Exec_Rptg_Product_Group__c = setting.SFDC_Executive_Reporting_Group__c;
            product.Product_Category__c = setting.SFDC_Product_Category__c;
            product.Family = setting.SFDC_Product_Family__c;
            product.Product_Sub_Category__c = setting.SFDC_Product_Sub_category__c;
            product.Solution_Group__c = setting.Solution_Group__c;
            product.Solution_Set__c = setting.Solution_Set__c;
            product.Sub_BU__c = setting.Sub_BU__c;
            product.X3rd_Party__c = setting.Third_party__c;
        } else {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

            mail.setToAddresses(new List<String>{
                toAddress
            });

            mail.setReplyTo(toAddress);
            mail.setSenderDisplayName(displayName);
            mail.setSubject('BMI Product with NO Match');
            mail.setHtmlBody('This does not have a matching BMI Product Code or Category');
            mails.add(mail);
        }

    }

    if (!mails.isEmpty()) {
        Messaging.sendEmail(mails);
    }
}
NOTE: This code has not been tested and may contain typographical or logical errors