Formula Field

Formula Field Practice Problems

14 free Salesforce Apex challenges · Full problem statements, best-practice notes, and step-by-step hints · Solve live on ApexArena

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.

On this page
  1. 1. Formula: Sales Commission Calculator
  2. 2. Formula: Cross-Object Formula — Parent Account Industry
  3. 3. Formula: Multi-Tier Discount with CASE()
  4. 4. Formula: Opportunity Days Open & Age Category
  5. 5. BLANKVALUE and ISNULL: Null Handling in Formulas
  6. 6. Text Functions: UPPER, LOWER, LEFT, RIGHT, MID, LEN
  7. 7. Number Functions: FLOOR, CEILING, ABS, MOD, MAX, MIN
  8. 8. PRIORVALUE: Prevent Reducing Opportunity Amount
  9. 9. REGEX: Validate Phone Number Format
  10. 10. Compound Logical: OR, AND, NOT for Lead Qualification
  11. 11. ISPICKVAL: Conditional Logic on Picklist Fields
  12. 12. Date Arithmetic: DATEVALUE, DATE, YEAR, MONTH, DAY
  13. 13. VLOOKUP: Auto-Fill Fields from a Custom Metadata Object
  14. 14. Advanced CASE() + Nested IF: SLA Tier Calculator
Easy Formula

1. Formula: Sales Commission Calculator

Problem #305 · Salesforce Apex Coding Challenge

Problem Statement

Write a Formula field named Commission__c on the Opportunity object that calculates the sales representative's commission:

  • If StageName equals 'Closed Won' → commission = 5% of Amount
  • Otherwise → commission = 0

Field Details

  • Object: Opportunity
  • Return type: Currency
  • Formula uses the IF() function

Syntax

IF(logical_test, value_if_true, value_if_false)
Approach
  • 1IF() takes three arguments: condition, true-value, false-value.
  • 2String comparisons in formulas use = not ==.
  • 3Multiply Amount by 0.05 to get 5%.
Medium Formula

2. Formula: Cross-Object Formula — Parent Account Industry

Problem #306 · Salesforce Apex Coding Challenge

Problem Statement

Write a cross-object formula field named Account_Industry__c on the Contact object that:

  • Returns the Industry field from the parent Account
  • If the Account's Industry is blank, returns the text 'No Industry'
  • Uses the cross-object formula syntax: Account.Industry

Cross-Object Formula Syntax

To 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.

Key Functions

  • ISBLANK(value) — returns true if the value is null or empty string
Approach
  • 1Cross-object formula: Account.Industry traverses the Contact's Account lookup.
  • 2ISBLANK() checks for null or empty — better than = null for text fields.
  • 3IF(ISBLANK(Account.Industry), "No Industry", Account.Industry)
Medium Formula

3. Formula: Multi-Tier Discount with CASE()

Problem #307 · Salesforce Apex Coding Challenge

Problem Statement

Write a formula field Discount_Pct__c on Opportunity that returns the discount percentage based on the Amount:

Amount RangeDiscount
≥ 50,00015%
≥ 20,00010%
≥ 10,0008%
≥ 5,0005%
< 5,0000%

Return Type

Percent field — return the number (e.g., 15 for 15%)

Use nested IF() or CASE()

Both approaches are valid. Nested IF() is often clearer for range-based logic.

Approach
  • 1Nested IF: IF(Amount >= 50000, 15, IF(Amount >= 20000, 10, ...))
  • 2Check from the highest tier downward — the first matching condition wins.
  • 3Return the number (15, not 0.15) for a Percent field.
Medium Formula

4. Formula: Opportunity Days Open & Age Category

Problem #308 · Salesforce Apex Coding Challenge

Problem Statement

Write two formula fields on the Opportunity object:

1. Days_Open__c (Number)

Calculate how many days the opportunity has been open:

  • If the opportunity is closed (IsClosed = true): CloseDate - CreatedDate
  • If still open: TODAY() - DATEVALUE(CreatedDate)
  • Use ROUND(..., 0) to get a whole number

2. Age_Category__c (Text)

Categorize the opportunity age using Days_Open__c:

  • ≥ 90 days → 'Stale'
  • ≥ 30 days → 'Aging'
  • ≥ 7 days → 'Active'
  • < 7 days → 'New'

Date Formula Functions

  • TODAY() — returns today's date
  • DATEVALUE(datetime) — converts DateTime to Date
  • ROUND(number, decimal_places) — rounds to whole number
Approach
  • 1DATEVALUE() converts CreatedDate (DateTime) to Date so subtraction works.
  • 2TODAY() returns today as a Date — no conversion needed.
  • 3Use IsClosed (Boolean checkbox field) in the IF condition, not a string compare.
Easy Formula Field

5. BLANKVALUE and ISNULL: Null Handling in Formulas

Problem #309 · Salesforce Apex Coding Challenge

Problem Statement

A 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 FirstName
  • Known_Years__c — returns Years_Known__c if not null, otherwise returns 0

Functions to Use

  • BLANKVALUE(field, substitute) — returns field unless blank, then returns substitute
  • ISNULL(field) — returns TRUE if field is null
  • IF(condition, true_val, false_val)
Approach
  • 1BLANKVALUE is the right tool for text fields — use it for Display_Name__c.
  • 2For number fields that can be null, use BLANKVALUE or IF(ISNULL(...), 0, field).
  • 3ISNULL does not work reliably on text fields in Salesforce — prefer ISBLANK for text.
Easy Formula Field

6. Text Functions: UPPER, LOWER, LEFT, RIGHT, MID, LEN

Problem #310 · Salesforce Apex Coding Challenge

Problem Statement

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 CAPS
  • Code_Prefix__c — returns the first 2 characters of Raw_Code__c
  • Code_Suffix__c — returns the last 2 characters of Raw_Code__c

Functions to Use

  • UPPER(text) / LOWER(text)
  • LEFT(text, num_chars) / RIGHT(text, num_chars)
  • MID(text, start_num, num_chars)
  • LEN(text)
Approach
  • 1UPPER(Raw_Code__c) converts the entire string to uppercase.
  • 2LEFT(Raw_Code__c, 2) gets the first two characters.
  • 3RIGHT(Raw_Code__c, 2) gets the last two characters.
Easy Formula Field

7. Number Functions: FLOOR, CEILING, ABS, MOD, MAX, MIN

Problem #311 · Salesforce Apex Coding Challenge

Problem Statement

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__c
  • Abs_Score__c — the absolute value of Raw_Score__c
  • Remainder__c — remainder when Raw_Score__c is divided by 7

Functions

  • FLOOR(number) — round down to nearest integer
  • CEILING(number) — round up to nearest integer
  • ABS(number) — absolute value
  • MOD(number, divisor) — remainder after division
  • MAX(n1, n2, ...) / MIN(n1, n2, ...)
Approach
  • 1FLOOR(Raw_Score__c) rounds down to the nearest integer.
  • 2ABS(Raw_Score__c) returns the non-negative value.
  • 3MOD(Raw_Score__c, 7) gives the remainder when divided by 7.

Halfway there — solve them live

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 →
Medium Formula Field

8. PRIORVALUE: Prevent Reducing Opportunity Amount

Problem #312 · Salesforce Apex Coding Challenge

Problem Statement

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:

  • The record is being edited (not newly created)
  • The new Amount is less than the previous value
  • The previous Amount was not null

Key Function

  • PRIORVALUE(field) — returns the field's value before the current edit
  • ISNEW() — returns TRUE if the record is being created for the first time
Approach
  • 1Use NOT(ISNEW()) to make sure the rule only applies on edits.
  • 2PRIORVALUE(Amount) gives the Amount before the current save.
  • 3Combine conditions: NOT(ISNEW()) AND Amount < PRIORVALUE(Amount) AND NOT(ISNULL(PRIORVALUE(Amount))).
Medium Formula Field

9. REGEX: Validate Phone Number Format

Problem #313 · Salesforce Apex Coding Challenge

Problem Statement

Write 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.

Key Function

  • REGEX(text, pattern) — returns TRUE if text matches the regex pattern
  • NOT(REGEX(...)) — returns TRUE when it does NOT match (use in validation)
Approach
  • 1The regex for (XXX) XXX-XXXX is: \(\d{3}\) \d{3}-\d{4}
  • 2Use NOT(REGEX(Phone, pattern)) so the rule fires on mismatch.
  • 3Wrap in AND(NOT(ISBLANK(Phone)), NOT(REGEX(...))) to only validate non-blank values.
Medium Formula Field

10. Compound Logical: OR, AND, NOT for Lead Qualification

Problem #314 · Salesforce Apex Coding Challenge

Problem Statement

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:

  • AnnualRevenue ≥ 1,000,000
  • NumberOfEmployees ≥ 200
  • Industry = "Technology"
Approach
  • 1Use NOT(IsConverted) to exclude converted leads.
  • 2Wrap the three criteria in OR(condition1, condition2, condition3).
  • 3Combine with AND(NOT(IsConverted), OR(...)).
Medium Formula Field

11. ISPICKVAL: Conditional Logic on Picklist Fields

Problem #315 · Salesforce Apex Coding Challenge

Problem Statement

An Opportunity has a picklist field StageName.

Create a formula field Stage_Group__c (Text) that maps stages to groups:

  • "Prospecting" or "Qualification" → "Early"
  • "Needs Analysis" or "Value Proposition" or "Id. Decision Makers" → "Mid"
  • "Perception Analysis" or "Proposal/Price Quote" or "Negotiation/Review" → "Late"
  • "Closed Won" → "Won"
  • "Closed Lost" → "Lost"

Key Function

  • ISPICKVAL(field, value) — returns TRUE if picklist field equals the given value
Approach
  • 1You cannot use = to compare picklist values — you must use ISPICKVAL().
  • 2Nest IF() statements or use a CASE() expression for multiple stages.
  • 3OR(ISPICKVAL(StageName, "Prospecting"), ISPICKVAL(StageName, "Qualification")) handles the Early group.
Medium Formula Field

12. Date Arithmetic: DATEVALUE, DATE, YEAR, MONTH, DAY

Problem #316 · Salesforce Apex Coding Challenge

Problem Statement

A Contract object has a StartDate (Date) field.

Create two formula fields:

  • Contract_Year__c (Number) — the year portion of StartDate
  • Anniversary_Date__c (Date) — the date exactly one year after StartDate using DATE(YEAR(StartDate)+1, MONTH(StartDate), DAY(StartDate))

Date Functions

  • YEAR(date) / MONTH(date) / DAY(date)
  • DATE(year, month, day) — construct a date from parts
  • DATEVALUE(text_or_datetime) — convert to Date
  • TODAY() — today's date
Approach
  • 1YEAR(StartDate) extracts just the year as a number.
  • 2DATE(YEAR(StartDate)+1, MONTH(StartDate), DAY(StartDate)) shifts the year by 1.
  • 3No DATEVALUE needed here since StartDate is already a Date (not DateTime).
Hard Formula Field

13. VLOOKUP: Auto-Fill Fields from a Custom Metadata Object

Problem #317 · Salesforce Apex Coding Challenge

Problem Statement

You 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__c
  • Lookup_Currency__c — the Currency_Symbol__c from the same lookup

Key Function

  • VLOOKUP(field_to_return, lookup_field, lookup_value)
Approach
  • 1VLOOKUP syntax: VLOOKUP($ObjectType.Region_Config__c.Fields.Tax_Rate__c, $ObjectType.Region_Config__c.Fields.Region_Code__c, Region__c)
  • 2The first argument is the field you want to return.
  • 3The second argument is the lookup key field on the target object.
  • 4The third argument is the value on the current record to match against.
Hard Formula Field

14. Advanced CASE() + Nested IF: SLA Tier Calculator

Problem #318 · Salesforce Apex Coding Challenge

Problem Statement

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:

  • If Priority = "High" AND Account.Industry = "Healthcare" → "Platinum"
  • If Priority = "High" → "Gold"
  • If Priority = "Medium" AND Account.Industry = "Technology" → "Silver"
  • If Priority = "Medium" → "Bronze"
  • Otherwise → "Standard"

Also create SLA_Hours__c (Number) using CASE() to map Priority to response hours: High→4, Medium→8, Low→24.

Approach
  • 1For SLA_Tier__c, use nested IF() since you need compound AND conditions.
  • 2ISPICKVAL(Priority, "High") is required — you cannot use Priority = "High" for picklist.
  • 3Cross-object formula: Account.Industry accesses the parent Account's Industry.
  • 4For SLA_Hours__c, CASE(Priority, "High", 4, "Medium", 8, "Low", 24, 0) is clean but picklist requires CASE with ISPICKVAL or nested IF.

Practice All 14 Problems Free

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 Problems
More Practice Topics
About ApexArena

ApexArena 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