Skip to content

Instantly share code, notes, and snippets.

@brianmfear
brianmfear / MonitorAsyncUsage.cls
Last active November 30, 2023 19:01
Notify Admin when approaching Async Apex usage limit
public class MonitorAsyncUsage implements Schedulable {
public class ResourceInfo {
public Integer Max;
public Integer Remaining;
}
public class LimitsAPI {
public ResourceInfo DailyAsyncApexExecutions;
}
@brianmfear
brianmfear / ExecuteAnonymous.java
Created November 27, 2023 14:55
SSCCE of running a 100k+ SOSL query
String[] objects = new String[0];
Set<String> exceptions = new Set<String> { 'caseexternaldocument' };
for(sObjectType t: Schema.getGlobalDescribe().values()) {
DescribeSObjectResult r = t.getDescribe();
if(r.isSearchable() && !exceptions.contains((''+t).toLowerCase())) {
objects.add(t+'('+string.join(r.fields.getMap().keySet(),',')+')');
}
}
String searchTerm = 'find {demo} in all fields returning '+string.join(objects,',');
System.debug(searchTerm.length());
@brianmfear
brianmfear / ExecuteAnonymous.java
Created November 26, 2023 22:14
SSCCE of running a 250k character SOQL query
// WARNING: uses lots of CPU time, set all log levels to NONE, Apex to ERROR.
String[] charset = new String[0];
for(Integer i = 48; i < 58; i++) {
charset.add(String.fromCharArray(new Integer[]{i}));
}
for(Integer i = 65; i < 91; i++) {
charset.add(String.fromCharArray(new Integer[]{i}));
}
for(Integer i = 97; i < 123; i++) {
charset.add(String.fromCharArray(new Integer[]{i}));
@brianmfear
brianmfear / StripInaccessibleAction.cls
Created November 22, 2023 02:12
Adding Security.stripInaccessible to a flow
public class StripInaccessibleAction {
public class Input {
@InvocableVariable(
description='The record to clean up'
label='Record'
required=true
)
public sObject record;
@InvocableVariable(
description='The access type to use'
@brianmfear
brianmfear / GetExtraLimitsForYourUnitTest.apxc
Last active January 11, 2024 05:09
Need more CPU or heap for creating your test data? Make it Queueable.
// Asking for extra CPU time and heap for those really "heavy" orgs
@isTest class GetExtraLimitsForYourUnitTest implements Queueable {
@testSetup static void testSetup() {
Test.startTest(); // TIP: this avoids governor limits for your @isTest methods
System.enqueueJob(new GetExtraLimitsForYourUnitTest()); // We'll get async limits!
// P.S. Did you know that the end of a unit test method triggers async code,
// just as if you called Test.stopTest()?
}
public void execute(QueueableContext context) {
// I now have 60000ms to do my setup, instead of just 10000ms.
public class CsvParser {
String csv;
Integer rowCount;
Integer cellCount;
final static String END_LINE = '\r';
final static String END_CELL = ',';
final static String ESCAPE_CHAR = '"';
final static String SPACE = ' ';
public CsvParser(String csvString) {
// https://salesforce.stackexchange.com/a/376822/2984
// Efficiently checking if any fields from a list of fields have changed
public class DataUtils {
public class Change {
public String[] changedFields = new String[0];
public sObject record1, record2;
Change(String[] changedFields, sObject record1, sObject record2) {
this.changedFields = changedFields;
this.record1 = record1;
@brianmfear
brianmfear / Logger.cls
Created April 5, 2022 10:30
Sample Logger in Apex (does not store the logs; do whatever you want here)
public class Logger {
static Pattern thePattern = Pattern.compile(
'(?i)^(?:class\\.)?([^.]+)\\.?([^\\.\\:]+)?[\\.\\:]?([^\\.\\:]*): line (\\d+), column (\\d+)$'
);
static Log__c[] logs = new Log__c[0];
static LoggingLevel controlLevel = LoggingLevel.DEBUG;
public static void addLog(String message) {
addLog(new DmlException(), controlLevel, message, null);
}
<aura:application >
<aura:attribute name="show" type="Boolean" default="false" />
<aura:if isTrue="{!v.show}">
<c:q369501c />
</aura:if>
</aura:application>
@brianmfear
brianmfear / label.app
Created February 14, 2022 20:25
Retrieve a label dynamically in LWC
<aura:application extends="force:slds">
<c:q317434 />
</aura:application>