This course is currently a work in progress. Please join the waitlist to receive regular updates.

Insert statement in apex

The DML insert statement is used to create new records in the Salesforce database. To create new records, you first create an instance of the object and populate the relevant field values. Then, use the DML insert statement to add the records to the database.

If the operation is successful, the system automatically generates a record ID. It is always recommended to catch any exceptions that occur during a DML operation to handle unexpected system behavior.

Insert

Add a new record into database

Here is an example of an insert statement to create a new account record in Salesforce.

Loading...

In the above example:

  • The variable accRecord is used to create an instance of Account object.
  • The name is populated with a string value 'Decodeforce'.
  • The DML statement Insert is used to insert the account record.

Note


Bulk operations are always recommended when performing DML operations, as they help save governor limits.

Try It Yourself

Loading...