Skip to content

Instantly share code, notes, and snippets.

@AlwaysThinkin
AlwaysThinkin / 2-DataTypes.java
Last active July 9, 2020 01:00
This file for practicing basic Salesforce Data Types must be saved in a new Apex Class (any name is OK). Certain lines have FIX ME!!! in their comments, these are the practice lines where you can add in the missing Apex and check the Debug logs to see if you get the expected result when you run the test method.
@isTest
public static TestMethod void DataTypes(){
string text1 = 'this text';
string text2 = 'that text';
string text3 = text1 + text2;
System.debug(text3);
text3 = text3 + 'more text';
System.debug(text3);
@AlwaysThinkin
AlwaysThinkin / ContactAfterTrigger.trg
Last active June 8, 2019 14:53
After Trigger and its unit test
trigger ContactAfterTrigger on Contact (after insert) {
List<Contact> consToUpdate = new List<Contact>();
for(Contact conInTrigger : trigger.new){
Contact c = new Contact();//Create a new instance of an as-yet-unidentified Contact
c.Id = conInTrigger.Id;//Set that Contact's ID to an existing ID from the Contacts in Trigger.new
c.Department = 'IT';//Change a field value on that Contact
consToUpdate.add(c);//Add it to our List so we can update it later when all Contacts in Trigger.new have been prepared
}
update consToUpdate;//Perform the DML update call outside of our for-loop to avoid Governor Limits (150 DML per transaction)!
@AlwaysThinkin
AlwaysThinkin / ContactCompareTrigger.trg
Last active June 8, 2019 15:30
"IsChanged" Trigger and its unit test
trigger ContactCompareTrigger on Contact (after update) {
//Create an empty Map of Contacts for eventual updates
Map<ID, Contact> consToUpdate = new Map<Id, Contact>();
//This is a very common pattern for iterating over the list of records
//We use the size() method for Lists to handle each Contact in Trigger.new
for(Integer i = 0 ; i < trigger.new.size() ; i++){
Contact old = trigger.old[i];
Contact nw = trigger.new[i];//We cannot use "new" as our variable name, it's a reserved word.
@AlwaysThinkin
AlwaysThinkin / LeadTrigger
Last active June 25, 2017 07:19
Trigger to handle multiple unrelated updates safely
trigger LeadTrigger on Lead (After Update) {
Map<ID, Lead> leadsToUpdate = new Map<ID, Lead>();//Create a Map to hold eventual updates
//This is a very common pattern for iterating over the list of records
//We use the size() method for Lists to handle each Lead in Trigger.new
for(Integer i = 0 ; i < trigger.new.size() ; i++){
Lead old = trigger.old[i];
Lead nw = trigger.new[i];//We cannot use "new" as our variable name, it's a reserved word.
@isTest
public class DataTypeTestAnswers {
public static TestMethod void DataTypes(){
string text1 = 'this text';
string text2 = 'that text';
string text3 = text1 + text2;
System.debug(text3);
text3 = text3 + 'more text';
@isTest
public class Collections {
static testMethod void Lists(){
//LISTS ARE AN ORDERED COLLECTION OF DATA
//How to instantiate a List with some pre-defined values
String someword = 'thread';
List<String> threeStrings = new List<String>{'rope', 'twine', someword};
//Lists have methods that allow us to evaluate or manipulate them
@isTest (seeAllData = true)
public class SOQLqueries {
static testMethod void relationshipQueries(){
//Child-to-Parent queries are easy. Simply use the API Name of the lookup field to the parent object
//and use dot notation to specify the field on that object you want
List<Contact> conAccts = [SELECT FirstName, LastName, Account.Name FROM Contact];
//FIX ME! Use a query with a WHERE filter to only return Contacts from the Account named 'sForce'
//List<Contact> sForceconAccts = ???
@isTest
public class CollectionsAnswers {
static testMethod void Lists(){
//LISTS ARE AN ORDERED COLLECTION OF DATA
//How to instantiate an List with some pre-defined values
String someword = 'thread';
List<String> threeStrings = new List<String>{'rope', 'twine', someword};
//Lists have methods that allow us to evaluate or manipulate them
/* This Apex Class is a Unit Test Class and all the code can be checked by running the test.
* Copy this into a new Apex Class named "ControlFlow" and save it.
* As you read through it, look for "FIX ME!" and make the changes suggested.
* If your changes are correct all the tests (System Asserts) will pass.
*
* You can also see what's happening in the Logs by opening a Log and checking the Debug Only box
* you can compare the line numbers in the Log to the line number in the Class to find a specific
* debug entry.
*/
/* This Apex Class is a Unit Test Class and all the code can be checked by running the test.
* Copy this into a new Apex Class named "ControlFlow" and save it.
* As you read through it, look for "FIX ME!" and make the changes suggested.
* If your changes are correct all the tests (System Asserts) will pass.
*
* You can also see what's happening in the Logs by opening a Log and checking the Debug Only box
* you can compare the line numbers in the Log to the line number in the Class to find a specific
* debug entry.
*/