Advertisement
Guest User

Untitled

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