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 id is specified.

To create contact and relate to existing account:

Account a = [select name from account where name like ‘sh%’];

Contact c = new Contact(lastname=’Again’, Account_id=a.id);

insert c;

Our Recent Blog

Share This Post