Salesforce Best Features available
1. Get Merged Body Without Extra Effort
Use the renderStoredEmailTemplate method to get the merged email body automatically without manually replacing merge fields.
EmailTemplate emailTemplate = [SELECT Id FROM EmailTemplate WHERE Name = 'Test1' LIMIT 1];
Messaging.SingleEmailMessage email = Messaging.renderStoredEmailTemplate(emailTemplate.Id, UserInfo.getUserId(), AccountObj.Id);
System.debug(email.plainTextBody);
2. Business Hour Calculations Example in Apex
This snippet checks whether a specific time falls within your organization's default business hours.
public static Boolean checkBusinessHour() {
// Business hour logic
List<BusinessHours> defaultBusinessHours = [SELECT Id FROM BusinessHours WHERE Name = 'Default' AND IsDefault = TRUE AND IsActive = TRUE LIMIT 1];
// Find whether the time is within the default business hours
return BusinessHours.isWithin(defaultBusinessHours[0].Id, System.now());
}
3. Check Boolean Expression in Apex
A utility class designed to dynamically evaluate complex logic strings containing AND/OR expressions and parentheses.
public class BooleanExpressionHandler {
static Map<String, String> logicTypes = new Map<String, String>();
static Map<String, Map<String, String>> expressionLogic = new Map<String, Map<String, String>>();
public class BooleanExpressionHandler {
public static Boolean eval(String expression) {
if (String.isBlank(expression)) {
return false;
}
// Standardize formatting: uppercase and handle spaces around operators/parentheses
String formattedExpr = expression.toUpperCase()
.replace('(', ' ( ')
.replace(')', ' ) ')
.replaceAll('\\s+', ' '); // Normalize spaces
List tokens = formattedExpr.trim().split(' ');
// Stacks for values and operators
List values = new List();
List operators = new List();
for (String token : tokens) {
if (token == 'TRUE') {
values.add(true);
} else if (token == 'FALSE') {
values.add(false);
} else if (token == '(') {
operators.add(token);
} else if (token == ')') {
while (!operators.isEmpty() && operators[operators.size() - 1] != '(') {
values.add(applyOperator(operators.remove(operators.size() - 1), values));
}
if (!operators.isEmpty()) {
operators.remove(operators.size() - 1); // Remove '('
}
} else if (token == 'AND' || token == 'OR') {
while (!operators.isEmpty() && precedence(operators[operators.size() - 1]) >= precedence(token)) {
values.add(applyOperator(operators.remove(operators.size() - 1), values));
}
operators.add(token);
}
}
while (!operators.isEmpty()) {
values.add(applyOperator(operators.remove(operators.size() - 1), values));
}
return values.isEmpty() ? false : values[0];
}
private static Integer precedence(String op) {
if (op == 'AND') return 2;
if (op == 'OR') return 1;
return 0;
}
private static Boolean applyOperator(String op, List values) {
if (values.size() < 2) return false;
Boolean b = values.remove(values.size() - 1);
Boolean a = values.remove(values.size() - 1);
if (op == 'AND') return a && b;
if (op == 'OR') return a || b;
return false;
}
}
4. Bypass Triggers, Validation Rules, and Process Builders
First, create a Custom Setting of Hierarchy type (e.g., BypassSetting__c) and add a checkbox field called Bypass_Trigger__c. Implement the following check inside your Triggers:
BypassSetting__c triggerSetting = BypassSetting__c.getInstance();
if (triggerSetting != null && !triggerSetting.Bypass_Trigger__c) {
// Your Trigger Logic Here
}
5. Split a List into Blocks of a Particular Size
A handy method to break down a massive list of records into smaller, manageable sub-lists.
public static List<List<Account>> splitList(List<Account> recordList, Integer splitSize) {
List<List<Account>> resultList = new List<List<Account>>();
List<Account> tempList = new List<Account>();
Integer index = 0, count = 0, size = recordList.size();
while (index < size) {
tempList.add(recordList.get(index++));
++count;
if (count == splitSize) {
resultList.add(tempList);
tempList = new List<Account>();
count = 0;
}
}
if (!tempList.isEmpty()) {
resultList.add(tempList);
}
return resultList;
}
6. Fetch a Field Value Dynamically from a Related Parent Object
When querying fields dynamically or using generic SObjects, use getSobject() chained together to safely navigate relationships.
sObject acc = [SELECT Owner.Profile.Name FROM Account LIMIT 1];
String profileName = (String) acc.getSobject('Owner').getSobject('Profile').get('Name');
System.debug(profileName);
Excellent read, Positive site, I have read a few of the articles on your website now, and I really like your style.
ReplyDeleteThanks a million and please keep up the effective work and also get to know about best Salesforce Development Company
I am thankful to this blog for giving unique and helpful knowledge about this topic, I read your blog and now share great information here. You can also check best Salesforce Development Company in USA and India.
ReplyDelete