SHARE
TWEET

Untitled

a guest Oct 21st, 2019 62 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.        
  8.         List<Account> topFiveAccounts = [SELECT Id, Name, AnnualRevenue FROM Account WHERE AnnualRevenue != 0 LIMIT 5];
  9.  
  10.         System.debug('This should be 5: ' + topFiveAccounts.size());
  11.  
  12.  
  13.         //2. Here is a query that is missing something.  It compiles, but if you try and run this method in Anonymous
  14.         //you'll get an error when the method tries to use the query results.  Fix it! :)
  15.  
  16.         List<Contact> contacts = [SELECT FirstName, LastName, MailingState FROM Contact LIMIT 10];
  17.  
  18.         for (Contact c : contacts) {
  19.             String name = c.FirstName + ' ' + c.LastName;
  20.             System.debug('Names are ' + name);
  21.         }
  22.  
  23.  
  24.         //3.  Can you write a SOQL query from scratch that will return the top 10 Accounts in the org, ordered by annual
  25.         //revenue in decending order?  Print your results in the debug log.
  26.        
  27.         List<Account> topTenAccounts = [SELECT Id, Name, AnnualRevenue FROM Account WHERE AnnualRevenue !=null ORDER BY AnnualRevenue DESC LIMIT 10];
  28.         Integer loopCounter = 0;
  29.        
  30.         //show results in order with revenue to verify SOQL
  31.         For (Account a : topTenAccounts){    
  32.             loopCounter++;
  33.             System.debug(loopCounter + ' Account = ' + a.Name +', Revenue = ' + a.AnnualRevenue);
  34.         }
  35.            
  36.        
  37.  
  38.  
  39.         //4.  Can you write a SOQL query that will return all opportunities for all accounts in the topFiveAccounts list that we used
  40.         //in Number 1?  (topFiveAccounts) Hint:  If you're stuck, look back a the code in WeekSixClassExercises, getOpenOppsForHotAccounts method
  41.         //  Print your results in the debug log.
  42.         List<Opportunity> allOpps = [SELECT Id,Name FROM Opportunity WHERE AccountID in:topFiveAccounts];
  43.             System.debug('Number of Top Accounts: ' + topFiveAccounts.size() + ', Number of Top Account Opportunities: ' + allOpps.size());
  44.             For (Opportunity o: allOpps){
  45.                 System.debug('Opportunities for Top Five Accounts include: ' + o.Name);
  46.             }
  47.        
  48.  
  49.     }
  50.  
  51.     public static void forTheLoveOfForLoops() {
  52.  
  53.         //1. Take a look at the list and loop below.  It's commented out since it can't run as is.
  54.         // Can you replace the ?? with the number that makes sense based on the comments?
  55.         // Remove the slashes and compile.
  56.         // Can you add an extra counter variable so that you can print out how many times the loop ran in total?
  57.  
  58.  
  59.         //This loop should run 5 times
  60.         Integer counter = 0;
  61.         for (Integer i=0; i<5; i++) {
  62.             counter++;
  63.             //System.debug('i is now: '+i);
  64.             //System.debug('Loop ran ' + counter + ' times');  
  65.         }
  66.         System.debug('Done with loop, it ran ' + counter + ' times.');
  67.  
  68.  
  69.         //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
  70.         //each account name in the debug log when you're done.
  71.         //Use the list size to tell you how many loops, and use indexing to fetch values.  If you need help, check the
  72.         //loopingThroughLists method in WeekSixClassExercises for hints
  73.  
  74.         List<Account> accountList = [SELECT Id, Name FROM Account LIMIT 5];
  75.         for (Account a : accountList) {
  76.             System.debug('Account Name: ' + a.name);
  77.         }
  78.        
  79.         Integer listSize = accountList.size();
  80.         System.debug('...........................Check out the same loop with a different syntax.....................');
  81.         for (Integer i = 0; i < listSize; i++){
  82.             System.debug('Opp in our new syntax: ' + accountList[i].Name);
  83.         }
  84.        
  85.         //But we can also use the new syntax we just learned, using the size of the list to know how many loops we need.
  86.         //Integer listSize = tenOpenOpps.size();
  87.         //System.debug('*************************************************And Now for the same loop with different Syntax:');
  88.         //for (Integer i = 0; i < listSize; i++) {
  89.             //Note that since we're usng this format, we use indexing to get values from each opportunity,
  90.             //Using the tenOpenOpps list and the current value of i as the index
  91.             //System.debug('Opp in our loop Version 2: ' + tenOpenOpps[i].Name);
  92.         }
  93.  
  94.  
  95.     }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top