SHARE
TWEET

Untitled

a guest Apr 26th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public with sharing class WeekSixHomework {
  2.  
  3.     public static void soqlPractice() {
  4.  
  5.         //1. Below is a SOQL query that should be returning the top 5 Accounts in our org based on Annual Revenue.
  6.         //Something's not quite right, can you fix the query?
  7.         List<Account> topFiveAccounts = [SELECT Id, Name, AnnualRevenue
  8.                                          FROM Account
  9.                                          WHERE AnnualRevenue != 0
  10.                                          LIMIT 5];
  11.  
  12.         System.debug('This should be 5: ' + topFiveAccounts.size());
  13.  
  14.  
  15.         //2. Here is a query that is missing something.  It compiles, but if you try and run this method in Anonymous
  16.         //you'll get an error when the method tries to use the query results.  Fix it! :)
  17.  
  18.         List<Contact> contacts = [SELECT FirstName, LastName, MailingState
  19.                                   FROM Contact
  20.                                   LIMIT 10];
  21.  
  22.         for (Contact c : contacts) {
  23.             String name = c.FirstName + ' ' + c.LastName;
  24.         }
  25.  
  26.  
  27.         //3.  Can you write a SOQL query from scratch that will return the top 10 Accounts in the org, ordered by annual
  28.         //revenue in decending order?  Print your results in the debug log.
  29.         List<Account> topTenAccts = [SELECT Name, AnnualRevenue
  30.                                      FROM Account
  31.                                      ORDER BY AnnualRevenue DESC
  32.                                      LIMIT 10];
  33.        
  34.  
  35.         System.debug('Top 10 Accounts by Annual Revenue: ' + topTenAccts);
  36.        
  37.         //4.  Can you write a SOQL query that will return all opportunities for all accounts in the topFiveAccounts list that we used
  38.         //in Number 1?  (topFiveAccounts) Hint:  If you're stuck, look back a the code in WeekSixClassExercises, getOpenOppsForHotAccounts method
  39.         //  Print your results in the debug log.
  40.        
  41.         List<Opportunity> opptsForTopAccounts = [SELECT Id, Name, Amount
  42.                                                  FROM Opportunity
  43.                                                  WHERE AccountID in:topFiveAccounts];
  44.         System.debug('Opps from Top 5 Accounts: ' + opptsForTopAccounts);
  45.  
  46.     }
  47.      
  48.  
  49.     public static void forTheLoveOfForLoops() {
  50.  
  51.         //1. Take a look at the list and loop below.  It's commented out since it can't run as is.
  52.         // Can you replace the ?? with the number that makes sense based on the comments?
  53.         // Remove the slashes and compile.
  54.         // Can you add an extra counter variable so that you can print out how many times the loop ran in total?
  55.  
  56.  
  57.         //This loop should run 5 times
  58.         Integer counterVariable = 0;
  59.         for (Integer i=0; i<5; i++) {
  60.         System.debug('i is now: '+i);
  61.         }
  62.  
  63.  
  64.         //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
  65.         //each account name in the debug log when you're done.
  66.         //Use the list size to tell you how many loops, and use indexing to fetch values.  If you need help, check the
  67.         //loopingThroughLists method in WeekSixClassExercises for hints
  68.  
  69.         List<Account> accountList = [SELECT Id, Name FROM Account LIMIT 5];
  70.  
  71.         for(Integer i=0; i<accountList.size(); i++){
  72.         System.debug('Account Name: ' + accountList[i].name);            
  73.         }
  74.     }
  75. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top