Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @isTest (seeAllData = true)
- public class SOQLqueries {
- static testMethod void SOQLandCollections(){
- //This only works when this test Class is set to SeeAllData=True!
- List<Account> accts = [Select ID from Account];
- System.debug(accts.size());
- System.debug(accts);
- //System.debug(accts[0].Name);//This won't work because Name was not acquired in the query.
- accts = [Select ID, Name, Phone, BillingState from Account];
- System.debug(accts.size());
- System.debug(accts);
- System.debug(accts[0].Name);
- List<Contact> cons = [Select Department, MobilePhone, IsDeleted from Contact];
- System.debug(cons);
- System.debug('smashing****************************' + cons[0].Id);
- //Populating a Map from a SOQL Query is also easy, although not as intutive
- //Note that you have to "cast" the query results (a List) to a Map
- Map<Id, Account> acctMap1 = new Map<Id, Account>([Select ID, Name from Account]);
- System.debug('Account Map: ' + acctMap1);
- Map<Id, Contact> conMap1 = new Map<Id, Contact>(cons);
- System.debug(conMap1);
- Map<Id, Contact> conMap2 = new Map<Id, Contact>([Select Id, Department, MobilePhone, IsDeleted from Contact]);
- System.debug(conMap2);
- //COMMON ROOKIE ERROR
- //Using Lists to update record instead of Maps
- List<Account> acctList = [Select ID, Name, Industry, Ownership from Account];
- List<Account> acctListToUpdate = new List<Account>();
- //{Some logic here that determines Type must be updated on a record}
- acctList[0].Type = 'Retailing';
- acctListToUpdate.add(acctList[0]);
- //{Some other logic here that determines Rating must be updated on the same record}
- acctList[0].Rating = 'Super Hot';
- acctListToUpdate.add(acctList[0]);
- System.debug(acctList[0]);
- System.debug(acctListToUpdate);
- /* The update below compiles: you can save this class with it uncommented, but the class
- * fails when we run the test because we added the same record to the list twice.
- */
- //update acctListToUpdate;//Fails with "Duplicate id in list" exception.
- //Let's try the same thing, but using a Map to hold the records to be updated
- Map<ID, Account> acctMapToUpdate = new Map<Id, Account>();
- //We make the same field/values assigment as before on the same record
- acctList[0].Type = 'Mineral Extraction';
- //but now we put the record in a Map instead of a list
- acctMapToUpdate.put(acctList[0].Id, acctList[0]);
- //And we do it again for the Rating Field
- acctList[0].Rating = 'Heavy';
- //when we put it a second time, the Map recognizes that the ID already exists, and just
- //amends the data in the Map with the new information
- acctMapToUpdate.put(acctList[0].Id, acctList[0]);
- system.debug(acctMapToUpdate);
- update acctMapToUpdate.values();
- //Here's an unexpected pattern for SOQL queries with Parent & Child records
- //You would expect that it might be a List<List> pattern so you could call Contacts
- //with acctsCons[0][0] but instead you must use dot notation to reference the sub-lists
- List<Account> acctCons = [SELECT Name, (SELECT FirstName, LastName FROM Contacts) FROM Account];
- system.debug(acctCons[0].Contacts[0].FirstName);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement