Combine conditions with && and ||
Apex basics · Beginner · 10 min · Runs in your Salesforce Developer org · Free
Write `isUrgentOpen(String priority, Boolean sev1, String status)` that returns `true` when a Case is both urgent (High priority or sev1) and still open (status not `Closed`). Combine the two urgent checks with `||`, group them in parentheses, then join to the status check with `&&`.
Lesson
Why this matters
Real business rules almost never fit one check. "Urgent" means High priority or sev1. "Needs action" means urgent and still open. If you can combine && and || correctly, you can turn an English rule into one line of Apex instead of a tangle of if statements.
The smallest working example
Boolean urgent = (priority == 'High' || sev1) && status != 'Closed';
For priority = 'High', sev1 = false, status = 'New', urgent is true.
Name the parts
- **
&&** is Apex's "and". Both sides must betrue. - **
||** is Apex's "or". Either side beingtrueis enough. - Parentheses force the order.
A || B && Cis read asA || (B && C). If you want "(A or B) and C", you must write the parentheses yourself. - Apex short-circuits: once
(priority == 'High' || sev1)istrue, it still has to check the right side of&&, but once&&seesfalseon the left, it skips the right side.
A common wrong version
Boolean urgent = priority == 'High' || sev1 && status != 'Closed';
Without parentheses, this reads as priority == 'High' || (sev1 && status != 'Closed'). A Closed High-priority Case returns true, which breaks the "Closed overrides" rule.
The fix
Wrap the "or" half in parentheses, so the && with status applies to the whole urgent half:
return (priority == 'High' || sev1) && status != 'Closed';
Now you try
Return that one expression from isUrgentOpen. The grader tries open and closed cases, High and Low priority, sev1 on and off.
Learn more
The challenge
Write isUrgentOpen(String priority, Boolean sev1, String status) that returns true when the Case is urgent (High priority or sev1) and still open (status is not Closed).
Examples:
isUrgentOpen('High', false, 'New')→trueisUrgentOpen('High', true, 'Closed')→falseisUrgentOpen('Low', false, 'New')→false
Requirements:
isUrgentOpen('High', false, 'New')should betrue.isUrgentOpen('Low', true, 'Working')should betrue.isUrgentOpen('High', true, 'Closed')should befalse.isUrgentOpen('Low', false, 'New')should befalse.isUrgentOpen('Medium', true, 'Escalated')should betrue.isUrgentOpen('High', false, 'Closed')should befalse.isUrgentOpen('Low', true, 'Closed')should befalse.
Starter code
Boolean isUrgentOpen(String priority, Boolean sev1, String status) {
return null;
}
Related drills
You write the Apex yourself. ApexSensei runs it and tells you what passed and what did not.
‹ Count Leads by Rating with a Map · Safe navigation with ?. ›