Installing Dark Mode for Salesforce Extension for Chrome [Recommended]

As a programmer you will spend hours at a strech staring at a screen figuring out solutions and writing code which makes it really important to protect your eyes. A good way to reduce the stress on eyes is using dark mode wherever possible. Salesforce doesn't provide an inbuilt dark mode UI, but it's possible to get there using a browser extension. The one I recommend is Dark Mode for Salesorce.

Download the extension from Chrome Web Store and follow on screen instructions

All screenshots through out the course will use Darkmode wherever possible. Note that this doesn't change any functionality in your Salesforce org, just changes the color of UI elements.

The Developer Console

Open developer console.

Open Developer Console in classic
Classic

Open Developer Console in lightning
Lightning

You should be greeted with a screen similar to this:

Developer Console with numbers and sections marked.

    Top Menu Bar

  1. File: This menu contains options to open, save, close and delete files and resources.
  2. Edit: This menu contains options to find and replace, fix indentation and code formatting.
  3. Debug: This menu contains options that relate to debugging and error fixing code.
  4. Test: This menu contains options related to testing our code and ensuring our code does what it's supposed to.
  5. Workspace: This menu contains options to setup UI for the developer console.
  6. Help: This menu contains general help topics like links to documentation, shortcut keys and preferences for the developer console.

  7. Text Editor

  8. Tabs: This space contains all open files in a tabbed format.
  9. Editor: This is where we write our code.

  10. Bottom Menu Bar

  11. Logs: This tab contains all logs from current and previous runs.
  12. Tests: This tab contains all reports from testing our code like code coverage and pass/fail results.
  13. Checkpoints: This tab contains information on checkpoints when setup in code.
  14. Query Editor: This option allows the developer to run SOSL and SOQL queries and validate them.
  15. View State: This tab allows you to examine the view state for Visualforce page request.
  16. Progress: This tab shows progress on running the current and previous code.
  17. Problems: This tab shows, in near real time, the errors available, if any, while writing our code.

Writing code

Now that we know where we write our code, let's write some of our first programs! Head over to Debug > Open execute anonymous window. This window allows us to write snippets of code. Alternativelt, you can press Ctrl and E to open quicker.

Let's make 2 integers and add them:

Integer a = 5;
Integer b = 4;
a + b;

Make sure the "Open logs after execution" is checked. Your screen should look something like this:

Running code in anon exec window

You will notice you're greeted with this screen:

Log screen
Line 3, Column 1:
Expression cannot be a statment 

The code in line 3, a+b is the equivalent to mixing coffee and water in nothing. We need a variable (cup) to hold them. Let's fix that:

Integer a = 5;
Integer b = 4;
Integer c = a + b;

You will notice you're automatically taken to this screen

Log screen

Click on Debug Only checkbox and you will notice it's empty.

Log screen

Here's what we did wrong. We took a calculator and pressed a + b, but we didn't press the = to see the result, the equivalent of = is using System.debug();.

System.debug(); is used to output text and variables in the log. Programmers use this handy line of code to debug and see values of variables throughtout it's runtime. The syntax for using it is:

System.debug(value);

Where value can be any Data type. Here are some examples:

System.debug(c);
System.debug("This is some text");
System.debug(45);

Let's add this to our code and run:

Integer a = 5;
Integer b = 4;
Integer c = a + b;
System.debug(c);

Now, click on Debug Only and you will notice your answer is there!

Showing logs

Understanding logs

Log breakdown
  • 03:51:26:002 is the timestamp of when the code was run. This is in the format HH:MM:ss:SS
  • USER_DEBUG [4]|DEBUG|9 is the event identifier. This includes information on who ran the code, the line which output the log, and the value from System.debug(); itself.

Basic Arithmetic Operations

  • Addition
  • Addition is done by using the + symbol between values / variables

    Integer a = 4;
    Integer b = 5;
    Integer c = a + b;
    Integer d = 4 + 5;
    Integer e = a + 5;
    
    System.debug(c);
    System.debug(d);
    System.debug(e);
    The results are 9 in every case.

  • Subtraction
  • Subtraction is done by using the - symbol between values / variables. This will subtract the value on the right from the value on the left.

    Integer a = 4;
    Integer b = 5;
    Integer c = a - b;
    Integer d = b - a;
    Integer e = 4 - 5;
    Integer f = a - 5;
    
    System.debug(c);
    System.debug(d);
    System.debug(e);
    System.debug(f);
    The results for c, e and f are -1, while d is 1.

  • Multiplication
  • Multiplication is done by using the * symbol between values / variables

    Integer a = 4;
    Integer b = 5;
    Integer c = a * b;
    Integer d = 4 * 5;
    Integer e = a * 5;
    
    System.debug(c);
    System.debug(d);
    System.debug(e);
    
    Integer f = -4;
    Integer g = a * f;
    System.debug(g);
    The results for c, d and e are 20, while g results to -20.

  • Division
  • Division is done by using the / symbol between values / variables. The value on the left is the numerator, and the one on the right is the denominator.

    Integer a = 4;
    Integer b = 5;
    Integer c = a / b;
    Integer d = 4 / 5;
    Integer e = a / 5;
    
    System.debug(c);
    System.debug(d);
    System.debug(e); 
Before reading further, run the above example and read the log. The answers are quite interesting 0, 0 and 0. There happens to be no decimals involved either!
This happens because an Integer Data Type only accepts whole numbers. If we wanted the exact number, a Decimal or Double data type would be more relevant. Try this code in Anon exec:

Integer a = 4;
Integer b = 5;

Decimal c = a / b;
System.debug(c);

On reading the log, the answer is still 0! This is because while c is a decimal, a and b are still integers! Let's switch our code again:

Decimal a = 4.0;
Decimal b = 5.0;
Decimal c = a / b;

System.debug(c);

The answer is 0.8! This happens because we have only 1 character after the decimal point. Take your time to experiment around this. What if a is 4.0 but b is 5.00? What if a is an integer and b is a decimal? There are no wrong answers here (yet)!

Let's use multiple operators now. Make 5 integers, assign them whole numbers and decimals at random and use multiple operators between them. Here's my code:

Integer a = 45;
Integer b = 18;
Decimal c = 4.0;
Decimal d = 5.0;
System.debug(a + b / c * d);
Before you run the code, use a pen and paper and calculate using BODMAS and PEMDAS rule before comparing values in the console. Noticed a difference? Just like BODMAS and PEMDAS, Apex has it's own order of arithmetic operations:

Precedence Operators Description
1 {} () ++ -- Grouping and prefix increments and decrements
2 new Unary negation, type cast and object creation
3 * / Multiplication and division
4 + - Addition and subtraction
5 < <= > >= instanceof Greater-than and less-than comparisons, reference tests
6 == != Comparisons: equal and not-equal
7 && Logical AND
8 || Logical OR
9 = += -= *= /= &= Assignment operators

If we want to follow a specific way of solving an operation, we enclose it in brackets ( ). Brackets are always solved inside out.

String Interpolation

Ever wonder how your instance of Salesforce org says "Good Morning <Your Name>"? This is possible by adding a variable to a string and is called String Interpolation. Try this code:

String myName = 'Harshdeep';
String hello = 'Hello ' + myName;
System.debug(hello); 
and the result will be Hello Harshdeep

Apart from personalization, it is really useful for writing logs to make them more readable. Let's write some more code:

Integer a = 5;
Integer b = 10;
Integer c = a + b;
String answer = 'The answer is ' + c;
System.debug(answer); 

The logs are so much easier to read now!. Take your time to make different variations using string interpolation. What if you want to output The answer is 15!?

Code Refactoring

While brewing your own coffee is fun, the cleaning up process can be mildly infurating because you ended up using 5 cups, 2 bowls, a pot, a french press, few spoons and a measuring cup which needs to be cleaned up. To deal with this, most of us would simply use less utensils. This can be done by using only 1 cup for measuring and rewiring our entire coffee making process to make less mess. This applies to code too. Let's take a look at a simple addition example:

integer a = 4;
integer b = 5;
integer c = a + b;
System.debug(c);
We made 3 variables here. One holds the number 4. One holds the number 5. One holds the result of addition. While a simple program like this doesn't take much to run, when we scale to write bigger, more complex programs, it'll take too much memory and computing power to run.

This is comparable to the simple act of turning off your lights when you leave your room to save electricity. You make a small difference towards your electricity usage, but when the entire family does it, then the entire neighborhood and then the entire city, there's a dramatic reduction in electricity consumption.

Code refactoring means to write code that has reusable blocks which ends up running faster and taking less memory, while having the same output.

Let's refactor our code. First, let's remove the variable c, and directly output a+b in our log:

Integer a = 4;
Integer b = 5;
System.debug(a+b);

Now, let's declare both our integers in the same line, using a , as a separator:

Integer a = 4, b = 5;
System.debug(a+b);
Notice how we went from 4 lines of code to 2. We also use one less variable and we get the same result, while also reusing the variables we already had. Code refactoring will play a super important role later in more complex code.

Code refactoring is important because Salesforce orgs run on a multi-tenant environment. This is similar to living in an apartment with strangers and sharing hallways and kitchen but having a personal room. Similarly, multiple orgs run on a server and use shared computing resources with limits called as Governor Limits. We will explore governor limits in depth later in this course.

Refactoring Arithmetic Operations

Certain arithmetic operations can be rewritten for ease of readibility. This will come handy when using loops.

  • ++ adds 1 to the value and assign it back to the variable.
  • -- subtracts 1 from the value and assign it back to the variable.
  • *= multiplies the variable with the number on the right and assign it back to the variable.
  • a *= 10; is equivalent to a = a * 10;

  • /= divides the variable with the number on the right and assign it back to the variable.
  • a /= 10; is equivalent to a = a / 10;

Summary

  • It's important to take care of your health as a programmer right from day 1 you could rely on the author telling you about it on day 3.
  • The results of arithmetic operations depends on the data type of parent values.
  • System.debug(); is a powerful and necessary tool to log values during runtime.
  • It's important to refactor code and value computing power and resources.

Note

The author is aware there are a lot of concepts marked as We will deal with this later. The course assumes this is your first time writing code and overloading with a lot of information that doesn't make sense immediately can be counter productive.

Day 4: Conditional Operators, More Arithmetic Operations and Loops