Free Salesforce Developers Tutorials >

Chapter 8 - Apex DML and Database Methods >

Insert | APEX DML Standalone Statements

Insert | APEX DML Standalone Statements

What You’ll Learn


tickInsert opertion in Salesforce

ticExamples of Insert operation in Salesforce

S2 Labs

Apex Insert

To create a new record, you need to use the Insert statement.

Example 1:

public static void main() {
    Account a = new Account(name = ’Shubham’, numberOfEmployees = 150);
    insert a;
}

Example 2:

List < Contact > newConList = new List < Contact > ();
List < Contact > conList = [SELECT firstname, lastname FROM Contact];
for (Contact cc: conList) {
    Contact c1 = new Contact(lastname = cc.lastname, firstname = cc.firstname);
    insert c1; // too many DML statements, hence Limit Reached
}
for (Contact cc: conList) {
    Contact c1 = new Contact(lastname = cc.lastname, firstname = cc.firstname);
    newConList.add(c1);
}
insert newConList; // only 1 DML Statement

Note:

We cannot insert a record in which the id is specified.

To create contact and relate to existing account:

Account a = [SELECT Name FROM Account WHERE Name like‘ sh % ’LIMIT 1];
Contact c = new Contact(lastName = ’Again’, AccountId = a.id);
insert c;
salesforce-developer

Download Study Material

Get access to exclusive study material for Salesforce Certification and ace your exams!

Download Now

Our Salesforce Certification Courses

Hey there! Glad you made it through our Salesforce Developer Training for beginners . But wait! We've got some high-in-demand Salesforce courses for you to take your Salesforce skills to the next level, making you a desired professional in the Salesforce job market.

Post a Comment

Your email address will not be published. Required fields are marked *