Last Updated on September 18, 2021 by Rakesh Gupta
In my last article Mastering Apex Variables – a Stepping Stone in your Apex Journey I discussed about Variables and Data Types. There were few assignments for you in that article, if you did not get a chance to complete it, do it right now. It will not take more than 30 minutes to complete them.
Irrespective of whether you are a newbie, just starting a career as a Salesforce Developer, or have some programming background, I would suggest you complete the assignments. The assignments will help you to, either learn few new things or, at the least, brush up your concepts.
Many of you wonder, and have asked me repeatedly – Are there any prerequisites to start learning Apex Programming? To that, my simple answer is – yes! However, my recommended prerequisites are not practicing ABCs of Apex! But, it is to cultivate your ability to build logic! My recommendation is not only based on my own experience but, also that of my friends and mentees – many of whom did not have any programming background; yet, by sharpening their abilities to develop models logically – i.e., develop Pseudocodes – almost on the fly – they have gone on to become great Developers.
Many Apex learners before you have started, and many after you will start, their Apex journey without any programming background. Out of this bunch, many have gone on to become successful Developers and many will do the same in the future. In short, if you venture your Apex journey without any programming experience or background, you will not be bestowed with a ‘pioneer medal’! But, if you do follow my suggestions and recommendations, the probability that you will go on to become a sought after Salesforce Developer is high.
The only other thing that you will need, after cultivating logical skills, is confidence! That is what I learnt from my experience – If a guy like, me who did not go to an English Medium school, or got a formal English language training, can publish 5 books – back-to-back – then anyone can achieve anything as long as they are determined to do so. Apex is just a programming language – not a rocket science! You can do it! So, let us get started – dust yourself, cultivate a logical mind and, take your next step with full force and confidence!.
Optimism is the faith that leads to achievement. Nothing can be done without hope and confidence.
Salesforce offers lots of content to get you started on your Apex journey. If you are just starting your Salesforce Developer career then read this article – Creatively Decode Your Salesforce Developer Journey – to get some guidance, as well as, to learn how to achieve your Salesforce Developer goal. Not convinced, and still worried about not having any programming background? Fine, then my recommendation is to meticulously read ‘Head first Java’ book. This is one of the highly recommended programming books for beginners; it won’t hurt to master it before you take a plunge into the world of Apex.
My primary purpose, through this and other blogs, is to be your guide and a mentor. As for Apex learning tools – such as browsing through Apex content to get you started on learning the language – there is no shortage of material available on the Web. For example, search for ‘best practice to write Apex trigger’ and you will end up with hundreds of article links. If, however, you fail to start your journey with a right mindset, it is unlikely that you will go far by practicing the codes and triggers mindlessly. So, let us start your Apex journey strategically!
Rules for Naming Variables
Apex is case insensitive. However, there are certain best practices that are followed in order to make code readable and easy to maintain. Naming conventions are, of course, a matter of personal preference. My suggestion is, to become a great Salesforce Developer, follow naming conventions religiously:
Class — MyClass — each letter of the starting word needs to be capitalized. It means use PascalCase for it.
Method and Variables — myTrailheadBadges — the first letter of the starting word to be lower case and subsequently uppercase. It means use camelCase for it.
Constant – EARTH_RADIUS — are generally written with UPPERCASE and sometimes segmented by an underscore.
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Operators can be divided into three categories: mathematical, comparison, and logical.
Mathematical operators: To perform a mathematical operation with two numbers (or two numbers represented by variables), use these symbols in your Apex program. Assume variable A store 10 and variable B store 20, then:
|+||Addition||A + B will give 30|
|–||Subtraction||A – B will give -10|
|*||Multiplication||A * B will give 200|
|/||Division||B / A will give 2|
|++||Incremented by 1||A++ will give 11|
|—||Decremented by 1||A– will give 9|
String operators: Use the following operators to Concatenate two strings.
Assignment operators: An assignment operator assigns a value to its left operand based on the value of its right operand.
|=||Assigns values from right side operands to left side operand||C = A + B will assign value of A + B into C|
|+=||It adds right operand to the left operand and assign the result to left operand||C += A is equivalent to C = C + A|
|-=||It subtracts right operand from the left operand and assign the result to left operand||C -= A is equivalent to C = C – A|
Comparison operators: These operators compare two expressions to determine whether they are equal, and if not, how they differ. Assume variable A stores 10 and variable B stores 20, then:
|==||Equality comparison||A==B, is not true|
|!=||Not Equals||A!=B, is true|
|>||Greater than||B>A, is true|
|<||Less than||A<B, is true|
|>=||Greater than or equal||B>=A, is true|
|<=||Less than or equal||B<=A, is not true|
Logical operators: Logical operators are mainly used to control program flow. Assume variable A stores 1 and variable B stores 0, then:
|&&||Logical AND||A&&B, is false|
|||||Logical OR||A||B, is true|
|!||Logical NOT||!(A&&B), is true|
Now look at these examples,
Integer badgesThisMonth = 20; //Trailhead badges earned in the current month Integer badgesLastMonth = 40; //Trailhead badges earned in the last month Integer totalBadges = badgesThisMonth+ badgesLastMonth; // Total Trailhead badges earned
Notice one thing, before creating a variable we are adding a Data Type. For example, we have used Integer Data Type for badgesThisMonth variable.
To create a variable follow the structure below:
Try to predict the answer and use Execute Anonymous, in the Developer Console, to run the code and tally with your answer.
Once you are done with predicting the answer, click on the Execute (2) button. Make sure to select Open Log (1) check box. Once you do that, the log will automatically open in the Log Inspector after the code executes.
To better analyze the variables’ run time value, make sure to select the Executable check box, as shown in the following screenshot:
That is it for today’s blog. In the next article, I am going to discuss system.debug and how to use it.
- Assume integer variable A stores 10, then what is the outcome of B= A+2.
- Assume integer variable A stores 13, then what is the outcome of (A+2)*3+(A-5).
- Assume boolean variable A and B both stores True, then what is the outcome of !(!(!A&&B)).
- Assume integer variable A stores 13, then what is the outcome of (A++)+(++A)+A.
- Assume integer variable A stores 10 and B stores 2, then what is the outcome of A+B*A+10/2*A.
Note: – I am thankful to Smartcloud.io for sponsoring my blog.
Technical Editor and Proofreader: - Munira Majmundar