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
Jayesh Deo 2Jayesh Deo 2 

insert bulk record practice

Write an apex program, to insert an Account Record, and 5 Related Contacts, and 4 Related Opportunities, and 2 Related Case records inside the object.
Account record(1)
|
|_______contact records(5)
|
|_______Opportunity records(4)
|
|________Case records(2)
Helen BarrowHelen Barrow
Right at the start of the tip we learned that we can load data in a table or view, as shown in the syntax. So, let's create a view that has a schema identical to the source file, as shown below. I have named the view SalesView, so that during the load we can be sure that we are using the view to bulk load the data and not the actual table. https://www.mibridges.me/
saurabh singh 310saurabh singh 310
Here's an example of an Apex program that inserts an Account record and related Contact, Opportunity, and Case records:
public class InsertRelatedRecords {
    public static void insertRecords() {
        // Create the Account record
        Account newAccount = new Account(Name='Test Account');
        insert newAccount;

        // Create the related Contact records
        List<Contact> newContacts = new List<Contact>();
        for (Integer i = 0; i < 5; i++) {
            newContacts.add(new Contact(LastName='Test Contact ' + i, AccountId=newAccount.Id));
        }
        insert newContacts;

        // Create the related Opportunity records
        List<Opportunity> newOpportunities = new List<Opportunity>();
        for (Integer i = 0; i < 4; i++) {
            newOpportunities.add(new Opportunity(Name='Test Opportunity ' + i, AccountId=newAccount.Id));
        }
        insert newOpportunities;

        // Create the related Case records
        List<Case> newCases = new List<Case>();
        for (Integer i = 0; i < 2; i++) {
            newCases.add(new Case(Subject='Test Case ' + i, AccountId=newAccount.Id));
        }
        insert newCases;
    }
}

You can call this method insertRecords() to insert the records. The above class will insert a new Account record, five new Contact records related to the Account, four new Opportunity records related to the Account, and two new Case records related to the Account.
Jayesh Deo 2Jayesh Deo 2
great! Thank You!!