Notes
Apex code to retrieve Contact records including Id, First Name and Last Name.
List<Contact> conList =[SELECT Id FROM Contact WHERE CreatedDate =TODAY];
Apex code to retrieve Contact records created today.
List<Contact> conList =[SELECT Id FROM Contact WHERE CreatedDate =THIS_WEEK];
Apex code to create a new empty List of Contact objects.
List<Contact> conList = new List<Contact>();
Apex code to create a new Contact record with a specified Last Name and insert it into the database.
Contact con = new Contact();
con.LastName= 'Last Name';
insert con;
Apex code to create a new Contact record with a specified Last Name using the constructor and insert it into the database.
Contact con = New Contact(LastName='Last Name');
insert con;
Apex code to create a new Contact record with a specified Last Name using an anonymous Apex script and insert it into the database in a single statement.
insert new contact(LastName='Last Name');
Apex code to create a new List of Contact objects with specified field values.
List<Contact> conList = new List<Contact>{new Contact(LastName='Last Name')};
Apex code to create a new Contact object with a specified field value and add it to a List.
List<Contact> conList = new List<Contact>();
Contact con = New Contact();
con.LastName='Last Name';
conList.add(con);
Apex code to retrieve and display the number of Contact records in the database.
List<Contact> conList =[SELECT Id FROM Contact];
System.debug(conList.size());
Apex code to delete all Contact records retrieved by a SOQL query.
List<Contact> conList =[SELECT Id FROM Contact];
delete conList;
Apex code to delete all Contact records with a null AccountId field retrieved by a SOQL query.
List<Contact> conList =[SELECT Id FROM Contact WHERE AccountId=Null];
delete conList;
Apex code to delete Contact records with a null AccountId field using a loop and separate List object.
List<Contact> conList =[SELECT Id, AccountId FROM Contact];
List<Contact> conListtoDelete = new List<Contact>();
for(Contact con: conList){
if(con.AccountId ==Null){
conListtoDelete.add(con);
}
}
delete conListtoDelete;
List<Contact> conList =[SELECT Id FROM Contact WHERE LeadSource='Web'];
for(Contact con : conList){
con.LeadSource ='Phone Inquiry';
}
update conList;
List<Contact> conList =[SELECT Id, LeadSource FROM Contact];
for(Contact con : conList){
if(con.LeadSource =='Web'){
con.LeadSource ='Phone Inquiry';
}
}
update conList;
List<Contact> conList = new List<Contact>();
for(Integer i=1; i<=10; i++){
Contact con = new Contact();
con.LastName='Last Name ' + i;
conList.add(con);
}
insert conList;
List<Account> acList =[SELECT Id FROM Account];
List<Contact> conList = new List<Contact>();
for(Account ac : acList){
Contact con = new Contact();
con.LastName='Last Name Test';
con.AccountId=ac.id;
conList.add(con);
}
insert conList;
List<Contact> conList = [SELECT Id FROM Contact WHERE AccountId=Null];
for(Contact con : conList){
con.AccountId='0018d00000KPuiRAAT';
}
update conList;
List<Contact> conList =[SELECT Id, LastName FROM Contact];
for(Contact con : conList){
con.LastName=con.LastName.toUpperCase();
}
update conList;
List<Contact> conList =[SELECT Id, FirstName FROM Contact WHERE FirstName LIKE '%DELETE%'];
delete conList;
Account ac = new Account();
ac.Name='abc';
insert ac;
Contact con = new Contact();
con.LastName = 'xyz';
con.AccountId = ac.Id;
insert con;