Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 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 FROM Account WHERE AnnualRevenue !=0 ORDER BY AnnualRevenue
  8. DESC 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. }
  21.  
  22.  
  23. //3. Can you write a SOQL query from scratch that will return the top 10 Accounts in the org, ordered by annual
  24. //revenue in decending order? Print your results in the debug log.
  25.  
  26. List<Account> topTenAccounts = [Select ID, Name, AnnualRevenue FROM Account WHERE AnnualRevenue !=0 ORDER BY AnnualRevenue DESC
  27. LIMIT 10 ];
  28.  
  29.  
  30. //4. Can you write a SOQL query that will return all opportunities for all accounts in the topFiveAccounts list that we used
  31. //in Number 1? (topFiveAccounts) Hint: If you're stuck, look back a the code in WeekSixClassExercises, getOpenOppsForHotAccounts method
  32. // Print your results in the debug log.
  33.  
  34. List<Opportunity> topFiveOpportunities = [Select ID, Name From Opportunity WHERE AccountId in:topFiveAccounts];
  35. System.debug('Here is the list of Opportunities'+ topFiveOpportunities);
  36.  
  37. }
  38.  
  39. public static void forTheLoveOfForLoops() {
  40.  
  41. //1. Take a look at the list and loop below. It's commented out since it can't run as is.
  42. // Can you replace the ?? with the number that makes sense based on the comments?
  43. // Remove the slashes and compile.
  44. // Can you add an extra counter variable so that you can print out how many times the loop ran in total?
  45.  
  46. Integer myCounter = 0;
  47.  
  48. //This loop should run 5 times
  49. for (Integer i=0; i<5; i++) {
  50. System.debug('i is now: '+i);
  51. myCounter++;
  52.  
  53. }
  54.  
  55.  
  56. //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
  57. //each account name in the debug log when you're done.
  58. //Use the list size to tell you how many loops, and use indexing to fetch values. If you need help, check the
  59. //loopingThroughLists method in WeekSixClassExercises for hints
  60.  
  61. List<Account> accountList = [SELECT Id, Name FROM Account LIMIT 5];
  62.  
  63. for (Account a : accountList) {
  64.  
  65. for (Integer i = 0; i < accountList.size(); i++){
  66. System.debug('Account Name: ' + a.name);
  67. }
  68. }
  69.  
  70.  
  71.  
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement