Salesforce - Developer - SOQL

salesforce-developer - Salesforce SOQL Relationship Queries - Relationships in Salesforce - Lookup, Master-Detail, Many to Many and One to Many

Dynamic query
Date and time
Querying related records
Multi-select picklist

Best Practices - Querying Record in Batches By Using SOQL For Loops (Bulkify our code)
Best Practices - Avoid filtering on formula fields or contact Salesforce
Best Practices - Avoid filtering on nulls or contact Salesforce
Best Practices - Combine multiple triggers if possible
Best Practices - Enable governor limit warning email
Best Practices - Use sort optimization for large queries
Avoid invoking future methods, SOQL and DML inside loops.

// Salesforce - Developer - SOQL - Some beautiful code:

trigger contactTest on Contact (before insert, before update) {
  Set<Id> accountIds = new Set<Id>();
  for(Contact ct: {

  //Do SOQL Query       
  Map<Id, Account> accounts = new Map<Id, Account>(
        [select id, name, billingState from Account where id in :accountIds]);

  for (Contact ct: {
    if (accounts.get(ct.AccountId).BillingState=='CA') {
      System.debug('found a contact related to an account in california...'); = '';
      //Apply more logic here....

The above code show us how to use Set, Map, especially how to query 
the database and put the result into a Map in one go.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License