Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public with sharing class WeekSixHomework {
- public static void soqlPractice() {
- //1. Below is a SOQL query that should be returning the top 5 Accounts in our org based on Annual Revenue.
- //Something's not quite right, can you fix the query?
- List<Account> topFiveAccounts = [SELECT Id, Name, AnnualRevenue FROM Account WHERE AnnualRevenue != 0 LIMIT 5];
- System.debug('This should be 5: ' + topFiveAccounts.size());
- //2. Here is a query that is missing something. It compiles, but if you try and run this method in Anonymous
- //you'll get an error when the method tries to use the query results. Fix it! :)
- List<Contact> contacts = [SELECT FirstName, LastName, MailingState FROM Contact LIMIT 10];
- for (Contact c : contacts) {
- String name = c.FirstName + ' ' + c.LastName;
- System.debug('Names are ' + name);
- }
- //3. Can you write a SOQL query from scratch that will return the top 10 Accounts in the org, ordered by annual
- //revenue in decending order? Print your results in the debug log.
- List<Account> topTenAccounts = [SELECT Id, Name, AnnualRevenue FROM Account WHERE AnnualRevenue !=null ORDER BY AnnualRevenue DESC LIMIT 10];
- Integer loopCounter = 0;
- //show results in order with revenue to verify SOQL
- For (Account a : topTenAccounts){
- loopCounter++;
- System.debug(loopCounter + ' Account = ' + a.Name +', Revenue = ' + a.AnnualRevenue);
- }
- //4. Can you write a SOQL query that will return all opportunities for all accounts in the topFiveAccounts list that we used
- //in Number 1? (topFiveAccounts) Hint: If you're stuck, look back a the code in WeekSixClassExercises, getOpenOppsForHotAccounts method
- // Print your results in the debug log.
- List<Opportunity> allOpps = [SELECT Id,Name FROM Opportunity WHERE AccountID in:topFiveAccounts];
- System.debug('Number of Top Accounts: ' + topFiveAccounts.size() + ', Number of Top Account Opportunities: ' + allOpps.size());
- For (Opportunity o: allOpps){
- System.debug('Opportunities for Top Five Accounts include: ' + o.Name);
- }
- }
- public static void forTheLoveOfForLoops() {
- //1. Take a look at the list and loop below. It's commented out since it can't run as is.
- // Can you replace the ?? with the number that makes sense based on the comments?
- // Remove the slashes and compile.
- // Can you add an extra counter variable so that you can print out how many times the loop ran in total?
- //This loop should run 5 times
- Integer counter = 0;
- for (Integer i=0; i<5; i++) {
- counter++;
- //System.debug('i is now: '+i);
- //System.debug('Loop ran ' + counter + ' times');
- }
- System.debug('Done with loop, it ran ' + counter + ' times.');
- //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
- //each account name in the debug log when you're done.
- //Use the list size to tell you how many loops, and use indexing to fetch values. If you need help, check the
- //loopingThroughLists method in WeekSixClassExercises for hints
- List<Account> accountList = [SELECT Id, Name FROM Account LIMIT 5];
- for (Account a : accountList) {
- System.debug('Account Name: ' + a.name);
- }
- Integer listSize = accountList.size();
- System.debug('...........................Check out the same loop with a different syntax.....................');
- for (Integer i = 0; i < listSize; i++){
- System.debug('Opp in our new syntax: ' + accountList[i].Name);
- }
- //But we can also use the new syntax we just learned, using the size of the list to know how many loops we need.
- //Integer listSize = tenOpenOpps.size();
- //System.debug('*************************************************And Now for the same loop with different Syntax:');
- //for (Integer i = 0; i < listSize; i++) {
- //Note that since we're usng this format, we use indexing to get values from each opportunity,
- //Using the tenOpenOpps list and the current value of i as the index
- //System.debug('Opp in our loop Version 2: ' + tenOpenOpps[i].Name);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement