Skip to content

Instantly share code, notes, and snippets.

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
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 / 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);
}
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
@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);
}
@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.
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;
public class ContactBeforeInsert {
public static void setContactTitle(List<Contact> cons){
for(Contact c : cons){
c.Title = 'Admin';
}
}
}
@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';
}
}