Salesforce Best Features available

 Salesforce Code Samples & Best Features available:

1. Get merged body without extra effort.

EmailTemplate emailTemplate = [Select Id FROM EmailTemplate where name = 'Test1'];  
Messaging.SingleEmailMessage email = Messaging.renderStoredEmailTemplate(emailTemplate.Id,
userinfo.getUserId(),AccountObj.Id);
system.debug(email.plainTextBody);

2. Business Hour Calculations example in apex:

public static Boolean checkBusinessHour(){
        //business hour logic
        List<BusinessHours> defaultBusinessHours = [SELECT Id FROM BusinessHours WHERE Name='Default' 
AND IsDefaultTRUE 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:

public class BooleanExpressionHandler {
    static Map<StringString> logicTypes = new Map<StringString>();
    static Map<StringMap<StringString>> expressionLogic = new Map<StringMap<StringString>>();
    
    public static Boolean eval(String expression) {        
        // If expression contains all TRUE or FALSE
        if(expression.containsNone('FALSE')) { return TRUE; }        
        if(expression.containsNone('TRUE')) { return FALSE; }
        
        fillLogic();
        
        return Boolean.valueOf(evaluateExpression(expression.toUpperCase()));
    }
    
    public static String evaluateExpression(String expression) {        
        for(String logicType : logicTypes.keySet()) {
            if(expression.contains(logicType)) {
                expression = simplifyExpression(expression, logicTypes.get(logicType));
            }
        }
        
        if(expression.contains('AND') || expression.contains('OR') || expression.contains('(')) {
            expression = evaluateExpression(expression);
        }
        
        return expression;
    }
    
    public static string simplifyExpression(String expression, String LogicType){
        Map<StringString> Logic = new Map<StringString>(expressionLogic.get(LogicType));
        
        for(String key : Logic.keySet()) {
            expression = expression.replace(key, Logic.get(key));
        }
        
        return expression;
    } 
    
    public static void fillLogic() {
        Map<StringString> ANDLogic = new Map<StringString>();
        Map<StringString> ORLogic = new Map<StringString>();
        Map<StringString> BRACELogic = new Map<StringString>();
        
        logicTypes.put('AND''AND');
        logicTypes.put('OR''OR');
        logicTypes.put('(''BRACES');
        
        // AND Logic
        ANDLogic.put('TRUE AND TRUE''TRUE');
        ANDLogic.put('TRUE AND FALSE''FALSE');
        ANDLogic.put('FALSE AND TRUE''FALSE');
        ANDLogic.put('FALSE AND FALSE''FALSE');
        expressionLogic.put('AND', ANDLogic);
        
        // OR Logic
        ORLogic.put('TRUE OR TRUE''TRUE');
        ORLogic.put('TRUE OR FALSE''TRUE');
        ORLogic.put('FALSE OR TRUE''TRUE');
        ORLogic.put('FALSE OR FALSE''FALSE');
        expressionLogic.put('OR', ORLogic);
        
        // Braces Logic
        BRACELogic.put('(TRUE)''TRUE');
        BRACELogic.put('(FALSE)''FALSE');
        expressionLogic.put('BRACES', BRACELogic);
    }
}

4. Bypass Trigger/Validation Rules/Process Builder

Firstly we need to create the custom setting of hierarchy type.
Create record into custom setting as shown in below screenshot.

Use below code into Triggers:

BypassSetting__c triggerSetting = BypassSetting__c.getInstance();
    if(triggerSetting !=null && !triggerSetting.Bypass_Trigger__c){
        // Trigger Logic
    }
5. Code to split list into particular size.

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 from a related parent object

sObject acc = [SELECT Owner.Profile.Name FROM Account LIMIT 1]; String profileName = (String) acc.getSobject('Owner').getSobject('Profile').get('Name'); System.debug(profileName);


7. 

Comments

  1. Excellent read, Positive site, I have read a few of the articles on your website now, and I really like your style.
    Thanks a million and please keep up the effective work and also get to know about best Salesforce Development Company

    ReplyDelete

Post a Comment

Popular posts from this blog

Communicating between Independent LWC in Omniscript

Efficient way to write apex code