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)
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
- Class/Trigger concept
- Special Apex Syntax
- Bind Variable in SOQL
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!
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?
Thanks Carla! I know, I am struggling to find easy use cases that Apex would do a better job (In fact this is the opening line for my next episode)
Depending on your learning style, there are two ways to start with flow:
1. If you like to build from templates and learn the concepts along the way:
I think https://jenwlee.com/ and https://automationchampion.com/ are great because they provide many examples with all the details
2. If you like to know the basic concepts first then start building:
My blog is designed for this purpose. You can find the intro to the basic concepts under Intro and How To, then go to the Use Case for practice examples
I first started with method 1 – just the the simplest trial and error. It is a good approach but I realized it took a lot of time searching for different concepts. This is why I wanted to consolidate everything I know, so it will be easier for new users to search 🙂
I am also working on converting them into videos, and will try to summarize a learning path for Flow too. Good luck with the learning!
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.
Hi Mary, glad that you like the content and can’t agree with you more! The fact that we can’t use indexing in Flow is also a bit troublesome, but hopefully they can support this feature soon