Configure Attendance Rules: Create Attendance Groups in Batch and Adjust Them Dynamically
Overview
This solution automates attendance group management end to end. It uses the attendance group management APIs on the DingTalk Open Platform to integrate business platforms such as HR and OA systems with DingTalk Attendance.Background
Organizations face the following challenges in attendance management:- Complex attendance rules: Fixed shift attendance, shift-based attendance, flexible working hours, and other modes coexist. Manual configuration is slow and error-prone.
- Frequent personnel changes: Onboarding, offboarding, and transfers require manual updates to attendance group members. Organizations with 1,000+ employees spend several hours on this every week.
- No cross-system sync: Organization Structure changes in the HR system do not reach DingTalk attendance groups in time, which causes clock-in data errors.
- No batch operations: Admins cannot create or update multiple attendance groups at once. Updating them one by one is highly inefficient.
Key Benefits
This solution helps organizations achieve the following:- Automated attendance group management: Create, query, update, and delete attendance groups through the API with zero manual intervention.
- Smart shift association: Create new shifts or associate existing ones automatically to keep attendance rules accurate.
- Efficient batch execution: Get, search, and update attendance groups in batch to significantly improve operational efficiency.
- Unified cross-system data: Sync HR, OA, and DingTalk Attendance data in real time to eliminate data silos.
Use Cases
This solution fits the following typical business scenarios:- HR system integration: Sync attendance rules from the HR management system to DingTalk attendance groups automatically.
- Mergers and acquisitions: Quickly create attendance groups and shift configurations in batch for newly acquired companies.
- OA workflow integration: Create or adjust the corresponding attendance group automatically when an Approval process ends.
- Dynamic permission adjustment: Update attendance group members and Admin permissions automatically based on Department changes.
Typical Business Scenarios
Scenario 1: Sync Attendance Groups from the HR System (Batch Creation and Management)
Pain Points
In the traditional model, after HR plans new attendance rules in the HR system, they must sign in to the DingTalk admin console and manually create each attendance group, configure shifts, and add members. Organizations with 1,000+ employees typically have 20 to 50 attendance groups. Manual setup takes hours and often leads to incorrect shifts or missing members.Value Validation
-
Automated workflow
The API-based Automation flow runs in these steps:
- The HR system records a new Employee.
- The system finds the matching attendance group automatically based on the Employee’s Department.
- The system adds new members in batch through the API.
- The system verifies and confirms the result in real time.
- Clock-in permission takes effect immediately.
-
Key advantages
- Fully automatic trigger: The API call runs automatically after HR enters a new Employee in the HR system. No one needs to sign in to the DingTalk admin console or start the action manually. Entry and sync happen together.
- Precise matching: The system queries and matches the
groupKeyof the attendance group automatically based on the Employee’s Department ID. This prevents clock-in errors caused by selecting the wrong attendance group manually. - Immediate effect: After the batch-add-employees API adds members in batch, the query-members-by-id API verifies the result immediately. Employees can clock in as soon as they join the attendance group.
- Audit trail: Each API call records the operator, timestamp, attendance group ID, Employee
userId, and other details to meet compliance auditing and traceability requirements.
Scenario 2: Sync Attendance Groups Automatically after Organization Structure Changes
Pain Points
When departments merge or split, or when employees transfer, HR must manually update the member lists and scheduling rules of each affected attendance group. Large-scale restructuring can involve dozens of attendance groups. Manual updates are time-consuming and often miss some groups, which causes clock-in errors for employees.Value Validation
-
Automated workflow
The Automation flow runs in five steps:
- OA Approval trigger: When an approval for a department merge or split or a personnel transfer is passed, the system automatically captures the change event.
- Identify affected attendance groups: The system queries all associated attendance groups automatically based on the changed Department ID.
- API batch update: The system calls the DingTalk attendance group update API to modify member lists and shift configurations in batch.
- Real-time data validation: Immediately after the update, the system calls the query API to verify data consistency across all attendance groups.
- Sync complete: Attendance group synchronization finishes and the updated attendance rules take effect with no manual intervention.
-
Key advantages
- Automatic trigger: The system captures the change event and runs the sync automatically when the OA Approval process ends. No one needs to start or monitor it. Approval and sync happen together.
- Precise batch processing: The system identifies and updates all affected attendance groups at once. This prevents omissions from manual lookups and ensures 100% coverage of organizational changes.
- Real-time validation and Feedback: The system calls the query API right after the update to verify data consistency and sends alerts automatically on errors. This ensures attendance rules take effect correctly.
- Complete audit logs: Each API call records the operator, timestamp, and change Content to meet compliance auditing and traceability requirements.
Implementation Guide
Technical Architecture
This solution is built on the Server API of the DingTalk Open Platform. The core technical components are:- Authentication layer: Uses the
Client IDandClient Secretto get theaccess_tokenand secure API calls. - Attendance group management layer: Provides complete API capabilities to create, query, update, and delete attendance groups. Supports fixed shift attendance groups and shift-based attendance groups.
- Shift management layer: Provides API capabilities to create and query shifts, which are prerequisite resources for attendance groups.
- Error handling layer: Provides a comprehensive error code system to help you locate and resolve issues quickly.
Prerequisites
Meet the following conditions before you implement the solution:- App preparation: Create and configure an Internal app. For details, see Create and Configure an App.
-
Permission requirements: Have DingTalk Organization Admin or Sub Admin permissions, and apply for the following API permissions:
qyapi_attendance_group_manage(attendance group management permission)qyapi_attendance_group_read(attendance group query permission)
- Development environment: Install a Java development environment (JDK 1.6 or later) and the Maven build tool.
- SDK preparation: Download the DingTalk Server-side SDK. For details, see Server-side SDK Download. The SDK supports Java, Python, Go, and other languages.
Code Implementation
Step 1: Get the App credential information, including the client ID and Client Secret. Step 2: Apply for API permissions related to attendance. Step 3: Get the Access credential of the App. See Get the access_token of an Internal App. When you call the API, the access token authenticates the caller’s identity. Step 4: Call the attendance APIs:- Call the Server API Create an Attendance Group to get the attendance group
id.
Note
The examples in this document use a fixed shift attendance group. To create a fixed shift attendance group, make sure a shift exists before you call the Create an Attendance Group API:- If no shift exists, call the Server API Create a Shift to get the shift ID.
- If a shift already exists, call the Server API Get Shift Summaries to get the existing shift ID.
-
Get attendance group information.
-
Get information about all attendance groups in the Organization
- Call the Server API Get Attendance Group Summaries in Batch to get summaries of all attendance groups in the Organization.
- Call the Server API Get Attendance Group Details in Batch to get details of all attendance groups in the Organization.
- Call the Server API Search Attendance Group Summaries to run a fuzzy query for attendance groups by name.
-
Get information about a single attendance group
-
Call the Server API Get Attendance Group Details with the attendance group
idto get the details of a single attendance group. -
Get attendance group details by
groupKey- Call the Server API Convert groupId to groupKey with the attendance group
idto get the attendance groupgroupKey. - Call the Server API Query Attendance Group Information by groupKey with the attendance group
groupKeyto get the attendance group information.
- Call the Server API Convert groupId to groupKey with the attendance group
-
Call the Server API Get Attendance Group Details with the attendance group
-
Get information about all attendance groups in the Organization
-
Call the Server API Update an Attendance Group with the attendance group
idto modify the attendance group name, shifts, and other information. -
Call the Server API Delete an Attendance Group with the attendance group
groupKeyto delete the attendance group.
Implementation Steps
Step 1: Get the App Credentials
- Sign in to the DingTalk Developer Console.
- Select the target App to open the App details page.
- Click Basic Information > Credentials and Basic Information.
- Record the App’s
Client IDandClient Secret.
Note
Keep theClient Secret secure and never share it with third parties. Store it in environment variables or a key management system instead of hardcoding it.
Step 2: Add API Permissions
- On the App details page, click Manage permissions.
-
Enter the following permission identifiers in the permission search box and apply for them:
qyapi_attendance_group_manage(attendance group management permission): Used to create, update, and delete attendance groups.qyapi_attendance_group_read(attendance group query permission): Used to query attendance group summaries and details.
Different business scenarios may require different permission combinations. Apply based on your actual needs. For example, batch Import scenarios also require permissions for batch User operations.
Step 3: Get the Access Credential (access_token)
Use the Client ID and Client Secret from Step 1 to get the App’s Access credential. See Get the access_token of an Internal App.- Caching strategy: The
access_tokenis valid for 2 hours. Cache it in memory or Redis and Refresh it proactively 5 minutes before it expires. - Concurrency control: Prevent multiple threads from refreshing the token at the same time, which causes Conflicts. Use a distributed lock.
- Retry on errors: Retry automatically on network fluctuations, up to 3 times, with increasing intervals (1s → 2s → 4s).
Step 4: Call the Core APIs
Create an Attendance Group
The examples in this document use a fixed shift attendance group.
-
If no shift exists, call the Server API Create a Shift to get the shift ID.
-
If a shift already exists, call the Server API Get Shift Summaries to get the existing shift ID.
id.
Get Attendance Group Information
-
Get information about all attendance groups in the Organization
-
Get attendance group summaries in batch: Call the Server API Get Attendance Group Summaries in Batch to get summaries of all attendance groups in the Organization.
-
Get attendance group details in batch: Call the Server API Get Attendance Group Details in Batch to get details of all attendance groups in the Organization.
-
Search attendance group summaries: Call the Server API Search Attendance Group Summaries to run a fuzzy query for attendance groups by name.
-
Get attendance group summaries in batch: Call the Server API Get Attendance Group Summaries in Batch to get summaries of all attendance groups in the Organization.
-
Get information about a single attendance group
-
Get details of a single attendance group: Call the Server API Get Attendance Group Details with the attendance group
idto get the details of a single attendance group. -
Convert groupId to groupKey: Call the Server API Convert groupId to groupKey with the attendance group
idto get the attendance groupgroupKey. -
Query attendance group information by groupKey: Call the Server API Query Attendance Group Information by groupKey with the attendance group
groupKeyto get the attendance group information.
-
Get details of a single attendance group: Call the Server API Get Attendance Group Details with the attendance group
Update an Attendance Group
Call the Server API Update an Attendance Group with the attendance groupid to modify the attendance group name, shifts, and other information.
Delete an Attendance Group
Call the Server API Delete an Attendance Group with the attendance groupgroupKey to delete the attendance group.
Note
- Confirm that the attendance group has no active employees. Call the Get Attendance Group Details API first to check.
- Transfer the To-Do items, Approval forms, and other items associated with the attendance group to other attendance groups.
- Back up the historical data of the attendance group, if necessary.
- Notify the relevant departments (IT, HR, Administration, etc.) to complete follow-up cleanup.
FAQ
- Q1: What should I do if I get a “Shift does not exist” error when creating an attendance group? A: A fixed shift attendance group requires an existing shift. Call the Create a Shift API first, or call the Get Shift Summaries API to get an existing shift ID. Then create the attendance group.
-
Q2: How do I get information about all attendance groups in batch?
A: Follow two steps. First, call Get Attendance Group Summaries in Batch to get brief information (including the
id) for all attendance groups. Then call Get Attendance Group Details in Batch as needed to get the full details. -
Q3: What is the difference between the attendance group id and groupKey?
A:
idis the numeric identifier of the attendance group, andgroupKeyis its string identifier. Some APIs, such as Delete an Attendance Group, require thegroupKey. Use the Convert groupId to groupKey API to convert between them. -
Q4: Do I need to pass all fields when updating an attendance group?
A: No. Pass only the fields to modify and the attendance group
id. Fields you omit keep their original values.