SHARE
TWEET

Untitled

a guest Oct 14th, 2019 76 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 FROM Account WHERE AnnualRevenue != 0 ORDER BY AnnualRevenue DESC LIMIT 5];
  8.  
  9.         System.debug('This should be 5: ' + topFiveAccounts.size());
  10.        
  11.  
  12.         //2. Here is a query that is missing something.  It compiles, but if you try and run this method in Anonymous
  13.         //you'll get an error when the method tries to use the query results.  Fix it! :)
  14.  
  15.         List<Contact> contacts = [SELECT FirstName,LastName, MailingState FROM Contact LIMIT 10];
  16.  
  17.         for (Contact c : contacts) {
  18.             String name = c.FirstName + ' ' + c.LastName;
  19.         }
  20.  
  21.  
  22.         //3.  Can you write a SOQL query from scratch that will return the top 10 Accounts in the org, ordered by annual
  23.         //revenue in decending order?  Print your results in the debug log.
  24.         List<Account> AccountsNew = [SELECT Id, Name, AnnualRevenue FROM Account WHERE AnnualRevenue != 0 ORDER BY AnnualRevenue DESC];
  25.  
  26.         System.debug('This should be ' + AccountsNew);
  27.  
  28.  
  29.         //4.  Can you write a SOQL query that will return all opportunities for all accounts in the topFiveAccounts list that we used
  30.         //in Number 1?  (topFiveAccounts) Hint:  If you're stuck, look back a the code in WeekSixClassExercises, getOpenOppsForHotAccounts method
  31.         //  Print your results in the debug log.
  32.    
  33.         List<Opportunity> hotAccountsOpenOpps = [SELECT Id, Name FROM Opportunity WHERE AccountId in:topFiveAccounts];
  34.  
  35.         //Let's see those opportunities!
  36.         System.debug('Our Hot Account Opportunities: ' + hotAccountsOpenOpps);
  37.  
  38.  
  39.  
  40.     }
  41.  
  42.    
  43.  
  44.     public static void forTheLoveOfForLoops() {
  45.  
  46.         //1. Take a look at the list and loop below.  It's commented out since it can't run as is.
  47.         // Can you replace the ?? with the number that makes sense based on the comments?
  48.         // Remove the slashes and compile.
  49.         // Can you add an extra counter variable so that you can print out how many times the loop ran in total?
  50.  
  51.  
  52.         //This loop should run 5 times
  53.         integer J;
  54.         for (Integer i=0; i<6; i++) {
  55.            
  56.             J = i+1;
  57.           System.debug('i is now: '+i);
  58.            
  59.         }
  60.         System.debug('Number of loops' +J);
  61.  
  62.         //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
  63.         //each account name in the debug log when you're done.
  64.         //Use the list size to tell you how many loops, and use indexing to fetch values.  If you need help, check the
  65.         //loopingThroughLists method in WeekSixClassExercises for hints
  66.  
  67.         List<Account> accountList = [SELECT Id, Name FROM Account LIMIT 5];
  68.  
  69.        
  70.         for (Integer i = 0; i<accountList.size(); i++){
  71.            
  72.                Integer count = i+1;
  73.            
  74.             System.debug('Account'+count+': '+ accountList[i]);
  75.         }
  76.  
  77.  
  78.     }
  79.  
  80.  
  81. }
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