Salesforce coding lessons for the 99%
Finally, Apex tutorials for point-and-click admins! Written by a self-taught Google engineer.
  • Beginner Tutorials
    • Apex
    • Certifications
    • Career Info
    • Technical Architect
    • Visualforce
    • Videos
  • Apex Academy
  • Success Stories
  • About Me
  • Misc
    • Mailbag
    • Challenges
    • Links
    • Login to my Org
Follow @dvdkliuor SUBSCRIBE!

Intro to SOQL and why you should learn it

June 5, 2013

Preface: this post is part of the SOQL: A Beginner’s Guide series.

SOQL is Salesforce’s official object query language. It’s based on SQL, one of the most popular database programming languages ever!

In a nutshell, SOQL is just a tool that lets you access anything about any record in your Salesforce database. Here’s an example:

SELECT Id, Name, Email, AccountId FROM Contact

In the above code, we’re using SOQL to access those four fields for every contact in Salesforce!
(You may have seen something similar when using Data Loader – that uses SOQL in the background!)

And that’s it – you already know 75% of SOQL, it’s really easy to learn!

Here’s why SOQL is significant:

  1. You can use both SOQL and Apex in a trigger
    In fact, more often than not you will! No need to worry – it’s really easy and I’ll show you how!
  2. SOQL lets you do cross-object actions in your trigger
    Remember how this was one of the limitations of using workflows? SOQL lets you access records that normally aren’t available in your trigger (relevant: trigger.new). For example, if you’re writing a trigger on Leads, you could use SOQL to access data from Users, a totally unrelated object!
  3. Mastering SOQL is the key to mastering Apex
    Once you start writing more complex pieces of code, you’ll soon run into governor limits. A good Salesforce developer prevents their code from hitting these limits by using SOQL effectively. Don’t worry about this for now, we’ll get to this later!

Next post: Where to write SOQL queries

6 Comments
Jeevan
September 7, 2016 @ 7:11 pm

upload a video o soql join.Then we can understand easily.
Thank You

Reply
    David Liu
    September 7, 2016 @ 7:33 pm

    It’s there in Apex Academy #3!

    David

    Reply
Anonymous
April 16, 2014 @ 7:35 am

Apex Trigger:

trigger ResultCandidate on CandidateResult__c (before insert) {
CandidateResult__c result=Trigger.new[0];
integer i = 0;
Candidate__c cc;
List c= new List();
c=[SELECT Name,Result__c FROM Candidate__c];
Iterator candidates =c.iterator();
while (candidates.hasNext())
{
cc=candidates.next();

if(cc.Name.isequals(result.CName__c))
{
UpdateCandidateResult.changeResult(result,cc);
}

}

}

Apex Class:

public class UpdateCandidateResult{
public static void changeResult(CandidateResult__c r,Candidate__c c){

if(r.InterviewResult__c==’Cleared’)
{
c.Result__c=’Pass’;
}

if(r.InterviewResult__c==’Partially Cleared’)
{
c.Result__c=’On Hold’;
}

if(r.InterviewResult__c==’Failed’)
{
c.Result__c=’Fail’;
}
update c;
}
}

Hi David,

i am Srinivas and very new to salesforce.com. i was just started 1 week ago and tried to write this code…
when i tried to save the trigger,,i got an error like..

Method does not exist or incorrect signature: [String].isequals(Id) at line 12 column 18

Exactly what my question is…how to access a master field using detail reference??? it would be very helpful for me..if u answer this question.i’m just started to follow ur site,it was really helping alot.

Another question Does Sandox appear default in any edition or not?? because i didnt find any sandbox under deploy.

Thanks in advance…

Reply
    David Liu
    April 16, 2014 @ 10:31 pm

    Try this!
    cc.Name = result.CName__c.Name;

    An Opp (child) and Account (master) have a master-detail relationship. If you’re on an Opp, you can reference the Account field by going like this:
    myOpp.Account.Name;

    This tutorial will help you understand this!
    https://www.sfdc99.com/2013/06/09/example-how-to-write-a-cross-object-soql-query/

    Also – sandboxes are only on paid editions! So can’t deploy if you’re on developer edition unfortunately!
    David

    P.S. sorry there is a delay when posting a comment – I manually approve comments because there is a lot of spam!

    Reply
      Samyuktha
      January 12, 2016 @ 12:40 am

      Hi David,

      This is samyuktha, am unable to get notes on SOQL and trigger to get good knowledge on it, basically am interested to learn it

      If you help me out in this it will more appreciable.

      Reply
        David Liu
        January 13, 2016 @ 8:23 pm

        Is this site insufficient? =)

        Reply

Leave a Reply Cancel reply

Your email address will not be published.


*

*

Theme: Simple Style by Fimply