I am getting an error when I try to save data from a Visualforce page calling a controller method. Can anyone help me figure out how to avoid this error? The error is:
List index out of bounds: 1 Error is in expression '{!saveAdd}' in component in page rda_task: Class.RDATasks.saveAdd: line 68, column 1
Controller:
public with sharing class RDATasks{
public list<Task> listTask{get;set;}
public list<Task> listTaskFollow{get;set;}
public integer n;
Set<Task>Tasks = new Set<Task>();
Set<Task>FollowTasks = new Set<Task>();
private final Contact con;
public RDATasks(ApexPages.StandardController myController){
listTask = new List<Task>();
listTaskFollow = new List<Task>();
con=(Contact)myController.getRecord();
}
// Adds RDA Task to list
public void RDA()
{
listTask.add(new Task(
WhoId = con.Id,
Status = 'Completed',
ActivityDate = system.TODAY(),
Subject = '',
Description = '',
OwnerId = UserInfo.getUserId(),
WhatId = con.AccountId,
RDA__c = TRUE)
);
}
// Adds RDA Follow-Up Task to list
public void RDAfollow()
{
n=listTask.size()-1;
listTaskFollow.add(new Task(
WhoId = con.Id,
Status = 'In Progress',
ActivityDate = system.TODAY()+7,
Subject = 'Follow-Up: '+listTask[n].Subject,
OwnerId = UserInfo.getUserId(),
WhatId = con.AccountId,
RDA__c = TRUE)
);
}
// Saves of RDA & follow-up tasks & add another
public PageReference saveAdd(){
insert listTask;
insert listTaskFollow;
if(!listTaskFollow.isEmpty()){
Tasks.addAll(listTask);
FollowTasks.addAll(listTaskFollow);
if(!Tasks.containsAll(FollowTasks)){
integer last = listTaskFollow.size();
listTask.add(listTaskFollow[last]);
}
}
RDAfollow();
{
PageReference contactPage = new PageReference('/apex/RDA_Task?id=' + con.Id);
contactPage.setRedirect(true);
return contactPage;
}
}
// Saves RDA & Follow-up Tasks added no additional
public PageReference saveRDA(){
insert listTask;
insert listTaskFollow;
if(!listTaskFollow.isEmpty()){
Tasks.addAll(listTask);
FollowTasks.addAll(listTaskFollow);
if(!Tasks.containsAll(FollowTasks)){
integer last = listTaskFollow.size();
listTask.add(listTaskFollow[last]);
}
}
RDAfollow();
{
PageReference contactPage = new PageReference('/' + con.Id);
contactPage.setRedirect(true);
return contactPage;
}
}
}