# Write a SOQL query to retrieve Accounts ordered by Name in ascending order, locked for editing in Apex (FOR UPDATE), while enforcing User Mode permissions (WITH USER_MODE).

**Topic:** Development · **Difficulty:** Hard · **Asked at:** Salesforce, Deloitte, Slalom · **Tags:** soql, scenario, locking, user-mode

Read it on the site: https://www.forcechallenges.com/interview-questions/write-a-soql-query-to-retrieve-accounts-ordered-by-name-in-ascending-order-locked-for-editing-in-apex-for

## Answer

Place `WITH USER_MODE` after the WHERE clause and before `ORDER BY`, then add `FOR UPDATE` at the end.

```apex
List<Account> accs = [SELECT Id, Name FROM Account WITH USER_MODE ORDER BY Name ASC FOR UPDATE];
```
This enforces the running user's object, field and sharing permissions while locking the returned rows for the rest of the transaction. Keep the transaction short to avoid `UNABLE_TO_LOCK_ROW` errors for others.

---
Source: ForceChallenges · last updated 2026-10-19
