Skip to content

Instantly share code, notes, and snippets.

@AlwaysThinkin
AlwaysThinkin / Batch Field Updater
Last active February 23, 2024 01:27
A generic Apex Batch Update to update a field on all records for an sObject class based on the examples from the Developer Guide
/* BatchRecordUpdate will update any 1 field on any 1 object.
*
* String values must be set to Object, Field and Value to be added to Field.
*
* Query can be modified to limit records updated.
* Example below excludes records for which Degree Offering already equals the new value.
*
* All 4 strings' values must be set to operate when executed.
*
* Execute in Anonymous Apex.
/* 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.
*/
@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);
trigger AccountTrigger on Account (after update) {
//prepare an empty Map of ID-to-Account for your updates
Map<ID, Account> acctMapToUpdate = new Map<Id, Account>();
//prepare a for-loop you can use to compare old and new field values
for(Integer i = 0 ; i < trigger.new.size() ; i++){
Account old = trigger.old[i];
Account nw = trigger.new[i];
//Evaluate Custom Settings to decide whether to execute actions
trigger LeadTrigger on Lead (After Update) {
Map<Id, Lead> leadsToUpdate = new Map<Id, Lead>();
List<Competitor__mdt> competitorRecords = [Select Label from Competitor__mdt];//Retrieve our Custom Metadata Records
List<String> competitorNames = new List<String>();//Instantiate an empty list to hold the names of our competitors
for(Competitor__mdt record : competitorRecords){//Loop through the Custom Metadata records our query returned
competitorNames.add(record.Label);//Add them to our empty list
}
for(Integer i = 0 ; i < trigger.new.size() ; i++){
@AlwaysThinkin
AlwaysThinkin / LogUtility Class
Last active June 29, 2019 15:17
Files for Exception Handling Session
public class LogUtility {
public static void messageLog(String classMethod, String customMessage, Set<Id> recordIds) {
Case logEntry = new Case(
Type = 'Message',
Subject = customMessage,
Description = 'Record IDs: ' + recordIds + '\n'
+ customMessage);
insertLog(logEntry, classMethod);
}
trigger CompetitorContactTrigger on Contact (before insert, before update) {
//Get a list of all our Competitors' names from our Custom Metadata
//Create an empty List of Strings to hold your Competitors' names
for(){//Query your Custom Metadata and get its Label field
//Add the Label to your List of Strings
}
//Create a Map of IDs to Accounts; populate your map on the same line by putting a query in the parentheses.
//Your query will just get Account IDs from Account where the Name is in your List of Strings
@AlwaysThinkin
AlwaysThinkin / 1 LeadTriggerTest
Last active June 22, 2019 13:43
Guided exercise to create a comparison trigger on Lead to set Rating = Cold on Closed - Not Converted Leads
@isTest
public class LeadTriggerTest {
public static testMethod void testOneClosedNotConverted(){
Lead l = new Lead();
l.LastName = 'Lead Last Name';
l.Company = 'Lead Company';
insert l;
test.startTest();
/* A simple batch Apex class to demonstrate updating a field on Opportunity
* Can be executed from Anonymous Apex using 2 lines:
* OpportunityBatch oppBatch = new OpportunityBatch();
* database.executeBatch(oppBatch);
*/
global class OpportunityBatch implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext BC) {
String query = 'SELECT Id,Name FROM Opportunity';
return Database.getQueryLocator(query);
}