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!

Introduction to Object-Oriented Programming

August 12, 2014

Preface – This post is part of the Object Oriented Thinking series.

“In my 20 years in this industry, I have never seen a revolution as profound as this.”
— Steve Jobs, on object-oriented programming

So what is object-oriented programming (OOP) and why was Steve Jobs so passionate about it?

I’ll tell you, but to be honest, the late Steve Jobs said it best:

“Objects are like people. They’re living, breathing things that have knowledge inside them about how to do things and have memory inside them so they can remember things. And rather than interacting with them at a very low level, you interact with them at a very high level of abstraction, like we’re doing right here.

Here’s an example: If I’m your laundry object, you can give me your dirty clothes and send me a message that says, “Can you get my clothes laundered, please.” I happen to know where the best laundry place in San Francisco is. And I speak English, and I have dollars in my pockets. So I go out and hail a taxicab and tell the driver to take me to this place in San Francisco. I go get your clothes laundered, I jump back in the cab, I get back here. I give you your clean clothes and say, “Here are your clean clothes.”

You have no idea how I did that. You have no knowledge of the laundry place. Maybe you speak French, and you can’t even hail a taxi. You can’t pay for one, you don’t have dollars in your pocket. Yet I knew how to do all of that. And you didn’t have to know any of it. All that complexity was hidden inside of me, and we were able to interact at a very high level of abstraction. That’s what objects are. They encapsulate complexity, and the interfaces to that complexity are high level.”

Steve somehow knew that object-oriented programming would revolutionize coding, long before the world realized this. What a brilliant man!

Don’t worry if the concept of OOP hasn’t sunk in your head yet – it takes time to understand it. But fear not students: we’re going to spend this entire chapter digging deeper into OOP, how it applies to Salesforce, and how it’ll help you to write bigger and better codebases.

Next post: What are objects and classes?

14 Comments
Travis
April 26, 2016 @ 8:39 am

Hello Community,

I’m attempting to use one SOQL query to get many data point that can be displayed on a Visualforce page. My query returns the expected results but I am not sure of how to parse through the query results and assign them to variables. My thought was that I could simply loop through the results and, based on the conditions, assign them to variables with getters. This does not generate an error, but my page will not return anything when calling {!MembershipsYTD}. if someone could help point me in the right direction it would be greatly appreciated.

public class MDEventResults
{

// Properties
public integer ThisMonth = date.today().month();
public integer MembershipsYTD {get; set;}
public integer MembershipsMTD {get; set;}

// Query Event Data
List EventAgg = [SELECT Sum(Total_Memberships__c) MembershipSum, count(ID) CNT, Calendar_month(Event_Date__c) Month, Facilitated_by_MD__c FacilitatedBy, Booked_By__c BookedBy,
GROUPING (Facilitated_by_MD__C) FacilGroup, GROUPING (Booked_By__c) BookedGroup, GROUPING (Event_Date__c) monthGroup
FROM Campaign
// WHERE Booked_by__r.id = :UserInfo.getUserid()//
WHERE End_Date_Time__c = This_Year
// AND Status = ‘Completed’ //
GROUP BY ROLLUP (Facilitated_by_MD__c , Booked_By__c, Event_Date__c)];

// method to parse results
public void MembershipsYTD() {
for (AggregateResult ar : EventAgg) {
if (integer.valueOf(ar.get(‘monthGroup’ )) == ThisMonth &&
integer.valueOf(ar.get(‘BookedGroup’)) == 1 &&
integer.valueOf(ar.get(‘FacilGroup’ )) == 1)
{
MembershipsYTD = integer.valueOf(ar.get(‘MembershipSum’));
system.debug(‘MembershipsYTD’ + MembershipsYTD);
}
}

}
}

Reply
    Travis
    April 27, 2016 @ 9:23 am

    updated question: Any help would be greatly appreciated
    https://developer.salesforce.com/forums/ForumsMain?id=906F0000000MMZaIAO

    Reply
Sinan Bunni
October 2, 2015 @ 1:44 am

Hi David,

Thank you for your tutorials, I am learning a lot from them. could you please let me know when are you going to release more sessions on your website?

Thank you,

Sinan

Reply
sai
August 31, 2014 @ 7:59 pm

Hi David,

Greetings!!

Glad to know the site which helps to learn salesforce coding…

I would like to know where to start apex….I am struggling to write code……..Dont know where to start ….how to learn basics of apex programming……

I want to be good programmer…..Any help on this please….Please guide me….I am losing confidence because of not having coding skills…..

Reply
    David Liu
    September 1, 2014 @ 10:53 pm

    What’s wrong with my site? =)

    Reply
      Reddy Penna
      May 21, 2015 @ 8:49 am

      LOL… Good one

      Reply
David S - Future APEX Developer.
August 14, 2014 @ 8:44 am

Jose – you need to load every Eric Thomas motivational speech to your iPod, and listed to it every day. Listen to this one in the morning, while getting ready for the day. http://youtu.be/_jHeqfZO69o

You need to want it… Time to Rise and Grind my brother.

Reply
    David Liu
    August 14, 2014 @ 9:43 pm

    This video was so good I made a post on it – thank you David for you inspiration to me and others!!

    Reply
lakshminarayana
August 14, 2014 @ 2:07 am

looking forward to watch it/..

Reply
Shobhit Tuteja
August 12, 2014 @ 11:42 pm

Looking forward for this exciting chapter…..!!!

Reply
Jose Maria Claramunt
August 12, 2014 @ 11:17 pm

OOP is very interesting and can empower so many novice programmers….I wish I had more time to learn!!
Im thinking of going full time study, 6 months of intense Java learning and Apex, then start an exciting job.
Who dares wins!

Reply
    David Liu
    August 12, 2014 @ 11:27 pm

    Jose, I was in your exact position not long ago. Quit, and learn to code, or stay on the job, and potentially lose out on the opportunity to do something I loved.

    Ultimately, I decided that I did not want to grow old and one day ask “what if?” I wanted to learn badly enough that I would have no regrets even if I failed.

    I learned though that the two things, your job and learning Apex, are not opposite tracks. You can still work a 40-50 hour a week job AND learn to code enough that you can get a dedicated job as a coder. Even if you have no freedom in your job to practice any code, you can still learn quickly enough at home and on weekends. 10 hours a week for six months is all it takes!

    So – definitely don’t feel like you have to quit your day job to follow a passion! Learning is much easier than you think – consistent practice, no matter how little each time, will get you there!

    Reply
      Jose Maria Claramunt
      August 13, 2014 @ 1:16 pm

      Hi Dave!

      I wish I had your energy! But yeah, for sure, sometimes you need to really dig deep and strive for your dreams.

      Something that sets me back is me having to practice SF admin and customization in my spare time rather than on live projects…grrrr. I was hired to implement SF for the company’s Sales team, but in the end, they went cold turkey and chose another CRM. So here I am, one year later, doing SAP and Ariba ERP administration and support (yuck) and leaving SF behind.

      Ive had some very juicy SF job offers thrown at me (based in England) and Ive had to turn them down due to my SF skills being a little rusty!

      Im in a dilema: to quit and go full SF and Apex VS staying and putting in the extra hours and effort…..I will follow my heart and also, take note of your advice :)

      Thank you once again for inspiring me to go forward and do my best!

      Reply
        David Liu
        August 13, 2014 @ 11:09 pm

        Oh man Jose, sounds like your job is pushing you away from Salesforce instead of the other way around!

        If you don’t choose to take time off or accept a Salesforce job offer, one thing you can also do is volunteer to do admin work for a Salesforce non-profit a few hours a week. A lot of my co-workers broke into the Salesforce industry this way!

        Best of luck and let me know what you decide!
        David

        Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *


*

*

Theme: Simple Style by Fimply