Ad Code

✨🎆 JOIN MERN, JAVA, PYTHON, AI, DEVOPS, SALESFORCE Courses 🎆✨

Get 100% Placement Oriented Program CLICK to new more info click

SOQL Queries and CRUD Operations in Salesforce by Shiva Sir

SOQL Queries and CRUD Operations in Salesforce

This tutorial explains how to perform CRUD (Create, Read, Update, Delete) operations in Salesforce using SOQL and Apex DML on a custom object Branch__c.


Custom Object Used

  • Object Name: Branch__c
  • Field Name: Branchname__c (Text)

What is SOQL?

SOQL (Salesforce Object Query Language) is used to retrieve data (SELECT only) from Salesforce objects.

Important:
SOQL does NOT support Insert, Update, or Delete.
These operations are performed using Apex DML.

1. CREATE (Insert Record) – Using Apex DML

To insert a new record into Branch__c, use the insert DML statement.

Example: Insert a Branch Record

Branch__c br = new Branch__c();
br.Branchname__c = 'Indore Branch';

insert br;

✔ This will create a new Branch record with the name Indore Branch.


2. READ (Select Records) – Using SOQL

To retrieve data from Salesforce, we use the SELECT SOQL query.

Example 1: Fetch All Branch Records

SELECT Id, Branchname__c
FROM Branch__c

Example 2: Fetch Branch by Name

SELECT Id, Branchname__c
FROM Branch__c
WHERE Branchname__c = 'Indore Branch'

Example 3: Fetch Limited Records

SELECT Id, Branchname__c
FROM Branch__c
LIMIT 5

3. UPDATE (Modify Existing Record) – Using Apex DML

To update a record, first fetch it using SOQL and then use the update statement.

Example: Update Branch Name

Branch__c br = [
    SELECT Id, Branchname__c
    FROM Branch__c
    WHERE Branchname__c = 'Indore Branch'
    LIMIT 1
];

br.Branchname__c = 'Indore Main Branch';

update br;

✔ The branch name will be updated successfully.


4. DELETE (Remove Record) – Using Apex DML

To delete a record, retrieve it using SOQL and use the delete statement.

Example: Delete a Branch Record

Branch__c br = [
    SELECT Id
    FROM Branch__c
    WHERE Branchname__c = 'Indore Main Branch'
    LIMIT 1
];

delete br;

✔ The selected Branch record will be deleted from Salesforce.


CRUD Summary Table

Operation Technology Used Keyword
Create Apex DML insert
Read SOQL SELECT
Update Apex DML update
Delete Apex DML delete

Conclusion

✔ SOQL is used only for fetching data (SELECT).
✔ Insert, Update, and Delete operations are done using Apex DML.
✔ CRUD operations are commonly used in Apex classes, triggers, and controllers.

Post a Comment

0 Comments