Skip to content

Instantly share code, notes, and snippets.

@olgaloza
Created April 9, 2018 20:27
Show Gist options
  • Save olgaloza/6219aec3c3c7bed5c7811fd46203f1bc to your computer and use it in GitHub Desktop.
Save olgaloza/6219aec3c3c7bed5c7811fd46203f1bc to your computer and use it in GitHub Desktop.
HomeworkSeven();
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) {
//Putting new accounts intoa list to bulkify the code
List<Account> acctList = new List<Account>();
//Loop through and add Acconts:
for (Integer i = 0; i < numberOfAccounts; i++) {
System.debug('i = ' + i);
Account a = new Account();
a.Name = 'Sample Account ' + (i+1);
acctList.add(a);
}
//Inserting all in one action:
insert acctList;
System.debug('Inserted ' + acctList.size() + ' accounts.');
//Now add Cases for each of the newly created Accounts
//Create a list
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 for Hmwk 7';
c.Description = 'Week 7 homework Case';
casesToInsert.add(c);
}
//Add the List all at once
insert casesToInsert;
System.debug('Inserted ' + casesToInsert.size() + ' cases.');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment