# Automatic SAP User Lock Job for Leaving Employees

> A background job that, on the leaving date, automatically locks the SAP users stored in infotype 0105 for employees with a recorded leaving action and sets the user's validity end to the leaving date. Locked users and leavers without a user found are reported to the security team by email.

- Type: Program, Custom development, Background program
- Module: HCM
- Estimated effort: M (5–8 working days)
- Last updated: 2026-11-17
- URL: https://nekuhub.com/en/programs/lock-sap-users-of-leaving-employees-automatically/


## Why is the standard not enough?

The PA40 leaving action updates HR records but does not touch the SAP user; SU10 locks users in bulk but does not know which users belong to leaving employees. When locking is done by hand from HR emails it is delayed, and audits report "active user of a leaver" findings.

### Closest standard alternatives

- SU10: Locks users in bulk; does not know which user belongs to a leaving employee.
- PA40: Records the leaving action; does not lock the SAP user.

## Tables used

| Table | Role | Access | Join | System |
|---|---|---|---|---|
| PA0000 | Leaving action and date (driving table) | Read | - | ECC + S/4 |
| PA0001 | Name and company code | Read | PA0001-PERNR = PA0000-PERNR | ECC + S/4 |
| PA0105 | Subtype 0001: the employee's SAP user name | Read | PA0105-PERNR = PA0000-PERNR AND PA0105-SUBTY = '0001' | ECC + S/4 |
| USR02 | User type, lock status, validity and last logon (updated via BAPI) | Read | USR02-BNAME = PA0105-USRID | ECC + S/4 |

## BAPIs and functions used

- BAPI_USER_LOCK: Locks the leaving employee's user
- BAPI_USER_CHANGE: Sets the user's validity end to the leaving date
- SO_DOCUMENT_SEND_API1: Emails the result list to the security team

## Selection screen parameters

| Parameter | Description | Type | Reference field | Required |
|---|---|---|---|---|
| S_MASSN | Leaving action types | Range / multiple | PA0000-MASSN | Yes |
| P_DAYS | Look-back window (days) | Single value | - | No |
| S_BUKRS | Company code | Range / multiple | PA0001-BUKRS | No |
| S_USTYP | User types to process | Range / multiple | USR02-USTYP | No |
| P_FUTURE | Set validity end for future-dated leavers | Checkbox | - | No |
| P_MAIL | Result email address or distribution list | Single value | - | No |
| P_TEST | Test mode (list without locking) | Checkbox | - | No |

## Result list columns

| Column | Source |
|---|---|
| Personnel no. | PA0000-PERNR |
| Name | PA0001-ENAME |
| Action type | PA0000-MASSN |
| Leaving date | PA0000-BEGDA |
| SAP user | PA0105-USRID |
| User type | USR02-USTYP |
| Last logon | USR02-TRDAT |
| Previous lock status | USR02-UFLAG |
| New validity end | USR02-GLTGB |
| Result | Calculated: Locked, already locked, validity set, no user or error |
| Message | Calculated: Return message from BAPI_USER_LOCK / BAPI_USER_CHANGE |

## How does the program work?

1. Records with employment status left (STAT2 = 0) for the selected leaving action types (MASSN) and a start date in the selected window (e.g. today and the last 7 days) are read from PA0000; leaving date = start date of the record.
2. Name and company code come from PA0001, and the subtype 0001 (SAP system user) record from PA0105, taken from the last record valid before the leaving date.
3. User type, current lock status (UFLAG), validity end and last logon date are read from USR02; service and system users are skipped depending on the selection screen.
4. When the leaving date has been reached, the user is locked with BAPI_USER_LOCK and the validity end is set to the leaving date with BAPI_USER_CHANGE. When the leaving date is in the future, only the validity end is set.
5. In test mode nothing is changed. Results are written to the log, and the list of locked, already locked and user-not-found employees is emailed to the security team.

## Authorization objects

- P_ORGIN: Read authorization for infotypes 0000, 0001 and 0105 (job user)
- S_USER_GRP: Lock and change authorization by user group (job user)

## Frequently asked questions

### Why is the user only locked, not deleted?

Locking and a validity end keep the audit trail: the user's past transactions and change documents stay visible under the user name. Deletion should be a separate process after the retention period ends.

### What if the employee is rehired on the same day?

If the job finds a valid hiring or rehire action after the leaving record, it does not lock the user and shows the case as a warning in the log.

### What if there are several SAP systems?

With central user administration, the lock is applied in the central system and distributed to the others. Without it, the job can be extended to call the same BAPIs in each system over a remote connection; this increases the effort range.

---
Let us build this program for your system: https://nekuhub.com/en/request-a-quote/?report=hcm-leaver-sap-user-lock-job
