Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. @isTest (seeAllData = true)
  2. public class SOQLqueries {
  3. static testMethod void SOQLandCollections(){
  4. //This only works when this test Class is set to SeeAllData=True!
  5. List<Account> accts = [Select ID from Account];
  6. System.debug(accts.size());
  7. System.debug(accts);
  8. //System.debug(accts[0].Name);//This won't work because Name was not acquired in the query.
  9.  
  10. accts = [Select ID, Name, Phone, BillingState from Account];
  11. System.debug(accts.size());
  12. System.debug(accts);
  13. System.debug(accts[0].Name);
  14.  
  15. List<Contact> cons = [Select Department, MobilePhone, IsDeleted from Contact];
  16. System.debug(cons);
  17. System.debug('smashing****************************' + cons[0].Id);
  18.  
  19.  
  20. //Populating a Map from a SOQL Query is also easy, although not as intutive
  21. //Note that you have to "cast" the query results (a List) to a Map
  22. Map<Id, Account> acctMap1 = new Map<Id, Account>([Select ID, Name from Account]);
  23. System.debug('Account Map: ' + acctMap1);
  24.  
  25. Map<Id, Contact> conMap1 = new Map<Id, Contact>(cons);
  26. System.debug(conMap1);
  27. Map<Id, Contact> conMap2 = new Map<Id, Contact>([Select Id, Department, MobilePhone, IsDeleted from Contact]);
  28. System.debug(conMap2);
  29.  
  30.  
  31. //COMMON ROOKIE ERROR
  32. //Using Lists to update record instead of Maps
  33. List<Account> acctList = [Select ID, Name, Industry, Ownership from Account];
  34. List<Account> acctListToUpdate = new List<Account>();
  35.  
  36. //{Some logic here that determines Type must be updated on a record}
  37. acctList[0].Type = 'Retailing';
  38. acctListToUpdate.add(acctList[0]);
  39.  
  40. //{Some other logic here that determines Rating must be updated on the same record}
  41. acctList[0].Rating = 'Super Hot';
  42. acctListToUpdate.add(acctList[0]);
  43.  
  44. System.debug(acctList[0]);
  45.  
  46. System.debug(acctListToUpdate);
  47. /* The update below compiles: you can save this class with it uncommented, but the class
  48. * fails when we run the test because we added the same record to the list twice.
  49. */
  50. //update acctListToUpdate;//Fails with "Duplicate id in list" exception.
  51.  
  52.  
  53. //Let's try the same thing, but using a Map to hold the records to be updated
  54. Map<ID, Account> acctMapToUpdate = new Map<Id, Account>();
  55.  
  56. //We make the same field/values assigment as before on the same record
  57. acctList[0].Type = 'Mineral Extraction';
  58. //but now we put the record in a Map instead of a list
  59. acctMapToUpdate.put(acctList[0].Id, acctList[0]);
  60.  
  61. //And we do it again for the Rating Field
  62. acctList[0].Rating = 'Heavy';
  63. //when we put it a second time, the Map recognizes that the ID already exists, and just
  64. //amends the data in the Map with the new information
  65. acctMapToUpdate.put(acctList[0].Id, acctList[0]);
  66.  
  67. system.debug(acctMapToUpdate);
  68. update acctMapToUpdate.values();
  69.  
  70. //Here's an unexpected pattern for SOQL queries with Parent & Child records
  71. //You would expect that it might be a List<List> pattern so you could call Contacts
  72. //with acctsCons[0][0] but instead you must use dot notation to reference the sub-lists
  73. List<Account> acctCons = [SELECT Name, (SELECT FirstName, LastName FROM Contacts) FROM Account];
  74. system.debug(acctCons[0].Contacts[0].FirstName);
  75.  
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement