Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  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. ORDER BY AnnualRevenue DESC
  11. LIMIT 5];
  12.  
  13. System.debug('This should be 5: ' + topFiveAccounts.size());
  14.  
  15.  
  16. //2. Here is a query that is missing something. It compiles, but if you try and run this method in Anonymous
  17. //you'll get an error when the method tries to use the query results. Fix it! :)
  18.  
  19. List<Contact> contacts = [SELECT FirstName, LastName, MailingState FROM Contact LIMIT 10];
  20.  
  21. for (Contact c : contacts) {
  22. String name = c.FirstName + ' ' + c.LastName;
  23. }
  24.  
  25.  
  26. //3. Can you write a SOQL query from scratch that will return the top 10 Accounts in the org, ordered by annual
  27. //revenue in decending order? Print your results in the debug log.
  28. List<Account> topTenAccounts = [SELECT Id, Name, AnnualRevenue
  29. FROM Account
  30. WHERE AnnualRevenue !=0
  31. ORDER BY AnnualRevenue DESC
  32. LIMIT 10];
  33.  
  34. System.debug('Here are the 10: ' + toptenAccounts.size());
  35.  
  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> topFiveAccountsOpenOpps = [SELECT Id, Amount, StageName
  42. FROM Opportunity
  43. WHERE IsClosed = false
  44. AND AccountId in:topFiveAccounts];
  45.  
  46. //Let's see those opportunities!
  47. System.debug('Our Top Five Accounts Opportunities: ' + topFiveAccountsOpenOpps);
  48. }
  49.  
  50. public static void forTheLoveOfForLoops() {
  51.  
  52. //1. Take a look at the list and loop below. It's commented out since it can't run as is.
  53. // Can you replace the ?? with the number that makes sense based on the comments?
  54. // Remove the slashes and compile.
  55. // Can you add an extra counter variable so that you can print out how many times the loop ran in total?
  56.  
  57. Integer counter = 0;
  58. //This loop should run 5 times
  59. for (Integer i=0; i<5; i++) {
  60. System.debug('i is now: '+i);
  61. counter++;
  62. }
  63. System.debug('How many times the loop ran?' +counter);
  64.  
  65. //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
  66. //each account name in the debug log when you're done.
  67. //Use the list size to tell you how many loops, and use indexing to fetch values. If you need help, check the
  68. //loopingThroughLists method in WeekSixClassExercises for hints
  69.  
  70. List<Account> accountList = [SELECT Id, Name FROM Account LIMIT 5];
  71.  
  72. for (Account a : accountList) {
  73. System.debug('Account Name: ' + a.name);
  74. }
  75. Integer listSize = accountList.size();
  76. System.debug('The same loop with different Syntax:');
  77. for (Integer i = 0; i < listSize; i++) {
  78.  
  79. System.debug('Accounts in Version 2: ' + accountList[i].Name);
  80. }
  81.  
  82. }
  83.  
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement