Apex Learning Journal #2 – Apex v.s. Java

Written Date: July 2, 2021

After finishing the Java course, I was ready to start learning Apex. I want to know how Apex differs from Java, so my original idea was to take an Apex-specific course. However, this idea was quickly discouraged by my amazing colleague who works as an architect. According to his words, Apex is like a limited version of Java with some syntax variation, so better to learn by doing instead of spending many hours on another course. So at this stage, I focused on using Trailhead modules to get that quick overview of Apex syntax, which worked out great! Here is the Apex Trailmix I created if you are interested. (I only finished every before “Study for PD1” as of date)

CONTENTS
  1. Study Notes
  2. Learning Path
  3. Summary
  4. Random Thoughts

The Trailhead study process went smoothly as I am now equipped with some basic Java knowledge. The modules are easy to follow, and there are many projects where we can practice coding – which is great! In this study notes, I have summarized the major differences of Apex / Java and of Apex / other automation tools. I also noted down some basic concepts and special syntax.

Basic Concepts of Apex

1. Apex is case insensitive
2. Apex is like a limited version of Java
3. Apex uses SOQL, SOSL to query data, and DML to modify data

I had some experience with SQL, so SOQL is not difficult for me to pick up. The SOQL examples I have seen so far are only simple queries. SOSL on the other hand is something new, however, the syntax was not hard to learn either. If you are familiar with any query language, these two should be easy to learn.

In my opinion, DML (Data Manipulation Language) just sounds really fancy (thus intimidating). It is actually just “insert, upsert, update, delete, undelete, and merge“. You can relate this to the Data Elements in Flow Builder (Update Records, Create Records, etc).

4. Apex has governor limits as we introduced in the Governor Limits post

We should pay extra focus on SOQL and DML limits! Make sure we don’t query data or modify data inside a loop to ensure we don’t run over limits.

5. The primary key is the Id for the current record, and the foreign key is the Id to the relationship records

This is more like a general knowledge for relational database, but I still want to note this down. For example, on the Opportunity object, the Opportunity Id is the primary key, and the Account Id will be the foreign key.

Major Difference Between Java and Apex

1. Class in Java is like Object in Salesforce, and Object in Java is like a Record in Salesforce
2. Apex has some slightly different namings for the primitive data type
3. Apex has three types of collection: List, Set, and Map
4. The syntax of multi-dimensional collections: Datatype[][] in Java / List<List<Datatype>> in Apex

The collection in Apex is the biggest difference in my opinion when jumping from the Java course to Trailhead modules. The Map definitely has the most confusing name. (Before I imagined this as a real map…). But it is actually a simple concept – a collection of key/value pairs. For example, Triggger.NewMap is a map collection that contains the pairs where key = IDs and value = Records.

The syntax to declare multi-dimensional collections (List of List) are also different. With Java, you will use [ ][ ] syntax. (Ex. List of list of integer is int[ ][ ]). With Apex, you write List<List< >> (Ex. List of list of integer is List<List<Integer>>)

5. Apex has Do-while loop

The Do-while loop was something new to me. While the “For” and “While” loop will only run and continue when the criteria are met, the “Do-while” will execute the action first, evaluate the criteria, and then continue if the criteria are met. Very interesting!

● Major Different Between Apex and Other Automation Tools

1. Apex Testing and Deployment are the most difficult part for declarative tools users

The most challenging part of my Apex learning journey is definitely the Testing and Deployment. Not saying that the concepts are difficult to understand, but these are the steps that we are not used to when using Process Builder or Flow Builder. We have to write a script that will reference the codes we wrote in classes and tell the system the expected outcome. The whole code has to have a 75% coverage, which means at least 75% of the codes have to be tested. It makes a lot of sense though, but just really difficult to get started as I have never done this before.

2. Apex Class/Trigger is a different concept

The Apex Class and Trigger structure is also very new. In the beginning, I had a hard time understanding why do we need Apex Classes, while we can write everything in Apex Trigger. Just as in Flow Builder or Process Builder, we build everything in one place. Now I know that Apex Triggers are just for automation, while Apex Classes can be used in several places (Ex. Visualforce pages). Also, the best practice is we should always write the logic in classes, and should only reference the classes and methods in triggers

● Special Apex Syntax

1. Remember to use Bind Variable in SOQL (ex. : variableName)
2. Apex has Switch/When syntax, which is similar to Case() function

  • You can use the same Apex Trailmix that I used for studying
  • Basic Apex Concept:
    • Case insensitive
    • Like a limited version of Java
    • Uses SOQL, SOSL for querying data, and DML for modifying data
    • Governor Limits
  • Difference of Apex/Java
    • Java: Class => SFDC: Object
    • Java: Object => SFDC: Record
    • Primitive data type
    • Apex collection: List, Set, and Map
    • Multi-dimensional collections – Java: Datatype[][] => Apex: List<List<Datatype>>
    • Do-While loop in Apex
  • Difference of Apex/Other automation tools
    • Testing script
    • Deployment
    • Class/Trigger concept
  • Special Apex Syntax
    • Bind Variable in SOQL
    • Switch/When

These are some random thoughts in the learning for my own reference. Feel free to leave a comment for any of the items:

  • I have seen this phrase a lot: “Use Apex only if the declarative tools can’t do the job”. I am still looking for examples of what Flow can’t do.

Want to learn Flow? Check Out Flow Use Cases Or Write Us One!

Subscribe
Notify of
guest

4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Carla

Hi Melody 🙂 Here Carla again. So great to see your journey progressing and the challenges you’re facing. I will definitely get back to this post once I start my own studies.
I particularly liked the “Use Apex only if the declarative tools can’t do the job”. I am still looking for examples of what Flow can’t do.
In fact a colleague once told me that she wanted to first start as admin and really thoroughly understand the tool, so that she wouldn’t fall on that trap of just writing an apex class for anything that could be done with other tools.
Where would you recommend that a person who has never worked with Flow would start?

Mary

Hi Melody! Thank you for the trailmix you put together. I came across your blog recently and have been enjoying it.
I think Flow can do most things Apex can, but sometimes in a much clunkier way. I don’t think there’s the concept of Maps in flows (yet), which makes it hard to manipulate lists.