Skip to content

Instantly share code, notes, and snippets.

List<Task> tasks =
[SELECT Id,
WhatId,
TypeOf What
WHEN Account THEN BillingState, BillingCity
END
FROM Task
WHERE WhatId In (SELECT Id
FROM Account)];
@lfreeland
lfreeland / gist:17dcc527e75c0102d1fad9cda039b85a
Last active September 27, 2019 10:13
Valid Related To Account Example
SELECT Id,
WhatId,
TypeOf What
WHEN Account THEN BillingState, BillingCity
END
FROM Task
WHERE WhatId In (SELECT Id
FROM Account)
@lfreeland
lfreeland / gist:78c33bef7b7663d6442a5c741240d87c
Last active September 27, 2019 10:14
Not Valid Related To SOQL Example
SELECT Id,
WhatId,
What.Name,
What.BillingState
FROM Task
WHERE WhatId In (SELECT Id
FROM Account)
@isTest
public class OCRServiceTest {
@isTest
static void runOCRRollupsTest() {
Test.enableChangeDataCapture();
Account acct = new Account(Name = 'Metillium Inc');
insert acct;
Contact c1 = new Contact(
@lfreeland
lfreeland / gist:09991d9377f7e1b7ebef71f5be56ba40
Last active July 1, 2019 13:31
OpportunityContactRoleChangeService Trigger
trigger OCRChangeEventTrigger on OpportunityContactRoleChangeEvent (after insert) {
new OCRService().runOCRRollups(Trigger.New);
}
public without sharing class OCRService {
public void runOCRRollups(List<OpportunityContactRoleChangeEvent> changeEvents) {
Set<String> oppContactRoleIds = new Set<String>();
for (OpportunityContactRoleChangeEvent evt : changeEvents) {
EventBus.ChangeEventHeader header = evt.ChangeEventHeader;
oppContactRoleIds.addAll(new Set<String>(header.recordids));
}
@lfreeland
lfreeland / gist:ed2d34288e7694a9f8ca12d65a6a429a
Created February 22, 2019 17:56
Simple Trigger Framework Insert Validation Trigger Example
trigger AccountTrigger on Account (after delete, after insert, after undelete, after update, before delete, before insert, before update) {
new AccountTriggerHandler().run();
}
@lfreeland
lfreeland / gist:18fd5691ad3ba61f1001cc4815c0613f
Last active February 22, 2019 17:55
Simple Trigger Framework Insert Validation Handler Example
public with sharing class AccountTriggerHandler extends TriggerHandlerBase {
protected override void beforeInsert(List<Sobject> newRecords) {
List<Account> newAccounts = (List<Account>) newRecords;
for (Account newAcct : Trigger.new) {
if (newAcct.Name == ‘bad’)
newAcct.addError(‘Bad Name’);
}
}
}
@lfreeland
lfreeland / gist:ce1b1a00dc78e5e61516323c0096d490
Last active February 22, 2019 17:21
Update Trigger Validation Example
trigger myAccountTrigger on Account (before delete, before update) {
if (Trigger.isBefore) {
if (Trigger.isUpdate) {
for (Account newAcct : Trigger.new) {
Account oldAcct = Trigger.oldMap.get(newAcct.Id);
if (oldAcct.Name != newAcct.Name &&
newAcct.Name.containsIgnoreCase(‘Luke’)
newAcct.addError(‘Choose a better name’);
}
}
@lfreeland
lfreeland / gist:53a1055e7f59c791fec6cd52c45c9153
Last active February 22, 2019 17:01
Insert Trigger Validation Example
trigger myAccountTrigger on Account (before delete, before insert) {
if (Trigger.isBefore) {
if (Trigger.isInsert) {
for (Account newAcct : Trigger.new) {
if (newAcct.Name == ‘bad’)
newAcct.addError(‘Bad Name’);
}
}
}
}