You need to sign in to do that
Don't have an account?
Khaja Basheer
Inserting bulk related records
Can any one point the mistakes in my code. I'm looking to insert an account record and its related 20 opportunity records and 5 related case records to that account.
public class AccountHelper {
public static void AccountRecordHelper() {
List<Opportunity> lstopp = New List <opportunity> ();
List<Case> CaseNew = New List <Case> ();
Account Acc = New Account();
Acc.Name = 'Gen Construction';
Acc.Industry= 'Contruction';
insert acc;
If (acc.id != Null) {
for (Integer C = 1; C<=20; C++) {
Opportunity Opp = New Opportunity ();
Opp.Name = 'Dean Constructions' + C;
opp.CloseDate = system.today ();
opp.StageName = 'Prospecting';
opp.AccountId = acc.Id;
lstopp.add(opp);
insert lstopp;
}
Case Cs = New Case();
for (integer i = 1; i<=10; i++) {
Cs.Status = 'New';
Cs.Origin = 'Phone';
Cs.AccountId = acc.Id;
casenew.add(cs);
insert casenew;
}
System.debug (lstopp);
system.debug(casenew);
system.debug(acc.id);
}
}
}
public class AccountHelper {
public static void AccountRecordHelper() {
List<Opportunity> lstopp = New List <opportunity> ();
List<Case> CaseNew = New List <Case> ();
Account Acc = New Account();
Acc.Name = 'Gen Construction';
Acc.Industry= 'Contruction';
insert acc;
If (acc.id != Null) {
for (Integer C = 1; C<=20; C++) {
Opportunity Opp = New Opportunity ();
Opp.Name = 'Dean Constructions' + C;
opp.CloseDate = system.today ();
opp.StageName = 'Prospecting';
opp.AccountId = acc.Id;
lstopp.add(opp);
insert lstopp;
}
Case Cs = New Case();
for (integer i = 1; i<=10; i++) {
Cs.Status = 'New';
Cs.Origin = 'Phone';
Cs.AccountId = acc.Id;
casenew.add(cs);
insert casenew;
}
System.debug (lstopp);
system.debug(casenew);
system.debug(acc.id);
}
}
}
Hara Sahoo
Pls avoid DMLs inside the loop as a best practice. rest all looks good