You need to sign in to do that
Don't have an account?
DaNae Graham
Apex Error: List index out of bounds: 0
I keep getting the following error when I test the code below:
System.ListException: List index out of bounds: 0
This error occurs on lines 13, 34, 64, and 93. It looks like the list is pulling no records and I cannot figure out why. I have confirmed that all required fields are captured. I referenced another article that said to use @isTest (seeAllData=true) and it is still not working.
Help would be greatly appreciated - thank you in advance!!
@isTest (seeAllData=true)
private class leadsTaskHandlerTest {
@isTest static void testLeadsWorkflow() {
Lead newLead = new Lead(
Company = 'Test Account', FirstName= 'Test', LastName= 'Lead',
LeadSource = 'Web', Country = 'Canada', Email = 'something@gmail.com',
Status = 'Open', Lead_Type__c = 'New Car Franchise');
insert newLead;
Task[] tasks = [SELECT Id, Subject FROM Task WHERE WhoId = :newLead.Id AND Subject = 'Evaluate and Assign Web Lead'];
tasks[0].Status = 'Completed';
update tasks[0];
System.assert(taskCreatedForLead('Email Introduction of TradeRev to main contact person', newLead.Id));
System.assert(taskCreatedForLead('Phone call to follow up from email of introduction', newLead.Id));
System.assert(taskCreatedForLead('Schedule an appointment with lead; no voicemail if you don\'t connect', newLead.Id));
System.assert(taskCreatedForLead('Attempt to contact lead to set appointment second time in the week', newLead.Id));
System.assert(taskCreatedForLead('Any interest in TradeRev?; let\'s schedule a demo', newLead.Id));
System.assert(taskCreatedForLead('Touch base to let them know about growth/updates of TradeRev', newLead.Id));
System.assert(taskCreatedForLead('Are they ready to talk about TradeRev?', newLead.Id));
}
@isTest static void testContractWorkflow() {
Lead newLead = new Lead(
Company = 'Test Account', FirstName= 'Test', LastName= 'Lead',
LeadSource = 'Web', Country = 'Canada',
Status = 'Contract Given', Lead_Type__c = 'New Car Franchise');
insert newLead;
Task[] tasks = [SELECT Id, Subject FROM Task WHERE WhoId = :newLead.Id AND Subject = 'Any questions on the Registration Forms?'];
tasks[0].Status = 'Completed';
update tasks[0];
System.assert(taskCreatedForLead('Are the Registration Forms complete?', newLead.Id));
}
@isTest static void testDueDateEmail() {
Lead newLead = new Lead(
Company = 'Test Account', FirstName= 'Test', LastName= 'Lead',
LeadSource = 'Web', Country = 'Canada', Email = 'something@gmail.com',
Status = 'Contract Given', Lead_Type__c = 'New Car Franchise');
insert newLead;
Task newTask = new Task( Priority = 'Normal',
Status = 'Not Started',
ActivityDate = Date.today(),
WhoId = newLead.Id);
insert newTask;
update newTask;
}
@isTest static void testFranchiseDealerWorkflow() {
Account newAccount = new Account(
Name = 'Test Franchise Dealer',
AccountSource = 'Web',
Account_Type__c = 'New Car Franchise');
insert newAccount;
Task[] tasks = [SELECT Id, Subject FROM Task WHERE WhatId = :newAccount.Id AND Subject = 'Set appointment for Installation and Training Franchise'];
tasks[0].Status = 'Completed';
update tasks[0];
System.assert(taskCreatedForAccount('Assist with On-boarding', newAccount.Id));
System.assert(taskCreatedForAccount('Launch Cars; Walk back lot and try identify more cars they can load', newAccount.Id));
System.assert(taskCreatedForAccount('Has buyer been in touch to make arrangements for payment/pickup?', newAccount.Id));
System.assert(taskCreatedForAccount('Has buyer taken car? Mark car delivered', newAccount.Id));
System.assert(taskCreatedForAccount('Use TradeRev for the Live Appraisals', newAccount.Id));
System.assert(taskCreatedForAccount('Any other cars to launch?', newAccount.Id));
System.assert(taskCreatedForAccount('Reminder to buy cars on TradeRev', newAccount.Id));
System.assert(taskCreatedForAccount('Feedback on TradeRev; any referrals?', newAccount.Id));
System.assert(taskCreatedForAccount('Touch Point; How are you doing? Can I help with anything? Good time to sell cars on TradeRev', newAccount.Id));
//System.assert(taskCreatedForAccount('Monthly Report; Tips For Better Listings', newAccount.Id));
//System.assert(taskCreatedForAccount('Touch Point; How are you doing? Can I help with anything? Good time to sell cars on TradeRev', newAccount.Id));
//System.assert(taskCreatedForAccount('Monthly Report; Tips For Better Listings', newAccount.Id));
}
@isTest static void testIndepedentDealerWorkflow() {
Account newAccount = new Account(
Name = 'Test Independent Dealer',
AccountSource = 'Web',
Account_Type__c = 'Independent Dealer');
insert newAccount;
Task[] tasks = [SELECT Id, Subject FROM Task WHERE WhatId = :newAccount.Id AND Subject = 'Set appointment for Installation and Training'];
tasks[0].Status = 'Completed';
update tasks[0];
System.assert(taskCreatedForAccount('Assist with On-boarding', newAccount.Id));
System.assert(taskCreatedForAccount('Any questions on bidding?', newAccount.Id));
System.assert(taskCreatedForAccount('Have they been bidding? Any cars in pending?', newAccount.Id));
System.assert(taskCreatedForAccount('Won any cars? Discuss timing expectations for payment and pickup', newAccount.Id));
System.assert(taskCreatedForAccount('If a car has been won; have they paid and picked up car yet?', newAccount.Id));
System.assert(taskCreatedForAccount('Feedback on TradeRev; any referrals?', newAccount.Id));
System.assert(taskCreatedForAccount('Touch Point; How are you doing? Can I help with anything? Having success moving the cars bought on TradeRev?', newAccount.Id));
System.assert(taskCreatedForAccount('Touch Point; How are you doing? Can I help with anything? Having success moving the cars bought on TradeRev?', newAccount.Id));
}
static boolean taskCreatedForLead(String subject, Id id) {
Task[] tasks = [SELECT Id, Subject FROM Task WHERE WhoId = :id AND Subject = :subject];
if (tasks.size() > 0) {
tasks[0].Status = 'Completed';
update tasks[0];
return true;
}
else {
return false;
}
}
static boolean taskCreatedForAccount(String subject, Id id) {
Task[] tasks = [SELECT Id, Subject FROM Task WHERE WhatId = :id AND Subject = :subject];
if (tasks.size() > 0) {
tasks[0].Status = 'Completed';
update tasks[0];
return true;
}
else {
return false;
}
}
}
System.ListException: List index out of bounds: 0
This error occurs on lines 13, 34, 64, and 93. It looks like the list is pulling no records and I cannot figure out why. I have confirmed that all required fields are captured. I referenced another article that said to use @isTest (seeAllData=true) and it is still not working.
Help would be greatly appreciated - thank you in advance!!
@isTest (seeAllData=true)
private class leadsTaskHandlerTest {
@isTest static void testLeadsWorkflow() {
Lead newLead = new Lead(
Company = 'Test Account', FirstName= 'Test', LastName= 'Lead',
LeadSource = 'Web', Country = 'Canada', Email = 'something@gmail.com',
Status = 'Open', Lead_Type__c = 'New Car Franchise');
insert newLead;
Task[] tasks = [SELECT Id, Subject FROM Task WHERE WhoId = :newLead.Id AND Subject = 'Evaluate and Assign Web Lead'];
tasks[0].Status = 'Completed';
update tasks[0];
System.assert(taskCreatedForLead('Email Introduction of TradeRev to main contact person', newLead.Id));
System.assert(taskCreatedForLead('Phone call to follow up from email of introduction', newLead.Id));
System.assert(taskCreatedForLead('Schedule an appointment with lead; no voicemail if you don\'t connect', newLead.Id));
System.assert(taskCreatedForLead('Attempt to contact lead to set appointment second time in the week', newLead.Id));
System.assert(taskCreatedForLead('Any interest in TradeRev?; let\'s schedule a demo', newLead.Id));
System.assert(taskCreatedForLead('Touch base to let them know about growth/updates of TradeRev', newLead.Id));
System.assert(taskCreatedForLead('Are they ready to talk about TradeRev?', newLead.Id));
}
@isTest static void testContractWorkflow() {
Lead newLead = new Lead(
Company = 'Test Account', FirstName= 'Test', LastName= 'Lead',
LeadSource = 'Web', Country = 'Canada',
Status = 'Contract Given', Lead_Type__c = 'New Car Franchise');
insert newLead;
Task[] tasks = [SELECT Id, Subject FROM Task WHERE WhoId = :newLead.Id AND Subject = 'Any questions on the Registration Forms?'];
tasks[0].Status = 'Completed';
update tasks[0];
System.assert(taskCreatedForLead('Are the Registration Forms complete?', newLead.Id));
}
@isTest static void testDueDateEmail() {
Lead newLead = new Lead(
Company = 'Test Account', FirstName= 'Test', LastName= 'Lead',
LeadSource = 'Web', Country = 'Canada', Email = 'something@gmail.com',
Status = 'Contract Given', Lead_Type__c = 'New Car Franchise');
insert newLead;
Task newTask = new Task( Priority = 'Normal',
Status = 'Not Started',
ActivityDate = Date.today(),
WhoId = newLead.Id);
insert newTask;
update newTask;
}
@isTest static void testFranchiseDealerWorkflow() {
Account newAccount = new Account(
Name = 'Test Franchise Dealer',
AccountSource = 'Web',
Account_Type__c = 'New Car Franchise');
insert newAccount;
Task[] tasks = [SELECT Id, Subject FROM Task WHERE WhatId = :newAccount.Id AND Subject = 'Set appointment for Installation and Training Franchise'];
tasks[0].Status = 'Completed';
update tasks[0];
System.assert(taskCreatedForAccount('Assist with On-boarding', newAccount.Id));
System.assert(taskCreatedForAccount('Launch Cars; Walk back lot and try identify more cars they can load', newAccount.Id));
System.assert(taskCreatedForAccount('Has buyer been in touch to make arrangements for payment/pickup?', newAccount.Id));
System.assert(taskCreatedForAccount('Has buyer taken car? Mark car delivered', newAccount.Id));
System.assert(taskCreatedForAccount('Use TradeRev for the Live Appraisals', newAccount.Id));
System.assert(taskCreatedForAccount('Any other cars to launch?', newAccount.Id));
System.assert(taskCreatedForAccount('Reminder to buy cars on TradeRev', newAccount.Id));
System.assert(taskCreatedForAccount('Feedback on TradeRev; any referrals?', newAccount.Id));
System.assert(taskCreatedForAccount('Touch Point; How are you doing? Can I help with anything? Good time to sell cars on TradeRev', newAccount.Id));
//System.assert(taskCreatedForAccount('Monthly Report; Tips For Better Listings', newAccount.Id));
//System.assert(taskCreatedForAccount('Touch Point; How are you doing? Can I help with anything? Good time to sell cars on TradeRev', newAccount.Id));
//System.assert(taskCreatedForAccount('Monthly Report; Tips For Better Listings', newAccount.Id));
}
@isTest static void testIndepedentDealerWorkflow() {
Account newAccount = new Account(
Name = 'Test Independent Dealer',
AccountSource = 'Web',
Account_Type__c = 'Independent Dealer');
insert newAccount;
Task[] tasks = [SELECT Id, Subject FROM Task WHERE WhatId = :newAccount.Id AND Subject = 'Set appointment for Installation and Training'];
tasks[0].Status = 'Completed';
update tasks[0];
System.assert(taskCreatedForAccount('Assist with On-boarding', newAccount.Id));
System.assert(taskCreatedForAccount('Any questions on bidding?', newAccount.Id));
System.assert(taskCreatedForAccount('Have they been bidding? Any cars in pending?', newAccount.Id));
System.assert(taskCreatedForAccount('Won any cars? Discuss timing expectations for payment and pickup', newAccount.Id));
System.assert(taskCreatedForAccount('If a car has been won; have they paid and picked up car yet?', newAccount.Id));
System.assert(taskCreatedForAccount('Feedback on TradeRev; any referrals?', newAccount.Id));
System.assert(taskCreatedForAccount('Touch Point; How are you doing? Can I help with anything? Having success moving the cars bought on TradeRev?', newAccount.Id));
System.assert(taskCreatedForAccount('Touch Point; How are you doing? Can I help with anything? Having success moving the cars bought on TradeRev?', newAccount.Id));
}
static boolean taskCreatedForLead(String subject, Id id) {
Task[] tasks = [SELECT Id, Subject FROM Task WHERE WhoId = :id AND Subject = :subject];
if (tasks.size() > 0) {
tasks[0].Status = 'Completed';
update tasks[0];
return true;
}
else {
return false;
}
}
static boolean taskCreatedForAccount(String subject, Id id) {
Task[] tasks = [SELECT Id, Subject FROM Task WHERE WhatId = :id AND Subject = :subject];
if (tasks.size() > 0) {
tasks[0].Status = 'Completed';
update tasks[0];
return true;
}
else {
return false;
}
}
}
Ok Try one more thing
Remove the condition WhoId = :newLead.Id in your test class like
Task[] tasks = [SELECT Id, Subject FROM Task WHERE Subject = 'Evaluate and Assign Web Lead'];
tasks[0].Status = 'Completed';
I think this will resolve the issue.
Thanks
Shashikant
All Answers
In the following code, you are trying to fetch a task record for a Lead that you just created.
Unless you have a Process, Workflow or a Trigger that creates Task as soon as a Lead is created, tasks list will be empty.
To avoid this error, put a condition check in place
For example, Use similar check in every place where you are getting this error.
Let me know if this helps.
Your query on Task not returing any result and then you are accessing item from 0t index and that is what causing the issue.
Task[] tasks = [SELECT Id, Subject FROM Task WHERE WhoId = :newLead.Id AND Subject = 'Evaluate and Assign Web Lead'];
tasks[0].Status = 'Completed';
Resolution: I think you are expecting Task to be created by other process so make sure that condition to invoke that Trigger/Process Builder is matched so your query return result.
Thanks
Shashikant
You are correct, the task should be created per a workflow rule. The criteria is being met in the lead being created in the class (I even double checked the spelling) and it is still not firing the workflow. Is there another way to test or check this? Perhaps some other functionality that doesn't fire workflow rules in a test class?
I would suggest
1. Check if Workflow is Active
2. If Workflow is Active then test use case manually if it creates the Task and WhatId is populated with the Account Record and Subject is Set Properly as per test class.
I think above check will debug the issue for you. If not then please post the Workflow Rule Screen Shot and Task Action Screen shot.
Thanks
Shashikant
Thank you!
DaNae
Ok Try one more thing
Remove the condition WhoId = :newLead.Id in your test class like
Task[] tasks = [SELECT Id, Subject FROM Task WHERE Subject = 'Evaluate and Assign Web Lead'];
tasks[0].Status = 'Completed';
I think this will resolve the issue.
Thanks
Shashikant