I have this trigger on an Account child object (Group_Name__c) and it's supposed to email a csv file with the selected records whenever the trigger fires. After it's all said and done I need to update the Priced__c field to true to prevent the same records from being queried again when the trigger fires - and that's why I'm creating a 'new' instance for the Group_Name__c for the update operation. The boolean property is an attempt to avoid recursivness inherent in after update triggers. I'm cognizant of the precariousness of having all your logic in a trigger but I'm trying to get it working first so please bear with.
Problem: trigger not firing and so no email being generated.
trigger PrepAccountsReadyForPricing on Group_Name__c (after insert, after update){
List<Account> actList = new List<Account>();
List<Group_Name__c> grpList = new List<Group_Name__c>();
public static boolean isExecuting = false;
public static void updateValues(){
if(isExecuting){
return;
}
isExecuting = true;
for(Group_Name__c g: Trigger.new){
if(g.Complete_Account__c == True && g.Group_ID__c != Null && g.Priced__c == false){
actList.add(new Account(Id = g.Client_Account__c));
grpList.add(new Group_Name__c(Id = g.Id, Priced__c = true));
}
}
if(actList != null && !actList.isEmpty()){
String headerString = 'SF_ACCT_ID,CarrierID,AccountID,GroupID,Group Name,Eff_Date,Account Name,\n';
for(Account a: actList){
String finalString = '';
String recordString = '';
for(Group_Name__c g : a.Group_Plans__r){
String myDate = g.Group_Effective_Date__c.format();
recordString = a.Id + ',' + a.Carrier_s_ID__c + ',' + a.Account_ID__c + ',' + g.Group_ID__c + ',' + g.Name + ',' + myDate + ',' + '"'+ a.Name + '"'+ '\n';
finalString = finalString + recordString;
}
finalString = headerString + finalString;
Date myDate = Date.Today();
String sDate = String.valueOf(myDate).replaceAll('-', '');
Messaging.EmailFileAttachment csvAtt = new Messaging.EmailFileAttachment();
blob csvBlob = Blob.valueOf(finalString);
String csvName = a.Id + '_' + sDate + '.csv';
csvAtt.setFileName(csvName);
csvAtt.setBody(csvBlob);
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String[] toAddresses = new List<String>{'[email protected]'};
String subject = 'group ready for pricing';
email.setSubject(subject);
email.setToAddresses(toAddresses);
email.setSaveAsActivity(False);
email.setPlainTextBody('pricing');
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAtt});
Messaging.SendEmailResult[] result = Messaging.SendEmail(new Messaging.SingleEmailMessage[]{email});
}
}
update grpList;
}
}