Skip to content

Instantly share code, notes, and snippets.

@asmitakhare
Created September 21, 2020 18:18
Show Gist options
  • Save asmitakhare/dff8c372a3443a0722e60f0a567ee29d to your computer and use it in GitHub Desktop.
Save asmitakhare/dff8c372a3443a0722e60f0a567ee29d to your computer and use it in GitHub Desktop.
Asmita Week 6 Homework.apxc
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('Frist Name and Last Name:' + 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> accList = [SELECT Id, Name, Type, AnnualRevenue FROM Account WHERE AnnualRevenue != null ORDER BY AnnualRevenue Desc LIMIT 10];
for (Integer i = 0; i < accList.size(); i++) {
System.debug('List of '+i+' Accounts in Descending order:' + accList[i]);
}
//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> allOppWithAcc = [SELECT Name, Id, Amount, StageName FROM OPPORTUNITY WHERE AccountId in :topFiveAccounts];
}
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
for (Integer i=0, count=1; i<5; i++) {
System.debug('i is now: '+i+ ', count is now: ' + count);
++count;
}
//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 WeekThree for hints
List<Account> accountList = [SELECT Id, Name FROM Account LIMIT 5];
for (Account a : accountList) {
System.debug('Account Name: ' + a.Name);
}
//Traditional new for loop
for (Integer i = 0; i < 5; i++){
System.debug('List size:' + i);
}
}
}
@chrisyojay
Copy link

Asmita, I can see you're having fun again with ln 47! I interpreted the instructions as printing out the total number of times the loop ran in the end, outside of the for-loop but this makes sense as well. What would you add to your code if you did want to use count outside of the loop?

@chrisyojay
Copy link

chrisyojay commented Sep 21, 2020

ln 64- I think you just want to iterate through the accountList to print out the names and not just 5 times with the traditional for-loop
ln 7- it's a bit up to interpretation, but to get the top annual revenue accounts, how would you update your query (you did it right below)?
ln 34- best to capitalize 'IN' for readability
ln 35- you forgot to print it :)

@chrisyojay
Copy link

Keep it up! I always see something different or new from you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment