This guide walks through 14 cross-object and conditional formula-field exercises. Each entry below includes the full problem statement, the governor-limit and best-practice constraints it's testing, and a numbered approach to solving it — then you can open the same problem in ApexArena's browser-based Apex editor and get instant pass/fail feedback against real test cases.
Write a Formula field named Commission__c on the Opportunity object that calculates the sales representative's commission:
StageName equals 'Closed Won' → commission = 5% of AmountIF() functionIF(logical_test, value_if_true, value_if_false)
Write a cross-object formula field named Account_Industry__c on the Contact object that:
Industry field from the parent AccountAccount.IndustryTo access a field on a related object, use dot notation through the relationship name:
RelationshipName.FieldName
For standard lookup relationships: Account.Industry, Owner.Name, etc.
ISBLANK(value) — returns true if the value is null or empty stringWrite a formula field Discount_Pct__c on Opportunity that returns the discount percentage based on the Amount:
| Amount Range | Discount |
|---|---|
| ≥ 50,000 | 15% |
| ≥ 20,000 | 10% |
| ≥ 10,000 | 8% |
| ≥ 5,000 | 5% |
| < 5,000 | 0% |
Percent field — return the number (e.g., 15 for 15%)
Both approaches are valid. Nested IF() is often clearer for range-based logic.
Write two formula fields on the Opportunity object:
Calculate how many days the opportunity has been open:
IsClosed = true): CloseDate - CreatedDateTODAY() - DATEVALUE(CreatedDate)ROUND(..., 0) to get a whole numberCategorize the opportunity age using Days_Open__c:
TODAY() — returns today's dateDATEVALUE(datetime) — converts DateTime to DateROUND(number, decimal_places) — rounds to whole numberA Contact object has a text field Nickname__c (can be blank)
and a number field Years_Known__c (can be null).
Create two formula fields:
Display_Name__c — returns Nickname__c if not blank, otherwise returns FirstNameKnown_Years__c — returns Years_Known__c if not null, otherwise returns 0BLANKVALUE(field, substitute) — returns field unless blank, then returns substituteISNULL(field) — returns TRUE if field is nullIF(condition, true_val, false_val)A Lead object has a text field Raw_Code__c (format: AB-12345-XY).
Create three formula fields:
Code_Upper__c — returns Raw_Code__c in ALL CAPSCode_Prefix__c — returns the first 2 characters of Raw_Code__cCode_Suffix__c — returns the last 2 characters of Raw_Code__cUPPER(text) / LOWER(text)LEFT(text, num_chars) / RIGHT(text, num_chars)MID(text, start_num, num_chars)LEN(text)An Opportunity has fields Raw_Score__c (decimal, can be negative)
and Bonus__c (number).
Create three formula fields:
Floor_Score__c — the largest integer ≤ Raw_Score__cAbs_Score__c — the absolute value of Raw_Score__cRemainder__c — remainder when Raw_Score__c is divided by 7FLOOR(number) — round down to nearest integerCEILING(number) — round up to nearest integerABS(number) — absolute valueMOD(number, divisor) — remainder after divisionMAX(n1, n2, ...) / MIN(n1, n2, ...)Every problem above runs in a real in-browser Apex editor with instant pass/fail feedback and best-practice linting. No Salesforce org needed.
Create Free Account →Write a Validation Rule on the Opportunity object that prevents
users from reducing the Amount field once it has been set.
The rule should fire (return TRUE to block save) when:
Amount is less than the previous valueAmount was not nullPRIORVALUE(field) — returns the field's value before the current editISNEW() — returns TRUE if the record is being created for the first timeWrite a Validation Rule on the Contact object that ensures
the Phone field (if not blank) matches the format (XXX) XXX-XXXX
where X is a digit.
Valid examples: (415) 555-1234, (800) 867-5309
The rule should fire (return TRUE) when Phone is not blank but does not match the expected pattern.
REGEX(text, pattern) — returns TRUE if text matches the regex patternNOT(REGEX(...)) — returns TRUE when it does NOT match (use in validation)A Lead object has these fields:
AnnualRevenue (currency)NumberOfEmployees (integer)Industry (text)IsConverted (boolean)Create a checkbox formula field Is_Qualified__c that returns TRUE
when the lead is not converted AND meets at least one of these criteria:
An Opportunity has a picklist field StageName.
Create a formula field Stage_Group__c (Text) that maps stages to groups:
"Early""Mid""Late""Won""Lost"ISPICKVAL(field, value) — returns TRUE if picklist field equals the given valueA Contract object has a StartDate (Date) field.
Create two formula fields:
Contract_Year__c (Number) — the year portion of StartDateAnniversary_Date__c (Date) — the date exactly one year after StartDate
using DATE(YEAR(StartDate)+1, MONTH(StartDate), DAY(StartDate))YEAR(date) / MONTH(date) / DAY(date)DATE(year, month, day) — construct a date from partsDATEVALUE(text_or_datetime) — convert to DateTODAY() — today's dateYou have a custom object Region_Config__c with fields:
Region_Code__c (Text, external ID) — e.g., "NA", "EMEA", "APAC"Tax_Rate__c (Percent)Currency_Symbol__c (Text)On the Opportunity object there is a text field Region__c.
Create two formula fields on Opportunity:
Lookup_Tax_Rate__c — the Tax_Rate__c from Region_Config__c where Region_Code__c = Region__cLookup_Currency__c — the Currency_Symbol__c from the same lookupVLOOKUP(field_to_return, lookup_field, lookup_value)A Case object has these fields:
Priority (picklist: "High", "Medium", "Low")Account.Industry (text via relationship)CreatedDate (DateTime)Create a formula field SLA_Tier__c (Text) using these rules:
"Platinum""Gold""Silver""Bronze""Standard"Also create SLA_Hours__c (Number) using CASE() to map Priority to response hours:
High→4, Medium→8, Low→24.
Write and run real Apex code right in your browser — instant pass/fail feedback, best-practice linting, and governor limit monitoring. No Salesforce org needed.
Create Free Account → Explore All ProblemsApexArena is a free, browser-based Salesforce Apex coding practice platform covering every major topic tested on the Salesforce Platform Developer I (PD1) and Platform Developer II (PD2) certification exams. All problems run directly in your browser with instant pass/fail feedback, best-practice linting (SOQL in loops, DML in loops, empty catch blocks), and governor limit monitoring — no Salesforce Developer Edition org required.
Related tutorials: Apex Triggers · SOQL · Batch Apex · Interview Q&A · Governor Limits