Skip to content

Instantly share code, notes, and snippets.

@abbiebauer37
Created May 6, 2025 14:51
Show Gist options
  • Save abbiebauer37/7b386b99879fcbb6ca1e528a8cd8b334 to your computer and use it in GitHub Desktop.
Save abbiebauer37/7b386b99879fcbb6ca1e528a8cd8b334 to your computer and use it in GitHub Desktop.
public with sharing class WeekSevenHomework {
//Assignment: The following method is supposed to create the number of accounts specified in the argument that it takes.
//Each Account should be named 'Sample Account #sampleNumber' where sample number is the count. So if you created 2 Accounts
//one would be called 'Sample Account 1' and the other 'Sample Account 2'
//Also, when we're done inserting them, we will create a case for each one with a Status of 'New', Origin of 'New Account' and the
//desription and subject of your choice.
//This isn't working quite right. Can you use your debugging skills to fix it? I had to comment it out since it won't compile
//Look out for consistency and formatting too! (even if they don't break the code)
//Add comments to describe what the code is doing.
//After you get it to compile, run it in execute anonymous and check that it's really working! Look up your new accounts in your org!
public static void createSampleAccounts(Integer numberOfAccounts) { //changed the parameter from decimal to integer
List<Account> acctList = new List<Account>();
for (Integer i = 0; i < numberOfAccounts; i++) { //removed = from <= to ensure we only create up to the number of account specified
Account a = new Account();
a.Name = 'Sample Account ' + i; //changed string to Sample Account and added the number of accounts
acctList.add(a); //added the new account(s)
}
insert acctList;
System.debug('How many accounts did we add? ' + acctList.size());
List<Case> casesToInsert = new List<Case>();
for (Account a: acctList) { //removed the extra c from acctList
Case c = new Case();
c.Status = 'New'; // corrected the "paratheses" to a 'single quote' for a string
c.Origin = 'New Account'; //added ;
c.AccountId = a.Id; //added .Id to a
c.Subject = 'New Case'; // added = to assign Case Subject to 'New Case'
c.Description = 'Follow Up'; // Corrected the spelling of Description (was descritpion) and removed the == to be = for assignment
casesToInsert.add(c);
}
}
}
@CecileO
Copy link

CecileO commented May 7, 2025

That's really good Abbie!
Also you found the parameter to the method should be an integer, you're only the second person so far who found that sneaky mistake, so well done!

Just a couple of comments:

  • Are you sure your first account will be named "Sample Account 1" ;) ? Isn't there something that should be done to ensure it starts at 1, not 0? There's a couple of options by the way, not only 1 way to do this :)
  • Are you certain you are inserting the cases? Have you checked? I think there is a little something missing after you create the sobjects in the list.

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