Development questions
What is SOQL Injection, and how do you prevent it using bind variables or String.escapeSingleQuotes()?
SOQL injection happens when user input is concatenated into a dynamic query, letting an attacker change its logic (e.g. input ' OR Name != ').
Prevention:
- Use static SOQL with bind variables (
:name) - With dynamic SOQL, use
Database.queryWithBindsor bind variables inside the string - Sanitize with
String.escapeSingleQuotes()when you must concatenate - Validate input against allowed values (whitelist) for field/object names
apex
String name = String.escapeSingleQuotes(userInput);
String q = 'SELECT Id FROM Account WHERE Name = \'' + name + '\'';
soqlsecurityinjection
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?