Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Episode 6 - DML, Transaction and Error handling in Salesforce

158 views

Published on

Episode 6 of path to code - DML, Transaction and Error handling in Salesforce

Published in: Education
  • Be the first to comment

  • Be the first to like this

Episode 6 - DML, Transaction and Error handling in Salesforce

  1. 1. Path to Code Begin Your Salesforce Coding Adventure
  2. 2. Episode 6 Performing DML & Error Handling
  3. 3. Salesforce MVP, Founder of ApexHours Follow me at @Amit_SFDC @ApexHours Amit Chaudhary
  4. 4. Agenda • Manipulate Records with DML • insert • update • delete • upsert • undelete • merge • Bulk DML • Database Methods and Error Handling • Transaction Control • Savepoint • Rollback
  5. 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. 6. Manipulate Records with DML Create and modify records in Salesforce by using the Data Manipulation Language, abbreviated as DML
  7. 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. 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. 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. 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. 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. 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. 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. 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. 15. Q & A
  16. 16. Thank You
  17. 17. Subscribe

×