Development questions
Write a SOQL query to find all LoginHistory records for a specific User in the last 24 hours where the login status failed.
Filter LoginHistory by user, time window and non-success status.
apex
Datetime since = Datetime.now().addHours(-24);
List<LoginHistory> failed = [
SELECT UserId, LoginTime, Status, SourceIp, LoginType, Application
FROM LoginHistory
WHERE UserId = :userId AND LoginTime >= :since AND Status != 'Success'
ORDER BY LoginTime DESC
];
Using a datetime bind variable is the reliable way to express "last 24 hours", since date literals work in whole days.
soqlscenariologin-historyaudit
More Development questions
- What is an Apex Trigger in Salesforce and when should you use it over Flows?
- What is the difference between before triggers and after triggers?
- Explain all Trigger context variables (isInsert, isUpdate, isDelete, isUndelete, isBefore, isAfter, isExecuting, size, operationType).
- What is the difference between Trigger.new and Trigger.old?
- In which trigger events are Trigger.new and Trigger.newMap available?
- In which trigger events are Trigger.old and Trigger.oldMap available?