Skip to content

Instantly share code, notes, and snippets.

View DataUtil.cls
// 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 Apr 5, 2022
Sample Logger in Apex (does not store the logs; do whatever you want here)
View Logger.cls
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);
}
View demo.app
<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 Feb 14, 2022
Retrieve a label dynamically in LWC
View label.app
<aura:application extends="force:slds">
<c:q317434 />
</aura:application>
@brianmfear
brianmfear / wSharingTest.apxc
Created Feb 9, 2022
Without Sharing overridden by System.runAs?
View wSharingTest.apxc
@isTest without sharing class wSharingTest {
@isTest static void test() {
Account a = new Account(Name='Name');
User u = [SELECT FIELDS(STANDARD) FROM User WHERE Id = :UserInfo.getUserId()].deepClone(false, false, false);
u.FederationIdentifier = '12345';
u.Alias = '12345678';
u.UserName += '.brian.fear';
u.CommunityNickname = '12345678';
u.ProfileId = [SELECT Id FROM Profile WHERE Name LIKE 'Standard%' LIMIT 1].Id;
System.runAs(new User(Id=UserInfo.getUserId())) {
View README.md

Save the file locally, decode with:

base64 -d -i q365527.zip.base64 > q365527.zip

Then decompress these files into a force-app/main/default/ directory.

Finally, deploy to your org:

sfdx force:source:deploy -p force-app
View README
Download this as a ZIP file, move the files so they line up in the proper places, then deploy.
force-app/main/default/classes/StockMarketSharesWrapper.cls
force-app/main/default/classes/StockMarketSharesWrapper.cls-meta.xml
force-app/main/default/lwc/wrapperList/wrapperList.html
force-app/main/default/lwc/wrapperList/wrapperList.js
force-app/main/default/lwc/wrapperList/wrapperList.js-meta.xml
Support for question https://salesforce.stackexchange.com/q/365372/2984
View Utils.apex
// Regexp from https://stackoverflow.com/questions/201323/how-can-i-validate-an-email-address-using-a-regular-expression
public class Utils {
public static String[] findEmailsInString(String source) {
String emailRegex = '(?:[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*|"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])';
Pattern p = Pattern.compile(emailRegex);
Matcher m = p.matcher(source);
String[] emails = new String[0];
while(m.find()) {
emails.add(m.group(0));
}
View RegexFaster.apex
// Randomly select A-Z, a-z, 0-9
String randomStringAllEnhanced(Integer count) {
Integer[] chars = new Integer[0],
offsets = new Integer[] { 48, 65, 97 },
mods = new Integer[] { 10, 26, 26 };
while(count > chars.size()) {
Integer rnd = Math.abs(Crypto.getRandomInteger()), cat = Math.mod(rnd, 3), seed = Math.mod(rnd, mods[cat]);
chars.add(seed+offsets[cat]);
}
return String.fromCharArray(chars);
@brianmfear
brianmfear / path.cmp
Created Jul 6, 2021
Custom Path Component in Aura
View path.cmp
<aura:component >
<aura:attribute name="currentStep" type="String" />
<aura:attribute name="steps" type="String[]" />
<aura:attribute name="renderInfo" access="private" type="Object[]" />
<aura:registerEvent name="onselect" type="c:valueSelected" />
<aura:handler name="init" value="{!this}" action="{!c.update}" />
<aura:handler name="change" value="{!v.steps}" action="{!c.update}" />