Most Common Salesforce Apex Operations

by Rijwan Mohmmed
0 comment
most-common-salesforce-apex-operations-techdicer

Hello friends, today we are going to discuss Most Common Salesforce Apex Operations. We all know there are many small-2 pieces of code in the apex that we need every time when we do code.

Also check this: Import third-party libraries in LWC

Key Highlights :

  1. Here we will put apex operations.
  2. These operations will help do code in apex easily.

Apex Operations:

1. Set Key Values in Map in For Loop

public class ApexCommanMapMethod {
    public static void setKeyValueInMap(){
        //store the same email with amount
        Map<String, Decimal> mapOfCon = new Map<String, Decimal>();
        for(Contact con : [SELECT Id, Email, Amount__c FROM Contact LIMIT 100]){
            if(!mapOfCon.containsKey(con.Email)){
                mapOfCon.put(con.Email, 0.00);
            }
            Decimal amount = mapOfCon.get(con.Email);
            amount = amount + con.Amount__c;
            mapOfCon.put(con.Email, amount);
        }
        System.debug('map debug==> '+ mapOfCon);
    }
}

2. Convert comma-separated String to a List

String comaSeperateString = 'x, y, z';
List<String> listOfString = comaSeperateString.split(',');
System.debug(listOfString);

3. SOQL Query and get a MAP returned

Map<ID, Account> m = new Map<ID, Account>([SELECT Id, Name, Type FROM Account]);

4. Get Set of Id from Sobject List

List<Account> accountList = [SELECT Id, Name, Type FROM Account];
Set<Id> acountIds = (new Map<Id,SObject>(accountList)).keySet();

5. Get Map Keys and Values in Apex

Map<String, Integer> monthsMap = new Map<String, Integer>{'January'  => 1,'February' => 2, 'March' => 3};
System.debug('mapKeys==>' + monthsMap.keys());
System.debug('mapValues==>' + monthsMap.values());

6. Generate a random string in Apex

public static String generateRandomString(Integer length) {
    String chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
    String randomString = '';
    while (randomString.length() < len) {
       Integer idx = Math.mod(Math.abs(Crypto.getRandomInteger()), chars.length());
       randomString += chars.substring(idx, idx+1);
    }
    return randomString ; 
}

7. Get Day, Month and Year From Date Field Using Apex

Date dt = System.today(); //current date
Integer day = dt.Day(); //get day
Integer month = dt.Month(); //get month
Integer year = dt.Year(); //get year

8. Convert String to Date in Apex

Date parseDate = date.parse('04/08/2022');
System.debug('parseDate ==> ' + parseDate);

9. Convert String to DateTime in Apex

System.debug(DateTime.parse('11/08/2022, 10:21 AM'));

10. Check User has a Custom Permission in Salesforce Using Apex

Boolean hasCustomPermission = FeatureManagement.checkPermission('CUSTOM_PERMISSION_API');

11. Convert DateTime to Date in Apex

DateTime dt = System.now()
Date d = dt.date();
System.debug('Date==>' + d);

12. Set and Get Sobject fields dynamically

for(Contact con:conList)  {
    String fieldValue = (String) con.get(selectedField);
    con.put(selectedField, 'test');
}

13. Get Week Number From Date in Apex

// Cast the Date variable into a DateTime
DateTime dtTime = (DateTime) yourDate;
String dayOfWeek = myDateTime.format('E');
System.debug('dayOfWeek==>' + dayOfWeek);
// dayOfWeek is Sun, Mon, Tue, etc.

14. Check If a text field is Empty in Apex

if(String.isBlank(Field)){
  System.debug(Field + ' is Empty');
}

15. Round a Decimal Number to two decimal places

Decimal num1 = 1.254754;
Decimal roundNum = num1.setScale(2);
system.debug(roundNum);

16. How to

Reference :

  1. Salesforce Apex

What’s your Reaction?
+1
1
+1
0
+1
0
+1
0
+1
0
+1
0

You may also like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.