SlideShare une entreprise Scribd logo
1  sur  17
Path to Code
Begin Your Salesforce Coding Adventure
Episode 6
Performing DML & Error Handling
Salesforce MVP, Founder of ApexHours
Follow me at @Amit_SFDC @ApexHours
Amit
Chaudhary
Agenda
• Manipulate Records with DML
• insert
• update
• delete
• upsert
• undelete
• merge
• Bulk DML
• Database Methods and Error Handling
• Transaction Control
• Savepoint
• Rollback
Some House Rules
• Mute your mic
• Keep adding questions in Zoom Q&A Window
• No question is too small
• Questions will be answered in last 15 mins
Manipulate Records with DML
Create and modify records in Salesforce by using the Data Manipulation Language,
abbreviated as DML
Insert and Update Statement
The insert DML operation adds one or more sObjects, such as
individual accounts or contacts, to your organization’s data
ID Field Auto-Assigned to New Records after Insert
// Create the account sObject
Account acct = new Account(Name='PathToCode', Phone='(111)111-1111');
insert acct;
Account myAcct = [SELECT Id, Name, BillingCity FROM Account WHERE Id = :acct.Id];
myAcct.BillingCity = 'San Francisco';
update myAcct;
Upsert
• Update+insert
• If the key is not matched, a new object record is created
• If the key is matched once, the existing object record is updated
Contact contact1 = new
Contact(FirstName='Amit',LastName='Chaudhary',Department='Finance');
insert contact1;
contact1.Description = 'Test Update';
Contact contact2 = new
Contact(FirstName='Jitender',LastName='Zaa',Department='Technology');
List<Contact> listCont = new List<Contact> { contact1, contact2 };
upsert listCont;
Delete and UnDelete
The ALL ROWS keyword queries all rows for both top level and
aggregate relationships, including deleted records and archived
activities
List<Contact> listCont = [SELECT Id FROM Contact WHERE LastName='Chaudhary'];
delete listCont;
List<Contact> listCont = [SELECT Id FROM Contact WHERE LastName='Chaudhary' ALL
ROWS ];
undelete listCont;
Merge
Account masterAcct = [SELECT Id, Name FROM Account WHERE Name = 'IBM.' LIMIT 1];
Account mergeAcct = [SELECT Id, Name FROM Account WHERE Name = 'IBM INDIA' LIMIT 1];
merge masterAcct mergeAcct;
Bulk DML
List<Account> lstAcc = new List<Account>();
for(Integer i=1 ;i<=10;i++){
Account acc = new Account();
acc.name = 'Test Bulk '+i;
lstAcc.add(acc);
}
insert lstAcc;
Database Methods
• Database methods are static methods available in Database class
• Partial insert is supported
• Includes the optional allorNone parameters that defaults true
• Database.insert()
• Database.update()
• Database.upsert()
• Database.delete()
• Database.undelete()
• Database.merge()
Database Methods
List<Account> acctList = new List<Account>();
acctList.add(new Account(Name='ABC'));
acctList.add(new Account(Name='XYZ'));
Database.SaveResult[] srList = Database.insert(acctList, false);
for (Database.SaveResult sr : srList) {
if (sr.isSuccess()) {
System.debug('Successfully inserted account. Account ID: ' + sr.getId());
} else {
// Operation failed, so get all errors
for(Database.Error err : sr.getErrors()) {
System.debug('The following error has occurred.');
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Account fields that affected this error: ' + err.getFields());
}
}
}
Transaction Control
• setSavepoint
• Rollback
Account a = new Account(Name = 'AAA');
insert a;
System.assertEquals(null, [SELECT AccountNumber FROM Account WHERE Id = :a.Id].AccountNumber);
// Create a savepoint while AccountNumber is null
Savepoint sp = Database.setSavepoint();
// Change the account number
a.AccountNumber = '123';
update a;
System.assertEquals('123', [SELECT AccountNumber FROM Account WHERE Id = :a.Id].AccountNumber);
// Rollback to the previous null value
Database.rollback(sp);
System.assertEquals(null, [SELECT AccountNumber FROM Account WHERE Id = :a.Id].AccountNumber);
Q & A
Thank You
Subscribe

Contenu connexe

Tendances

Introduction to Salesforce CRM Reporting
Introduction to Salesforce CRM ReportingIntroduction to Salesforce CRM Reporting
Introduction to Salesforce CRM Reporting
Michael Olschimke
 

Tendances (20)

Batch Apex in Salesforce
Batch Apex in SalesforceBatch Apex in Salesforce
Batch Apex in Salesforce
 
Salesforce asynchronous apex
Salesforce asynchronous apexSalesforce asynchronous apex
Salesforce asynchronous apex
 
LWC Episode 3- Component Communication and Aura Interoperability
LWC Episode 3- Component Communication and Aura InteroperabilityLWC Episode 3- Component Communication and Aura Interoperability
LWC Episode 3- Component Communication and Aura Interoperability
 
Asynchronous apex
Asynchronous apexAsynchronous apex
Asynchronous apex
 
Lwc presentation
Lwc presentationLwc presentation
Lwc presentation
 
Apex Trigger in Salesforce
Apex Trigger in SalesforceApex Trigger in Salesforce
Apex Trigger in Salesforce
 
SOQL & SOSL for Admins
SOQL & SOSL for AdminsSOQL & SOSL for Admins
SOQL & SOSL for Admins
 
Apex Enterprise Patterns: Building Strong Foundations
Apex Enterprise Patterns: Building Strong FoundationsApex Enterprise Patterns: Building Strong Foundations
Apex Enterprise Patterns: Building Strong Foundations
 
Data model in salesforce
Data model in salesforceData model in salesforce
Data model in salesforce
 
Salesforce Community Cloud
Salesforce Community CloudSalesforce Community Cloud
Salesforce Community Cloud
 
Salesforce integration best practices columbus meetup
Salesforce integration best practices   columbus meetupSalesforce integration best practices   columbus meetup
Salesforce integration best practices columbus meetup
 
Salesforce complete overview
Salesforce complete overviewSalesforce complete overview
Salesforce complete overview
 
OEBS R12 Presentation.ppt
OEBS R12 Presentation.pptOEBS R12 Presentation.ppt
OEBS R12 Presentation.ppt
 
Relationships in Salesforce
Relationships in SalesforceRelationships in Salesforce
Relationships in Salesforce
 
Oracle Fusion Development, May 2009
Oracle Fusion Development, May 2009Oracle Fusion Development, May 2009
Oracle Fusion Development, May 2009
 
Oracle EBS r12-2-6 New Features
Oracle EBS r12-2-6 New FeaturesOracle EBS r12-2-6 New Features
Oracle EBS r12-2-6 New Features
 
Salesforce REST API
Salesforce  REST API Salesforce  REST API
Salesforce REST API
 
Salesforce customization vs configuration
Salesforce customization vs configurationSalesforce customization vs configuration
Salesforce customization vs configuration
 
Building BI Publisher Reports using Templates
Building BI Publisher Reports using TemplatesBuilding BI Publisher Reports using Templates
Building BI Publisher Reports using Templates
 
Introduction to Salesforce CRM Reporting
Introduction to Salesforce CRM ReportingIntroduction to Salesforce CRM Reporting
Introduction to Salesforce CRM Reporting
 

Similaire à Episode 6 - DML, Transaction and Error handling in Salesforce

A Brief Introduction in SQL Injection
A Brief Introduction in SQL InjectionA Brief Introduction in SQL Injection
A Brief Introduction in SQL Injection
Sina Manavi
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SaiMiryala1
 
Performance Tuning for Visualforce and Apex
Performance Tuning for Visualforce and ApexPerformance Tuning for Visualforce and Apex
Performance Tuning for Visualforce and Apex
Salesforce Developers
 
Business Intelligence Portfolio
Business Intelligence PortfolioBusiness Intelligence Portfolio
Business Intelligence Portfolio
Chris Seebacher
 
Kevin Bengtson Portfolio
Kevin Bengtson PortfolioKevin Bengtson Portfolio
Kevin Bengtson Portfolio
Kbengt521
 

Similaire à Episode 6 - DML, Transaction and Error handling in Salesforce (20)

Preethi apex-basics-jan19
Preethi apex-basics-jan19Preethi apex-basics-jan19
Preethi apex-basics-jan19
 
70433 Dumps DB
70433 Dumps DB70433 Dumps DB
70433 Dumps DB
 
Winter'23 Release Updates.pptx
Winter'23 Release Updates.pptxWinter'23 Release Updates.pptx
Winter'23 Release Updates.pptx
 
Merge In Sql 2008
Merge In Sql 2008Merge In Sql 2008
Merge In Sql 2008
 
Banking Database
Banking DatabaseBanking Database
Banking Database
 
How to teach an elephant to rock'n'roll
How to teach an elephant to rock'n'rollHow to teach an elephant to rock'n'roll
How to teach an elephant to rock'n'roll
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
 
SQL Tunning
SQL TunningSQL Tunning
SQL Tunning
 
How Clean is your Database? Data Scrubbing for all Skill Sets
How Clean is your Database? Data Scrubbing for all Skill SetsHow Clean is your Database? Data Scrubbing for all Skill Sets
How Clean is your Database? Data Scrubbing for all Skill Sets
 
A Brief Introduction in SQL Injection
A Brief Introduction in SQL InjectionA Brief Introduction in SQL Injection
A Brief Introduction in SQL Injection
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
 
Performance Tuning for Visualforce and Apex
Performance Tuning for Visualforce and ApexPerformance Tuning for Visualforce and Apex
Performance Tuning for Visualforce and Apex
 
PHP tips by a MYSQL DBA
PHP tips by a MYSQL DBAPHP tips by a MYSQL DBA
PHP tips by a MYSQL DBA
 
Business Intelligence Portfolio
Business Intelligence PortfolioBusiness Intelligence Portfolio
Business Intelligence Portfolio
 
Empowering Users with Analytical MDX
Empowering Users with Analytical MDXEmpowering Users with Analytical MDX
Empowering Users with Analytical MDX
 
Kevin Bengtson Portfolio
Kevin Bengtson PortfolioKevin Bengtson Portfolio
Kevin Bengtson Portfolio
 
PL/SQL TRIGGERS
PL/SQL TRIGGERSPL/SQL TRIGGERS
PL/SQL TRIGGERS
 
Crm saturday madrid 2017 3 mosqueteros demian-marco-mario
Crm saturday madrid 2017   3  mosqueteros demian-marco-marioCrm saturday madrid 2017   3  mosqueteros demian-marco-mario
Crm saturday madrid 2017 3 mosqueteros demian-marco-mario
 

Plus de Jitendra Zaa

Plus de Jitendra Zaa (20)

Episode 13 - Advanced Apex Triggers
Episode 13 - Advanced Apex TriggersEpisode 13 - Advanced Apex Triggers
Episode 13 - Advanced Apex Triggers
 
Episode 18 - Asynchronous Apex
Episode 18 - Asynchronous ApexEpisode 18 - Asynchronous Apex
Episode 18 - Asynchronous Apex
 
Episode 15 - Basics of Javascript
Episode 15 - Basics of JavascriptEpisode 15 - Basics of Javascript
Episode 15 - Basics of Javascript
 
Episode 23 - Design Pattern 3
Episode 23 - Design Pattern 3Episode 23 - Design Pattern 3
Episode 23 - Design Pattern 3
 
Episode 24 - Live Q&A for getting started with Salesforce
Episode 24 - Live Q&A for getting started with SalesforceEpisode 24 - Live Q&A for getting started with Salesforce
Episode 24 - Live Q&A for getting started with Salesforce
 
Episode 22 - Design Pattern 2
Episode 22 - Design Pattern 2Episode 22 - Design Pattern 2
Episode 22 - Design Pattern 2
 
Episode 21 - Design Pattern 1
Episode 21 - Design Pattern 1Episode 21 - Design Pattern 1
Episode 21 - Design Pattern 1
 
Episode 20 - Trigger Frameworks in Salesforce
Episode 20 - Trigger Frameworks in SalesforceEpisode 20 - Trigger Frameworks in Salesforce
Episode 20 - Trigger Frameworks in Salesforce
 
Episode 19 - Asynchronous Apex - Batch apex & schedulers
Episode 19 - Asynchronous Apex - Batch apex & schedulersEpisode 19 - Asynchronous Apex - Batch apex & schedulers
Episode 19 - Asynchronous Apex - Batch apex & schedulers
 
Episode 17 - Handling Events in Lightning Web Component
Episode 17 - Handling Events in Lightning Web ComponentEpisode 17 - Handling Events in Lightning Web Component
Episode 17 - Handling Events in Lightning Web Component
 
Episode 16 - Introduction to LWC
Episode 16 - Introduction to LWCEpisode 16 - Introduction to LWC
Episode 16 - Introduction to LWC
 
Introduction to mulesoft - Alpharetta Developer Group Meet
Introduction to mulesoft - Alpharetta Developer Group MeetIntroduction to mulesoft - Alpharetta Developer Group Meet
Introduction to mulesoft - Alpharetta Developer Group Meet
 
Episode 12 - Basics of Trigger
Episode 12 - Basics of TriggerEpisode 12 - Basics of Trigger
Episode 12 - Basics of Trigger
 
Episode 11 building & exposing rest api in salesforce v1.0
Episode 11   building & exposing rest api in salesforce v1.0Episode 11   building & exposing rest api in salesforce v1.0
Episode 11 building & exposing rest api in salesforce v1.0
 
Episode 10 - External Services in Salesforce
Episode 10 - External Services in SalesforceEpisode 10 - External Services in Salesforce
Episode 10 - External Services in Salesforce
 
Episode 14 - Basics of HTML for Salesforce
Episode 14 - Basics of HTML for SalesforceEpisode 14 - Basics of HTML for Salesforce
Episode 14 - Basics of HTML for Salesforce
 
South East Dreamin 2019
South East Dreamin 2019South East Dreamin 2019
South East Dreamin 2019
 
Episode 9 - Building soap integrations in salesforce
Episode 9 - Building soap integrations  in salesforceEpisode 9 - Building soap integrations  in salesforce
Episode 9 - Building soap integrations in salesforce
 
Episode 8 - Path To Code - Integrate Salesforce with external system using R...
Episode 8  - Path To Code - Integrate Salesforce with external system using R...Episode 8  - Path To Code - Integrate Salesforce with external system using R...
Episode 8 - Path To Code - Integrate Salesforce with external system using R...
 
Episode 5 - Writing unit tests in Salesforce
Episode 5 - Writing unit tests in SalesforceEpisode 5 - Writing unit tests in Salesforce
Episode 5 - Writing unit tests in Salesforce
 

Dernier

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Dernier (20)

TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 

Episode 6 - DML, Transaction and Error handling in Salesforce

  • 1. Path to Code Begin Your Salesforce Coding Adventure
  • 2. Episode 6 Performing DML & Error Handling
  • 3. Salesforce MVP, Founder of ApexHours Follow me at @Amit_SFDC @ApexHours Amit Chaudhary
  • 4. Agenda • Manipulate Records with DML • insert • update • delete • upsert • undelete • merge • Bulk DML • Database Methods and Error Handling • Transaction Control • Savepoint • Rollback
  • 5. Some House Rules • Mute your mic • Keep adding questions in Zoom Q&A Window • No question is too small • Questions will be answered in last 15 mins
  • 6. Manipulate Records with DML Create and modify records in Salesforce by using the Data Manipulation Language, abbreviated as DML
  • 7. Insert and Update Statement The insert DML operation adds one or more sObjects, such as individual accounts or contacts, to your organization’s data ID Field Auto-Assigned to New Records after Insert // Create the account sObject Account acct = new Account(Name='PathToCode', Phone='(111)111-1111'); insert acct; Account myAcct = [SELECT Id, Name, BillingCity FROM Account WHERE Id = :acct.Id]; myAcct.BillingCity = 'San Francisco'; update myAcct;
  • 8. Upsert • Update+insert • If the key is not matched, a new object record is created • If the key is matched once, the existing object record is updated Contact contact1 = new Contact(FirstName='Amit',LastName='Chaudhary',Department='Finance'); insert contact1; contact1.Description = 'Test Update'; Contact contact2 = new Contact(FirstName='Jitender',LastName='Zaa',Department='Technology'); List<Contact> listCont = new List<Contact> { contact1, contact2 }; upsert listCont;
  • 9. Delete and UnDelete The ALL ROWS keyword queries all rows for both top level and aggregate relationships, including deleted records and archived activities List<Contact> listCont = [SELECT Id FROM Contact WHERE LastName='Chaudhary']; delete listCont; List<Contact> listCont = [SELECT Id FROM Contact WHERE LastName='Chaudhary' ALL ROWS ]; undelete listCont;
  • 10. Merge Account masterAcct = [SELECT Id, Name FROM Account WHERE Name = 'IBM.' LIMIT 1]; Account mergeAcct = [SELECT Id, Name FROM Account WHERE Name = 'IBM INDIA' LIMIT 1]; merge masterAcct mergeAcct;
  • 11. Bulk DML List<Account> lstAcc = new List<Account>(); for(Integer i=1 ;i<=10;i++){ Account acc = new Account(); acc.name = 'Test Bulk '+i; lstAcc.add(acc); } insert lstAcc;
  • 12. Database Methods • Database methods are static methods available in Database class • Partial insert is supported • Includes the optional allorNone parameters that defaults true • Database.insert() • Database.update() • Database.upsert() • Database.delete() • Database.undelete() • Database.merge()
  • 13. Database Methods List<Account> acctList = new List<Account>(); acctList.add(new Account(Name='ABC')); acctList.add(new Account(Name='XYZ')); Database.SaveResult[] srList = Database.insert(acctList, false); for (Database.SaveResult sr : srList) { if (sr.isSuccess()) { System.debug('Successfully inserted account. Account ID: ' + sr.getId()); } else { // Operation failed, so get all errors for(Database.Error err : sr.getErrors()) { System.debug('The following error has occurred.'); System.debug(err.getStatusCode() + ': ' + err.getMessage()); System.debug('Account fields that affected this error: ' + err.getFields()); } } }
  • 14. Transaction Control • setSavepoint • Rollback Account a = new Account(Name = 'AAA'); insert a; System.assertEquals(null, [SELECT AccountNumber FROM Account WHERE Id = :a.Id].AccountNumber); // Create a savepoint while AccountNumber is null Savepoint sp = Database.setSavepoint(); // Change the account number a.AccountNumber = '123'; update a; System.assertEquals('123', [SELECT AccountNumber FROM Account WHERE Id = :a.Id].AccountNumber); // Rollback to the previous null value Database.rollback(sp); System.assertEquals(null, [SELECT AccountNumber FROM Account WHERE Id = :a.Id].AccountNumber);
  • 15. Q & A