Skip to content

Instantly share code, notes, and snippets.

@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';
public class ContactAfterInsert {
public static void setContactDeptIT(List<Contact> cons){
List<Contact> consToUpdate = new List<Contact>();
for(Contact conInTrigger : cons){
Contact c = new Contact(Id = conInTrigger.Id, Department = 'IT');
consToUpdate.add(c);
}
update consToUpdate;
@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
@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 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 = ???
@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 / ContactBeforeTrigger.trg
Created September 22, 2016 22:09
Before Trigger and its unit test
Trigger ContactBeforeTrigger on Contact (before insert) {
for(Contact c : Trigger.new){
c.Title = 'Admin';
}
}
public class ContactBeforeInsert {
public static void setContactTitle(List<Contact> cons){
for(Contact c : cons){
c.Title = 'Admin';
}
}
}