function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Uday KUday K 

How to Bulkify trigger in this scenario

Hello All,


I have requirement like when ever case is inserting, i have to insert Account, Contact and assign the inserted account and Contact to  the case which is inserting. For this my approach is like below, But it wont work out because it is not getting Account id for inserting contact. How to achieve this?

I reuest anyone to give solution.

 

for (Case c : Trigger.New) // I am using before insert and Before Update
        {
            Account acc = New Account();
            acc.Name = c.SuppliedName;
            acc.Phone = c.SuppliedPhone;
            CreateAccount.add(acc);
            
            Contact con = New Contact();
            con.LastName = c.SuppliedName;
            con.AccountId = acc.Id;
            con.Phone = c.SuppliedPhone;
            con.Email = c.SuppliedEmail;
            CreateContact.add(con);
 

           c.AccountId = acc.Id;
            c.ContactId = con.Id;
         }
        insert CreateAccount; //List of Account
        insert CreateContact; //List of Contact

 

Thanks,

Uday

Bhawani SharmaBhawani Sharma
looks like this list will always create the same number of accounts and contacts. So you can go with following
insert CreateAccount; //Account list

for(Integer i = 0; i < CreateAccount.Size(); i++) {
CreateContact[i].AccountId = CreateAccount[i].Id;
}

insert CreateContact; // List of Contacts