You need to sign in to do that
Don't have an account?
Frank 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)
}
}
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)
}
}
NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.
NOTE: This code has not been tested and may contain typographical or logical errors