Skip to content

Instantly share code, notes, and snippets.

@radwomenunazhang
Created September 22, 2020 05:33
Show Gist options
  • Save radwomenunazhang/529fe3ac95449cd1633cdb9db58d6372 to your computer and use it in GitHub Desktop.
Save radwomenunazhang/529fe3ac95449cd1633cdb9db58d6372 to your computer and use it in GitHub Desktop.
public with sharing class WeekSixHomework {
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?
List<Account> topFiveAccounts = [SELECT Id, Name, AnnualRevenue FROM Account
WHERE AnnualRevenue != NULL
ORDER BY AnnualRevenue DESC
LIMIT 5];
System.debug('This should be 5: ' + topFiveAccounts);
//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! :)
List<Contact> contacts = [SELECT FirstName, LastName, MailingState FROM Contact LIMIT 10];
for (Contact c : contacts) {
String name = c.FirstName + ' ' + c.LastName;
System.debug(name);
}
//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> accounts = [SELECT Name, AnnualRevenue FROM Account
WHERE AnnualRevenue !=NULL
ORDER BY AnnualRevenue DESC
LIMIT 10];
for(Account a : accounts){
System.debug(a.Name + ': Annual Revenue: $' + a.AnnualRevenue);
}
//4. Can you write a SOQL query that will return all opportunities for all accounts in the topFiveAccounts list that we used
//in Number 1? (topFiveAccounts) Hint: If you're stuck, look back a the code in WeekSixClassExercises, getOpenOppsForHotAccounts method
// Print your results in the debug log.
List<Opportunity> oppsofTopFiveAccounts = [SELECT Name, AccountId FROM Opportunity
WHERE AccountId IN :topFiveAccounts];
System.debug('Opps for Top Five Accounts: ' + oppsofTopFiveAccounts);
}
public static void forTheLoveOfForLoops() {
//1. Take a look at the list and 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
//for (Integer i=0; i<??; i++) {
// System.debug('i is now: '+i);
//}
integer counter = 0;
for (Integer i=0; i<5; i++) {
counter++;
System.debug('i is now: '+i);
System.debug('The loop has run '+ counter + ' times.');
}
System.debug('The loop ran a total of ' + counter + ' times.');
//2. Below is a loop that iterates through a list. Can you change it to use the new For Loop syntax? It should print out
//each account name in the debug log when you're done.
//Use the list size to tell you how many loops, and use indexing to fetch values. If you need help, check the
//loopingThroughLists method in WeekThree for hints
//Sorry, I have problem in getting this loop part done properly
List<Account> accountList = [SELECT Id, Name FROM Account LIMIT accountListSize];
for (Account a : accountList) {
System.debug('Account Name: ' + a.name);
for(integer i = 0; i<accountListSize; i++) {
Account a = accountList[accountCounter];
}
}
@patmcclellan
Copy link

Hey Una, good work this week. Everything is perfect up to the 2nd loop exercise... let's talk about that.

There are a couple of ways to loop through a list. You can either use the for(Account a : accountList) or you can use the standard For Loop syntax for(Integer i = 0; i < 10; i++). This exercise is asking you to use that second way of doing it. The problem is that often you'll have a list of Accounts (or whatever) and you won't know how many there are... so i < ??. In this situation, you can use the accountList.size() method inside your for loop. Let's say you get a list of accounts located in Texas -- you don't know how many there are, but you'll limit it to 100 just to be safe. So, now, you don't know how many items in your List.

List<Account> accountList = [SELECT Id, Name FROM Account WHERE BillingState = 'TX' LIMIT 100];
for(Integer i = 0; i < accountList.size(); i++){
     Account a = accountList[i]; // this pulls the accounts from the list starting with index 0 and incrementing each loop
    // do whatever you want with the account
    System.debug('This account is ' + a.Name + ' at index ' + i);
}

Make sense?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment