Master the concepts of Salesforce in just 45 days Click here to know more.
8
10
58
27
Premium Salesforce Weekend Batch Start From Tomorrow.
Public static void main()
{
List<Contact> conList = new List<Contact>();
for(integer i=0; i<10; i++)
if(i==5)
conList.add(new Contact());
else
conList.add(new Contact(lastname=’ABC’+1));
}
insert conList; // Throws DML Exception
Database.insert(conList, false); // Doesnt throws exception if a record fails
In DML Statements if there is an error or problem in any of the record then the whole DML results in an exception. (DML Expection) which you can handle on your own whereas in database class methods we can specify whether or not to allow partial record passing if errors are encountered.
We can do so by passing an additional boolean parameter.
When this parameter is set as false and if a record fails the remainder of DML operation can still succeed.
Database.insert(conList);
Database.insert(conList, false);
Ex:
List<Account> accountsToInsert = new List<Account>();
Account accountToUpdate;
for(Integer i=0; i<3; i++){
Account acc = new Account(Name = ‘Simplilearn’, BillingCity = ‘New York’);
accountsToInsert.add(acc);
Database.insert(accountsToInsert);
Ex :
accountToUpdate = [Select BillingCity FROM Account WHERE Name = ‘Simplilearn’ LIMIT 1];
accountToUpdate.BillingCity = ‘San Francisco’;
Database.update(accountToUpdate);
Account afterUpdate = [Select BillingCity FROM Account WHERE Id =: accountToUpdate.Id];
System.assertEquals(afterUpdate.BillingCity, ‘San Francisco’);
Syntax : Databse.upsert(sObject/List<sObject>, externalIdField, allOrNone);
Note: If externalIdField left blank then id field will be used as external id.
List<Account> accountsList = [SELECT Id, Name, BillingCity
FROM Account WHERE BillingCity = ‘San Francisco’];
for (Account acc : accountsList) {
acc.BillingCity = ‘Las Vegas’;
Account newAccount = new Account(Name = ‘SimpliLearn’, BillingCity = ‘Palo Alto’);
accountsList.add(newAccount);
Database.upsert(acctsList);
List<Account> accList = [SELECT name FROM account limit 10];
Database.delete(accList, false);
Note: You cannot delete an account which have cases related to it.
List<Account> accList = [SELECT name FROM account
WHERE isDeleted=true ALL ROWS]
Database.undelete(accList, false);
Syntax :
Database.merge(master_sObject,
duplicate (sObject/List<sObject/ID/List<ID>), allOrNone);
List<Account> accList = [SELECT name FROM account LIMIT 3];
Account masterRecord = accList[0];
List<Account> dupList = new List<Account>;
dupList.add(accList[1]);
dupList.add(accList[2]);
Database.merge(masterRecord, dupList);
< < Previous
Next > >
The batch is going to start shortly. Fill the form given below to Register yourself now.
Fill out the form and get consulted by our Salesforce experts.
Fill out the form to figure out to detemine the perfect Salesforce profile for yourself.
Fill the form below to get a demo of this course.
Fill out the form to get your Premium Salesforce Development Course
Online Salesforce Development Course is soon going to be launched. Please fill the form and we will notify you about the course.