Created
November 13, 2025 09:00
-
-
Save JessWingerden/e9070f1a14f76e76f617ac7b83eb2669 to your computer and use it in GitHub Desktop.
Week 4 Homework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public with sharing class WeekFourHomework { | |
| public static void soqlPractice() { | |
| //1. Below is a SOQL query that should be returning the top 5 Accounts in our org based on Annual Revenue. | |
| //Something's not quite right, can you fix the query? | |
| //JW: No need for the ID as this is our freebee not limited and not sorted by top 5. | |
| List<Account> topFiveAccounts = [SELECT Name, AnnualRevenue FROM Account WHERE AnnualRevenue != 0 ORDER BY AnnualRevenue DESC Limit 5]; { | |
| System.debug('This should be 5: ' + topFiveAccounts.size()); | |
| } | |
| //2. Here is a query that is missing something. It compiles, but if you try and run this method in Anonymous | |
| //you'll get an error when the method tries to use the query results. Fix it! :) | |
| //JW: Was missing the LastName in retrieving Contact Fields | |
| List<Contact> contacts = [SELECT FirstName, LastName, MailingState FROM Contact LIMIT 10]; | |
| for (Contact c : contacts) { | |
| String name = c.FirstName + ' ' + c.LastName; | |
| } | |
| //3. Can you write a SOQL query from scratch that will return the top 10 Accounts in the org, ordered by annual | |
| //revenue in decending order? Print your results in the debug log. | |
| List<Account> topTenAccounts = [SELECT Name FROM Account ORDER BY AnnualRevenue DESC Limit 10]; { | |
| System.debug('Show me top 10 accounts: ' + topTenAccounts); | |
| } | |
| } | |
| public static void forTheLoveOfForLoops() { | |
| //1. Take a look at the loop below. It's commented out since it can't run as is. | |
| // Can you replace the ?? with the number that makes sense based on the comments? | |
| // Remove the slashes and compile. | |
| // Can you add an extra counter variable so that you can print out how many times the loop ran in total? | |
| //This loop should run 5 times. JW: ?? was replaced with 5. Counter variable = countedLoops | |
| Integer countedLoops = 0; | |
| for (Integer i=0; i<5; i++) { | |
| System.debug('i is now: '+i); | |
| countedLoops++; | |
| } | |
| {System.debug('Count of loops run:' + countedLoops); | |
| } | |
| //Brain teaser: Below is a loop that iterates through a list. Can you change it to use the traditional For Loop syntax? It should print out | |
| //each account name in the debug log when you're done. | |
| //Use the list size (accountList.size()) to tell you how many loops, and use indexing to fetch values (a[index]) | |
| /*List<Account> accountList = [SELECT Id, Name FROM Account LIMIT 5]; | |
| for (Account a : accountList) { | |
| System.debug('Account Name: ' + a.name); | |
| }*/ | |
| //JW: Updated to traditional loops | |
| List<Account> accountList = [SELECT Name FROM Account LIMIT 5]; | |
| for (Integer accountIndex = 0; accountIndex < accountList.size(); accountIndex++){ | |
| System.debug('Account Name: ' + accountList[accountIndex].Name); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Loved your comments in the code and it ran without a hitch! Keep up the great work. :)