0

I have a trigger that updates fields on the product record depending on a certain comination of two fields. I have two if else statements below. For the else statements, i would like an email to send with the Product Code. I am having trouble coming up with the else statements. Could soomeone help? The code is below:

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('[email protected]');
  mail.setToAddresses(sendTo);

mail.setReplyTo('[email protected]');
  mail.setSenderDisplayName('Frank Jordan');

mail.setSubject('BMI Product with NO Match');
  String body = 'This' productcode  ;
  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)
}

}

1 Answer 1

1

What you want to do in the else statement is create a collection of records that didn't meet the criteria of the if statement. Later, you can test to see if the collection isEmpty(). If it's not, then you can create your emails as part of your trigger, or better yet, send a list of the records to another class that will send them for you.

Your statement should look something like this:

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>{};
set<string>Prod2String4Email = 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
       {

          //add Ids & any fields you want to email set of Ids for email

          string m = 'Product2Id = '+ P.Id +', BigMachines_Product_Group_Code__c = '+ 
            P.BigMachines_Product_Group_Code__c +', BigMachines_product_Category__c = '
            + P.BigMachines_product_Category__c;

          Prod2String4Email.add(m);

       }

    .....

Later, at the end of your trigger, do a test like below

If(!Prod2String4Email.isEmpty()) MyCustomEmailHandler(Prod2String4Email);

If there's anything in your set of strings, it passes it to your email handler for processing. If not, it just continues on to the end of your trigger unless you want to process them through your trigger which you really don't want to do.

You can do the same kind of thing with your 2nd else statement including what's relevant that you want to include in your email message by assembling it in a string.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .