Creating, Reading, Updating, And Deleting Records In Salesforce Apex (CRUD Operation In Salesforce)

Introduction

If we want to save records in the database on a permanent basis, we use DML statements in salesforce. We have multiple DML statements through which we can insert, update, delete or undelete records in the Salesforce database.

Some are mentioned below:

1. Insert

2. Delete

3. Update

4. Undelete

INSERT Operation

It is used to insert new records into any object. In this, row identification is not required due to the insertion of records.

Syntax

DMLStatement sObjectName;

Insert sObjectName;

Example

Here I want to insert Account record with Account name as ‘New Account’ and Industry as chemical.

Account acc = new Account();

acc.Name = ‘New Account’;

acc.Industry = ‘Chemicals’;

insert acc;

UPDATE Operation

It is used to modify the existing data or records. Row identification is required since we are updating the records. So, we need to identify which record to update.

Syntax

Update sObjectName;

Example

Update the type to ‘prospect’ and Industry as ‘Banking’ of the Account object.

acc.type = ‘Prospect’;

acc.Industry = ‘Banking’;

update acc;

DELETE Operation

It is used to delete the existing records present in our salesforce org.

Once the record is deleted, it will go to recycle bin for 15 days. Within 15 days, we can restore our records, after that, they will be no more present in our org.

Syntax

delete sObjectName;

Example

delete acc;

The record will be deleted and stored in the recycle bin.

 

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.