Skip to content

Instantly share code, notes, and snippets.

@ohadios
Forked from AlwaysThinkin/4-SOQLqueries.java
Last active August 16, 2016 14:38
Show Gist options
  • Select an option

  • Save ohadios/6fdf182f85bbc56a0327c791f14f3c43 to your computer and use it in GitHub Desktop.

Select an option

Save ohadios/6fdf182f85bbc56a0327c791f14f3c43 to your computer and use it in GitHub Desktop.
@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);
system.debug(acctCons);
// Let's try to query all accounts with their child cases in one query call
// For each case that we want to query, we also want to find their contact ID
// notice that this time we are not using 'case' which is the object name but rather 'cases' which is the relationship name!
List<Account> acctCases = [SELECT id, name, (SELECT id, subject, contactid from cases) from account where name like '%United%' or name like '%Tyco%' order by name desc];
System.debug('Number of accountes queries: '+acctCases.size());
System.debug('***Accounts and Cases queried, but only accounts shown on debug****');
System.debug(acctCases);
// Let's get the number of cases from the first account
System.Debug('Yes the cases are queried and we can show them');
System.Debug('Number of cases on third account: '+acctCases[2].Cases.size());
System.debug(acctCases[2].Cases);
// Another way to is to query all account and all cases into two separate lists, but then would require some additional
// handling to make sense of the data.
List<Account> acctNoCases = [SELECT id, name from Account where name like '%United%' or name like '%Tyco%' order by name desc];
List<Case> cases = [Select id, subject, contactid from case];
// Let's get only the cases for the first account queried
List<Case> firstAcctCases = [SELECT id, subject, contactid from case where accountid =: acctNoCases[2].id];
System.Debug('Number of cases on first account queried: '+firstAcctCases.size());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment