8

Insert | APEX DML Standalone Statements

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

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
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

Next Topic

Need more support?

Get a head start with our FREE study notes!

Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.

ServiceNow Stripe