Salesforce Asked on November 11, 2021
I am trying to understand what may be going on.
My code basically sends emails through a batch. However, in the executions it is giving the error "List index out of bounds: 0".
I can’t find the problem. I am checking the size of all the lists before using.
Log Error:
12:43:37:784 FATAL_ERROR System.ListException: List index out of bounds: 0
global class PesquisaNPSSemanalBatch implements Database.Batchable<sObject>, Database.Stateful {
final String NOME_MODELO_EMAIL = 'NPSSemestral';
OrgWideEmailAddress owa = [SELECT Id, Address, DisplayName FROM OrgWideEmailAddress WHERE DisplayName = 'PESQUISA' LIMIT 1];
EmailTemplate modeloEmail = [select id, name from EmailTemplate where name =: NOME_MODELO_EMAIL limit 1];
global Database.QueryLocator start(Database.BatchableContext bc) {
String query = 'SELECT Id, Type, ContactId, CaseNumber, ContactEmail,DataEnvioPesquisa__c,EnviaEmailContato__c, RecordTypeId' +
' FROM Case WHERE ClosedDate = LAST_N_DAYS:7 AND EnviaEmailContato__c = true AND ContactId != null AND ContactEmail != null' +
' AND Status = 'Closed'';
System.debug('Database.getQueryLocator(query)' + Database.getQueryLocator(query));
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<Case> scope){
String REC_BULK = RecordTypeMemory.getRecType('Case', 'ContingenciaBulk');
String REC_ASS_TC = RecordTypeMemory.getRecType('Case', 'AssistenciaTecnicaCorretiva');
String REC_PGP = RecordTypeMemory.getRecType('Case', 'FaturamentoPGPRota');
String REC_MP = RecordTypeMemory.getRecType('Case', 'SolicitacaoMaterialProdutivo');
String REC_MNP = RecordTypeMemory.getRecType('Case', 'SolicitacaoMaterialNaoProdutivo');
String REC_PUB = RecordTypeMemory.getRecType('Case', 'PickUpBulk');
String REC_FB = RecordTypeMemory.getRecType('Case', 'FaturamentoBulk');
String REC_TNB = RecordTypeMemory.getRecType('Case', 'TrocaNotaBulk');
String REC_CPGP = RecordTypeMemory.getRecType('Case', 'ContingenciaPGP');
String REC_FPGPRota = RecordTypeMemory.getRecType('Case', 'FaturamentoPGPRota');
String REC_SPGP = RecordTypeMemory.getRecType('Case', 'SuprimentosPGP');
String REC_BALCAO = RecordTypeMemory.getRecType('Case', 'Balcao');
String REC_AN = RecordTypeMemory.getRecType('Case', 'AtualizacaoDeNivel');
String REC_FATU = RecordTypeMemory.getRecType('Case', 'Faturamento');
String REC_DEVBULK = RecordTypeMemory.getRecType('Case', 'DevolucoesBulk');
System.debug('scope' + scope);
List<Case> lstCasos = new List<Case>();
//Variavel para armazenar se o mês de envio da pesquisa é diferente deste mes
Boolean enviarEsteMes;
for(Case cs : scope) {
enviarEsteMes = cs.DataEnvioPesquisa__c != null && cs.DataEnvioPesquisa__c.month() != Date.today().month();
if((cs.DataEnvioPesquisa__c == null || enviarEsteMes) && cs.EnviaEmailContato__c){
if(cs.RecordTypeId != REC_BULK &&
cs.RecordTypeId != REC_ASS_TC &&
cs.RecordTypeId != REC_PGP &&
cs.RecordTypeId != REC_MP &&
cs.RecordTypeId != REC_MNP &&
cs.RecordTypeId != REC_PUB &&
cs.RecordTypeId != REC_FB &&
cs.RecordTypeId != REC_TNB &&
cs.RecordTypeId != REC_CPGP &&
cs.RecordTypeId != REC_FPGPRota &&
cs.RecordTypeId != REC_SPGP &&
cs.RecordTypeId != REC_BALCAO &&
cs.RecordTypeId != REC_AN &&
cs.RecordTypeId != REC_FATU &&
cs.RecordTypeId != REC_DEVBULK &&
cs.Type != 'Contato Interno' && cs.Type != 'Lead' && cs.Type != 'SPAM'){
lstCasos.add(cs);
}
}
}
if(lstCasos.size() > 0){
sendEmailNotification(lstCasos);
}
}
global void finish(Database.BatchableContext bc){}
public void sendEmailNotification(List<Case> lstCasos){
System.debug('sendEmailNotification');
System.debug('lstCasos ---> ' + lstCasos.size());
List < Messaging.SingleEmailMessage > emails = new List < Messaging.SingleEmailMessage > ();
List<Case> casesToUpd = new List<Case>();
if(modeloEmail != null && modeloEmail.id != null){
System.debug('modeloEmail.id' + modeloEmail.id);
for(Case c : lstCasos){
if(c.ContactId != null){
System.debug('c.ContactId' + c.ContactId );
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setOrgWideEmailAddressId(owa.id);
email.setTemplateId(modeloEmail.id);
email.setTargetObjectId(c.ContactId);
email.setWhatId(c.id);
email.setSaveAsActivity(true);
emails.add(email);
casesToUpd.add(c);
}
}
}
// Messaging.sendEmail(emails);
// Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {email};
Messaging.SendEmailResult[] results = Messaging.sendEmail(emails, false);
System.debug('results.size()' + results.size());
if(results.size() > 0){
for(Messaging.SendEmailResult r : results){
if (r.success) {
System.debug('The email was sent successfully.');
for(Case cs : casesToUpd){
cs.DataEnvioPesquisa__c = System.now();
}
BatchLogs__c b = new BatchLogs__c();
b.Status__c = 'Êxito';
b.Name = 'NPS Semanal';
insert b;
System.debug('insert b');
} else {
System.debug('The email failed to send: ' + results[0].errors[0].message);
BatchLogs__c b = new BatchLogs__c();
b.MensagemErroCompleta__c = results[0].errors[0].message;
b.Status__c = 'Falha';
b.Name = 'NPS Semanal';
insert b;
System.debug('insert b');
System.debug('The email failed to send: ' + results[0].errors[0].message);
//l.DescricaoErroNotificacaoLead__c = results[0].errors[0].message;
}
}
update casesToUpd;
}
}
}
You're accessing the first Messaging.SendEmailResult in the list, but that may not be the one that has the error, so errors[0] throws this exception. Use the current iteration instead:
//System.debug('The email failed to send: ' + results[0].errors[0].message);
System.debug('The email failed to send: ' + r.errors[0].message);
Answered by sfdcfox on November 11, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP