Girl with shades

Mastering Apex Logic: A Step-by-Step Guide

Building robust and efficient Apex logic is a cornerstone of Salesforce development. It’s where the rubber meets the road, transforming business requirements into tangible code. To help you sharpen your logical thinking and problem-solving skills, we’ve curated a series of practical scenarios. This is part three in the Logical Scenarios in APEX series. These challenges in the series are designed to simulate real-world situations, allowing you to apply your Apex knowledge in a hands-on manner. Whether you’re a budding developer or a seasoned pro looking to enhance your toolkit, these exercises will push your abilities and expand your understanding of Apex capabilities. Let’s dive in!

#1 ~ Create an Apex method that takes a list of integers and returns the integer that occurs the most frequently in the list.

In Salesforce development, working with collections and performing data analysis is a common task. One frequent requirement is to determine the most frequently occurring element in a collection of data. For instance, given a list of integers, you might need to identify the integer that appears most frequently. This can be useful for various analytical purposes, such as identifying the most common transaction amount or the most popular product ID. In this post, we will create an Apex method that takes a list of integers and returns the integer that occurs most frequently in the list.

public class LogicalApexClass {
    
    public static Integer mostFrequentIntegerMethod(List<Integer> numList){
        Integer mostFrequentNumber;
        Map<Integer, Integer> intMap = New Map<Integer, Integer>();
        for(Integer num : numList){
            if(intMap.containsKey(num)){
                Integer count = intMap.get(num);
                count = count + 1;
                intMap.put(num, count);
            } else {
                intMap.put(num, 1);
            }
        }     
     
        Integer maxFrequency = 0;
        
        for(Integer i : intMap.keySet()){
            Integer frequency = intMap.get(i);
            if(frequency>maxFrequency){
                maxFrequency = frequency;
                mostFrequentNumber = i;
            }
        }
        system.debug('this is mostFrequentNumber==='+mostFrequentNumber);
        return mostFrequentNumber;
    }
}
Apex

#2 ~ Write an Apex method that takes a map of strings to integers and returns a list of strings whose corresponding integer values are even.

In Salesforce development, manipulating and transforming data structures like maps and lists is a common task. Often, we need to filter data based on specific criteria to meet business requirements or to streamline data processing. For example, you might have a map where the keys are strings and the values are integers, and you need to extract only those keys whose corresponding values are even numbers. This can be useful for various purposes, such as generating reports or validating data. In this post, we will create an Apex method that performs this filtering efficiently and returns a list of strings with even integer values.

public class LogicalApexClass {
    
    public static List<String> evenIntStringValueMethod(Map<String, Integer> strIntMap){
        List<String> evenString = New List<String>();
        for(String s : strIntMap.keySet()){
            integer strNum = strIntMap.get(s);
            integer num = strNum/2;
            integer num1 = num*2;
            if(strNum == num1){
               evenString.add(s); 
            }
        }
        system.debug('this is evenString==='+evenString);
        return evenString;
    }
}
Apex

#3 ~ Create an Apex method that converts a list of account names into a map where the key is the account name and the value is the length of the account name.

In Salesforce development, working with collections like lists and maps is a common task. Sometimes, we need to transform data from one type of collection to another to make it easier to work with or to fit specific business requirements. One such transformation involves converting a list of account names into a map where each account name serves as a key and the value is the count of number of character the account name has. This can be useful for various purposes, such as quickly retrieving the length of an account name for validation or formatting purposes. In this post, we will create an Apex method to achieve this transformation efficiently.

 public class LogicalApexClass {
    
    public static Map<String, Integer> listToMapOfAccounts(List<Account> accList){
       Map<String, Integer> accMap = New Map<String, Integer>();
        for(Account acc : accList){
            String name = acc.Name;
            Integer nameLength = name.length();
            accMap.put(name, nameLength);
        }
        
        system.debug('this is accMap==='+accMap);
        return accMap;
    }
}
Apex

# ~ For your reference

String class in APEX

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm