Preface – This post is part of the Core Apex Tools series.
In this series we’ll cover the Apex fundamentals that I guarantee you’ll use in 100% of your code!
Data types are simply the different types of variables you can have. You can name your variables whatever you like as long as they’re one word. It’s an unwritten rule to have your variables in camel case.
Here’s an example of a String:
String favoriteBeer = 'Mikes Hard Lemonade';
Strings are simply text variables. You use them with all text, email, or picklist type fields in Salesforce. Notice how Strings are always wrapped in single quotes!
Integer numberOfBeers = 6; Decimal bloodAlcoholLevel = 0.15;
These are used in number, currency, and percentage fields in Salesforce. Integers are for number fields without any decimal places.
Boolean isDrunk = true;
Booleans are just either true or false. They’re used with checkboxes in Salesforce.
Date courtDate = Date.today().addDays(30); DateTime timePulledOver = DateTime.now();
Contact me = [SELECT FirstName, LastName FROM Contact WHERE Email = 'firstname.lastname@example.org' LIMIT 1];
Here we queried Salesforce for a record using SOQL and stored the result in an object variable… Apex is so awesome!
sObject is just a fancy name for an object in Salesforce. Custom objects will work too! We could’ve done something similar using a Prisoner__c sObject.
Next post: Data collections – lists, sets, and maps!