Skip to content

Instantly share code, notes, and snippets.

@radwomenunazhang
Last active October 10, 2020 06:39
Show Gist options
  • Save radwomenunazhang/2b28f282c0e59ef41bd0b6169673a972 to your computer and use it in GitHub Desktop.
Save radwomenunazhang/2b28f282c0e59ef41bd0b6169673a972 to your computer and use it in GitHub Desktop.
public with sharing class WeekEightHomework {
//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 void createSampleAccounts(Integer numberOfAccounts) {
List<Account> acctList = new List<Account>();
for (Integer i = 0; i < numberOfAccounts; i++) {
Account a = new Account();
a.Name = 'Sample Account'+(i+1);
}
insert acctList;
List<Case> casesToInsert = new List<Case>();
for (Account a: acctList) {
Case c = new Case();
c.Status = 'New';
c.Origin = 'New Account';
c.AccountId = a.Id;
c.Subject = 'New Case';
c.Description ='Follow Up';
casesToInsert.add(c);
}
if(casesToInsert.size()>0){
insert casesToInset;
}
}
@patmcclellan
Copy link

Hi Una, thanks for getting your homework in early. There are 2 issues you need to fix.

  1. All of the accounts you create will have the same name. You need to use the variable i to make them unique. Remember how we use "NewAccount" + i ?
  2. If you run the code, you'll discover no Cases were inserted. You added them to the casesToInsert list... and then, you forgot your DML statement (outside the for-loop, of course).

@patmcclellan
Copy link

I just noticed one more issue. On line 18, you use i <= numberOfAccounts -- that should just be i < numberOfAccounts... otherwise you'll create 1 extra account.

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