ApexSensei

Drills Apex basics

Lists of Opportunities

A List<Opportunity> keeps records in order: add puts one on the end, [i] reads position i starting at 0, and size() counts them.

Your task

Put two Opportunities in a List, change one through the List, and read items only at safe positions.

List<Opportunity> opportunities = new List<Opportunity>();
Opportunity renewal = new Opportunity(
    Name = 'Renewal', StageName = 'Prospecting', CloseDate = Date.newInstance(2027, 6, 30)
);
Opportunity expansion = new Opportunity(
    Name = 'Expansion', StageName = 'Prospecting', CloseDate = Date.newInstance(2027, 9, 30)
);

// Pick at 1
Integer requestedIndex = 1;
String picked = null;

// Pick at 2
requestedIndex = 2;
picked = null;

// Pick at -1
requestedIndex = -1;
picked = null;

Requirements

  1. First name: Renewal should be printed
  2. List size: 2 should be printed
  3. Renewal stage: Qualification should be printed
  4. Pick at 1: Expansion should be printed
  5. Pick at 2: none should be printed
  6. Pick at -1: none should be printed

Sign in to open this drill