Automated Attendance Management System in Salesforce (Apex)
Objective:
To automate student attendance tracking by:
- Excluding Saturdays, Sundays, and approved leaves.
- Auto-generating daily attendance and monthly summaries.
- Reducing manual effort for teachers/admins.
Modules:
(a) Student Management
- Stores student details (Student__c object).
- Each student is uniquely identified by Student ID.
Field Name | Field Type | Value | Description |
---|---|---|---|
Student_name | Text | NA | Contain Student name. |
Admission_Date | Date | NA | Store Student Admission Date |
Roll_number | Text | NA | Student Roll_number |
Class | Picklist | BCA,MBA,BBA,MCABTech,MTech | Contain Student Stream . |

(b) Leave Management
- Students can apply for leaves (Leave__c object).
- Each leave request includes From_Date__c and To_Date__c.
Field Name | Field Type | Value | Description |
---|---|---|---|
Student | Lookup (Student) | NA | Take a Student name from Student Object. |
To_Date | Date | NA | Last date of your leave |
From_Date | Date | NA | First date from you takes a leave |
Reason | text | NA | Reason why you taking leave. |
(c) Daily Attendance
- Attendance__c object records Present / Absent / Leave for each student per day.
- Saturdays and Sundays are skipped automatically.
Field Name | Field Type | Value | Description |
---|---|---|---|
Student | Lookup (Student) | NA | Take a Student name from Student Object. |
Date | Date | NA | Contain daily dates |
Status | Picklist | Present , Absent, Leave | Daily Status of attendance |
(d) Monthly Attendance
- Monthly_Attendance__c object stores summary:
- Total Working Days
- Total Present
- Total Absent
- Total Leaves
Field Name | Field Type | Value | Description |
---|---|---|---|
Student | Lookup (Student) | NA | Take a Student name from Student Object. |
Month | Date | NA | Contain Name of Months |
Year | Text | NA | Contain YEAR |
Total Working Days | Number | NA | Count of working day excluding sat,sun |
Total present | Number | NA | Count Present day |
Total Absent | Number | NA | Count Absent Day |
Leave | Number | NA | Count Leave |
Automation:
- Helper Class (AttendanceHelper.cls): Generates attendance data.
public class AttendanceHelper {
// Generate Monthly Attendance
public static void generateMonthlyAttendance(Integer year, Integer month) {
Date startDate = Date.newInstance(year, month, 1);
Date endDate = startDate.toStartOfMonth().addMonths(1).addDays(-1);
List<Student__c> students = [SELECT Id FROM Student__c];
List<Leave__c> allLeaves = [
SELECT Id, Student__c, From_Date__c, To_Date__c
FROM Leave__c
];
// Map<studentId, List<Leave>>
Map<Id, List<Leave__c>> studentLeaves = new Map<Id, List<Leave__c>>();
for (Leave__c lv : allLeaves) {
if (!studentLeaves.containsKey(lv.Student__c)) {
studentLeaves.put(lv.Student__c, new List<Leave__c>());
}
studentLeaves.get(lv.Student__c).add(lv);
}
List<Attendance__c> attendanceList = new List<Attendance__c>();
List<Monthly_Attendance__c> monthlyList = new List<Monthly_Attendance__c>();
for (Student__c stu : students) {
Integer totalWorking = 0;
Integer totalPresent = 0;
Integer totalAbsent = 0;
Integer totalLeave = 0;
for (Date d = startDate; d <= endDate; d = d.addDays(1)) {
// Get Day Name (Sat/Sun check)
String dayName = DateTime.newInstance(d.year(), d.month(), d.day()).format(‘E’);
if (dayName == ‘Sat’ || dayName == ‘Sun’) {
continue;
}
totalWorking++;
Boolean isLeave = false;
// Check Leave
if (studentLeaves.containsKey(stu.Id)) {
for (Leave__c lv : studentLeaves.get(stu.Id)) {
if (d >= lv.From_Date__c && d <= lv.To_Date__c) {
isLeave = true;
break;
}
}
}
String status = ‘Absent’;
if (isLeave) {
status = ‘Leave’;
totalLeave++;
} else {
status = ‘Present’;
totalPresent++;
}
attendanceList.add(new Attendance__c(
Student__c = stu.Id,
Date__c = d,
Status__c = status
));
}
totalAbsent = totalWorking – (totalPresent + totalLeave);
monthlyList.add(new Monthly_Attendance__c(
Student__c = stu.Id,
Month__c = DateTime.newInstance(startDate.year(), startDate.month(), 1).format(‘MMMM’),
Year__c = String.valueOf(year),
Total_Working_Days__c = totalWorking,
Total_Present__c = totalPresent,
Total_Absent__c = totalAbsent,
Total_Leaves__c = totalLeave
));
}
if (!attendanceList.isEmpty()) {
insert attendanceList;
}
if (!monthlyList.isEmpty()) {
insert monthlyList;
}
}
}
- Scheduler (AttendanceScheduler.cls): Runs automatically on the 1st of every month to generate attendance for the previous month.
global class AttendanceScheduler implements Schedulable {
global void execute(SchedulableContext sc) {
Date today = Date.today();
Integer prevMonth = today.addMonths(-1).month();
Integer prevYear = today.addMonths(-1).year();
AttendanceHelper.generateMonthlyAttendance(prevYear, prevMonth);
}
}
Workflow:
1. Admin adds students into Student__c.
2. Students apply for leave → stored in Leave__c.
3. On 1st of every month, scheduler triggers AttendanceHelper.
4. Helper:
a. Loops through all students and days.
b. Skips Saturdays & Sundays.
c. Checks if student has leave on that day.
d. Marks Present / Absent / Leave in Attendance__c.
e. Generates summary record in Monthly_Attendance__c.
Features:
- Auto calculation of working days excluding weekends.
- Auto leave management.
- Monthly summary.
- Scalable for any class size.
- Reduces manual workload for staff.
Advantages:
- Time-saving – Fully automated.
- Error-free – No manual counting errors.
- Reporting ready – Easy to generate student reports.
- Scalable – Works for schools, colleges, companies.