Apex Basics & Database Flashcards
(303 cards)
As a language, Apex is (9 things):
- Hosted—Apex is saved, compiled, and executed on the server—the Lightning Platform.
- Object oriented—Apex supports classes, interfaces, and inheritance.
- Strongly typed—Apex validates references to objects at compile time.
- Multitenant aware—Because Apex runs in a multitenant platform, it guards closely against runaway code by enforcing limits, which prevent code from monopolizing shared resources.
- Integrated with the database—It is straightforward to access and manipulate records. Apex provides direct access to records and their fields, and provides statements and query languages to manipulate those records.
- Data focused—Apex provides transactional access to the database, allowing you to roll back operations.
- Easy to use—Apex is based on familiar Java idioms.
- Easy to test—Apex provides built-in support for unit test creation, execution, and code coverage. Salesforce ensures that all custom Apex code works as expected by executing all unit tests prior to any platform upgrades.
- Versioned—Custom Apex code can be saved against different versions of the API.
Apex is like other object-oriented programming languages because it supports these language constructs:
- Classes, interfaces, properties, and collections (including arrays).
- Object and array notation.
- Expressions, variables, and constants.
- Conditional statements (if-then-else) and control flow statements (for loops and while loops).
Apex is NOT like other object-oriented programming languages in that it supports:
- Cloud development as Apex is stored, compiled, and executed in the cloud.
- Triggers, which are similar to triggers in database systems.
- Database statements that allow you to make direct database calls and query languages to query and search data.
- Transactions and rollbacks.
- The global access modifier, which is more permissive than the public modifier and allows access across namespaces and applications.
- Versioning of custom code.
Is Apex case-sensitive or case-sensitive?
case-INsensitive
Apex is one data type specific to Salesforce. What is it?
sObject
What data types are supported by Apex?
- A primitive, such as an Integer, Double, Long, Date, Datetime, String, ID, Boolean, among others.
- An sObject, either as a generic sObject or as a specific sObject, such as an Account, Contact, or MyCustomObject__c (you’ll learn more about sObjects in a later unit.)
- A collection, including:
- A list (or array) of primitives, sObjects, user defined objects, objects created from Apex classes, or collections
- A set of primitives
- A map from a primitive to a primitive, sObject, or collection
- A typed list of values, also known as an enum
- User-defined Apex classes
- System-supplied Apex classes
Why is creating a List easier than creating an Array?
Lists don’t require you to determine ahead of time how many elements you need to allocate.
Name a benefit of using Apex classes.
Code reuse. Class methods can be called by triggers and other classes.
Why is Anonymous Apex a handy tool?
Allow you to run lines of code on the fly and is a handy way to invoke Apex, especially to test out functionality. Debug logs are generated as with any other Apex execution.
Before you can insert a Salesforce record, you must …
…create it in memory first as an sObject.
Ex: Account acct = new Account();
How many ways can you add fields to an sObject, and what does the syntax look like?
1. through a constructor Account acct = new Account(Name='Acme', Phone='(415)555-1212', NumberOfEmployees=100);
2. using dot notation Account acct = new Account(); acct.Name = 'Acme'; acct.Phone = '(415)555-1212'; acct.NumberOfEmployees = 100;
Is dot notation available when working with a generic sObject?
No, because a generic sObject doesn’t know what it is, so there are no properties accessible.
But, you can cast the generic sObject as a specific sObject to access its properties, e.g.:
// Cast a generic sObject to an Account
Account acct = (Account)myGenericSObject;
// Now, you can use the dot notation to access fields on Account
String name = acct.Name;
String phone = acct.Phone;
Describe how creating generic sObjects is different from creating specific sObjects.
Generic sObjects can only be created through the newSObject() method. Also, the fields can only be accessed through the put() and get() methods.
To retrieve a record, you have to use what?
SOQL - Salesforce Object Query Language
To insert an sObject as a record, you have to use what?
DML - Data Manipulation Language
What does DML provide?
Straightforward way to manage records by providing simple statements to insert, update, merge, delete and restore (undelete) records.
How is Apex DML different from other programming languages?
Other programming languages require additional setup to connect to data sources, but Apex DML allows quick access to perform operations on SF records because Apex is data focused.
What does the “merge” DML statement do?
Merges up to 3 records of the same sObject into one record, Deletes the others, and re-parents any related records.
How would you retrieve the ID of a newly created record in Salesforce?
On insert, the ID is automatically returned to the sObject variable used for insert, so it’s immediately accessible.
What is the max number of DML statements per Apex transaction?
150
If you don’t specify a field when doing an “upsert” statement, what field is used by default?
The sObject’s Id.
What fields can be used during an “upsert” statement to identify records?
For custom objects, the Id field or any other field marked as an external ID.
For standard objects, the Id field and any field that has idLookup property set to True. For example, Email on both Contact and User objects has this property set.
upsert sObjectList Account.Fields.MyExternalId;
What happens if multiple matching records are found during an “upsert” statement?
An error is generated and the object record is neither inserted or updated.
How many days does a deleted record live in the Recycle Bin?
15 days