Skip to content

Instantly share code, notes, and snippets.

@asmitakhare
Last active September 1, 2020 01:06
Show Gist options
  • Save asmitakhare/1c31f865ae10ce81a3cf330303fbd4a9 to your computer and use it in GitHub Desktop.
Save asmitakhare/1c31f865ae10ce81a3cf330303fbd4a9 to your computer and use it in GitHub Desktop.
Asmita WeekThreeHomework.apxc
public with sharing class WeekThreeHomework{
public static void homeworkAssignmentMethod() {
//Read through the setup below and then complete the code following the prompts. When you're done, make sure to compile (save) your work
//Open Execute Anonymous in the Developer Console and execute your code by typing in: WeekThreeHomework.homeworkAssignmentMethod();
//Read through the debug statements to make sure you're done your work correctly.
//************************************************************************************************************
// 1. Add two more whole numbers to this list using .add()
List<Integer> numberList = new List<Integer>{ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 };
numberList.add(37);
numberList.add(41);
//Checking our work:
System.debug('This number should be 12: ' + numberList.size());
//************************************************************************************************************
// 2. Create a new Lead and insert it in the database. (If you're stuck, look back at the WeekThreeClassExercises class for an example of creating a new SObject Record)
//You can give it any values you like, but remember that last name and Company are required fields (both are simple text fields.)
Lead l = new Lead();
l.LastName = 'Portually';
l.Company = 'HP';
//Checking our work:
insert l;
System.debug('We should see One DML was executed: ' + Limits.getDMLRows());
//************************************************************************************************************
//3. For the loop that is commented out below, update the while condition by replacing the ?? so that the loop runs 5 times
//delete the slashes so that the loop is no longer commented out and compile the class.
//Can you add a debug statement to print out the counter value every time it runs through the loop?
Integer counter = 0;
while (counter < 5) {
counter++; //without this line to increment counter, we'd have an infinite loop!
//}
System.debug('Done with the loop, it ran: ' + counter);
}
}
public static void forLoopsExercise(){
//1. Let's review iterating over a list of stuff. How about Strings?
List < String > myStringList = new List < String >{'red', 'yellow', 'green', 'blue'};
for(String st : myStringList){
System.debug('loop over the list of strings:' +st);
}
// 2. How about some SObjects?
Opportunity opp = new Opportunity();
opp.Name = 'Dunkin';
opp.StageName = 'process';
System.debug('New Opportunity values:' +opp);
// I'll do the data setup here to create a list of contacts that all have a first name:
Contact c1 = new Contact(FirstName='Sam', LastName='Laguiny' );
Contact c2 = new Contact(FirstName='Diane', LastName='Bernardi');
Contact c3 = new Contact(FirstName='Coach', LastName='Huge');
List<Contact> myContacts = new List<Contact> {c1,c2,c3};
// Your turn! Using a for loop, print out the first name of each contact on a different line
//hint, you will need to use dot notation!
for(Contact co : myContacts){
System.debug('looping through contact list:' +co.FirstName);
}
//Bonus question to ponder: Do these contacts actually exist in our database? no
//What would it take to get them there?
insert myContacts;
}
}
@chrisyojay
Copy link

Good job! Also appreciating that you are getting things in so early. Please let me know if you've got any questions

@chrisyojay
Copy link

Good job with the update-- addressed the things I called out!

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