I have a Schedulable class that I execute every day. I get some accounts that accomplish some conditions and then I create a email for everyone of this records into a List, but I'm getting this error.
public class ALI_AvisoPlazoExcesivoEnExpediente implements Schedulable {
public void execute (SchedulableContext sc){
metodo();
}
public void metodo(){
EmailTemplate templateId1 = [select id, HtmlValue from EmailTemplate where developername = : 'Plazo_Expediente_Excesivo_OMFEF'];
EmailTemplate templateId2 = [select id, HtmlValue from EmailTemplate where developername = : 'Plazo_Expediente_Excesivo_OPF'];
List<Account> cuentas = [SELECT Id, Name, desde_fecha__c, desde_fecha_Estado__c, Departamento_pendiente_finalizacion__c,
Plazo_Estimado_Expediente__c, Plazo_Estimado_Expediente_ESTADO__c, Aviso_Enviado_desde_fecha__c,
Aviso_Enviado_Plazo_Estimado_Exp__c, Responsable_Area_Tecnica__c, Delegado_Tecnico_Territorial__c,
Responsable_Area_Tecnica__r.Email, Responsable_Area_Tecnica__r.Name, Delegado_Tecnico_Territorial__r.Email, Delegado_Tecnico_Territorial__r.Name
FROM Account
WHERE (Plazo_Estimado_Expediente__c > 12 OR desde_fecha_Estado__c like '%flag_red.gif%'
OR Plazo_Estimado_Expediente_ESTADO__c like '%flag_red.gif%')
AND Responsable_Area_Tecnica__c != null AND Delegado_Tecnico_Territorial__c != null
AND (Aviso_Enviado_desde_fecha__c = false OR Aviso_Enviado_Plazo_Estimado_Exp__c = false)];
List<Account> accountsToUpdate = new List<Account>();
for (Account a : cuentas) {
// Prepare recipients
Map<Id, String> recipients = new Map<Id, String>();
recipients.put(a.Responsable_Area_Tecnica__c, a.Responsable_Area_Tecnica__r.Name);
recipients.put(a.Delegado_Tecnico_Territorial__c, a.Delegado_Tecnico_Territorial__r.Name);
if (a.Plazo_Estimado_Expediente__c > 12 && a.Aviso_Enviado_Plazo_Estimado_Exp__c == false) {
for (Id key: recipients.keySet()) {
String name = recipients.get(key);
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
email.setTemplateId(templateId1.Id);
String baseUrl = System.URL.getSalesforceBaseUrl().toExternalForm() + '/';
String subject = '[HEAT] - Plazo estimado superior a 12 meses en el expediente ' + a.Name;
String fixed_template = templateId1.HtmlValue.replace('{!Account.Name}', '<a href="'+baseUrl+a.Id+'">'+a.Name+'</a>').
replaceAll('<!\\[CDATA\\[','').replaceAll('\\]\\]>','').replace('NOMBRE_PERSONA', name);
email.setSubject(subject);
email.setHtmlBody(fixed_template);
email.setSaveAsActivity(false);
email.setTargetObjectId(key);
email.setSenderDisplayName('Soporte Aliseda HEAT');
allmsg.add(email);
}
a.Aviso_Enviado_Plazo_Estimado_Exp__c = true;
}
if (a.desde_fecha_Estado__c == '<img src="/img/samples/flag_red.gif" alt="Estado: desde fecha" border="0"/>' && a.Aviso_Enviado_desde_fecha__c == false) {
for (Id key: recipients.keySet()) {
String name = recipients.get(key);
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
email.setTemplateId(templateId2.Id);
String baseUrl = System.URL.getSalesforceBaseUrl().toExternalForm() + '/';
String subject = '[HEAT] - En el expediente: ' + a.Name + ' se ha superado el tiempo permitido en el Departamento Pendiente de finalización';
String fixed_template = templateId2.HtmlValue.replace('{!Account.Name}', '<a href="'+baseUrl+a.Id+'">'+a.Name+'</a>').
replaceAll('<!\\[CDATA\\[','').replaceAll('\\]\\]>','').replace('NOMBRE_PERSONA', name).
replace('DEPARTAMENTO_PENDIENTE_DE_FINALIZACION', a.Departamento_pendiente_finalizacion__c);
email.setSubject(subject);
email.setHtmlBody(fixed_template);
email.setSaveAsActivity(false);
email.setTargetObjectId(key);
email.setSenderDisplayName('Soporte Aliseda HEAT');
allmsg.add(email);
}
a.Aviso_Enviado_desde_fecha__c = true;
}
if (a.Aviso_Enviado_Plazo_Estimado_Exp__c == true || a.Aviso_Enviado_desde_fecha__c == true) {
accountsToUpdate.add(a);
}
}
//Calling sendEmail method only once Messaging.sendEmail(allmsg,false); update accountsToUpdate; } }