Skip to main content
Khaja Basheer 님이 #Apex에 질문했습니다
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);

            

        } 

    }

}
답변 1개
  1. 2021년 8월 31일 오후 3:33
    Pls avoid DMLs inside the loop as a best practice. rest all looks good
0/9000