function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rohan SRohan S 

Error while inserting a record possibly caused due to related Apex Class

I'm getting the following error while saving a record in Student object: "Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger StudentAttendanceTrigger caused an unexpected exception, contact your administrator: StudentAttendanceTrigger: execution of AfterInsert caused by: System.ListException: List index out of bounds: 12: Class.StudentAttendanceClass.StudentAttendance: line 24, column 1"

I have the following objects and fields:
Object 1: Student
Field Name: Name(Std), & Roll_Number__c

Object 2: Attendance
Field Name: Name(Std), Roll_Number__c, Student__c(Master-Detail Reln, Student object is master)

My requirement is when I create any student record, it should create 12 Attendance records (Jan to Dec) in Attendance object. I have written the following Apex class. I'm getting the above error while inserting a record in Student object. Any inputs highly appreciated!!!


public class StudentAttendanceClass
{
public static void StudentAttendance(list<Student__c> StudentsList)
{
list<Attendance__c> AttendanceList = new list<Attendance__c>();
list<string> MonthsList = new list<string>();
MonthsList.add('Jan');
MonthsList.add('Feb');
MonthsList.add('Mar');
MonthsList.add('Apr');
MonthsList.add('May');
MonthsList.add('Jun');
MonthsList.add('Jul');
MonthsList.add('Aug');
MonthsList.add('Sep');
MonthsList.add('Oct');
MonthsList.add('Nov');
MonthsList.add('Dec');
for(Student__c VarS : StudentsList)
{
for(integer p = 1; p <= 12; p = p+1)
{
Attendance__c VarA = new Attendance__c();
VarA.Name = MonthsList.get(p);
VarA.Student__c = VarS.id;
VarA.Roll_Number__c = VarS.Roll_Number__c;
AttendanceList.add(VarA);
}
}
insert AttendanceList;
}
}
Best Answer chosen by Rohan S
Khan AnasKhan Anas (Salesforce Developers) 
Hi Rohit,

Greetings to you!

MonthsList.get(12) has no elements so trying to access the 12th index element will cause the list index out of bounds exception. The index position of the first element in a list is always 0. So, the last element is on the 11th index in your case.

You need to change the for loop as:
for(integer p = 0; p < 12; p = p+1)

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_lists.htm

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Rohit,

Greetings to you!

MonthsList.get(12) has no elements so trying to access the 12th index element will cause the list index out of bounds exception. The index position of the first element in a list is always 0. So, the last element is on the 11th index in your case.

You need to change the for loop as:
for(integer p = 0; p < 12; p = p+1)

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_lists.htm

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Rohan SRohan S
Thanks Anas. It worked :)