Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save forcethesales/8399b9a009ccd0a001b04064da1e39d0 to your computer and use it in GitHub Desktop.
Save forcethesales/8399b9a009ccd0a001b04064da1e39d0 to your computer and use it in GitHub Desktop.
RAD women week 7 homework. insert accounts and connected cases
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() {
//declare a variable for the number of accounts I'd like to insert
Decimal numberOfAccounts = 3;
//create a list of the future accounts
List<Account> acctList = new List<Account>();
//set the integer i to 1, to skip over 0.
for (Integer i = 1; i <= numberOfAccounts; i++) {
//set the values of the account variables
Account a = new Account();
a.Name = 'Sample AccountAgain' + i;
system.debug('Sample Account' + i);
//add this account to the list
acctList.add(a);
}
//insert the whole list of accounts at once!
insert acctList;
//new list for the cases I want to insert
List<Case> casesToInsert = new List<Case>();
//for each account in the list..
for (Account a: acctList) {
//....create a new case
Case c = new Case();
//assign values to the case fields
c.Status = 'New';
c.Origin = 'New Account';
c.AccountId = a.Id;
c.Subject = 'New Case';
c.Description = 'Follow Up';
//add this case to the list
casesToInsert.add(c);
}
//insert the cases!
Insert casesToInsert;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment