Few Apex Scenario Based Programming Questions
Recently at one of the local Hackathon I came across some scenario based Apex programming questions and I thought I would go ahead and blog on them. Below are three scenarios on which we are going to write some Apex code!
Scenario 1: Create a Method which accepts List of String and returns a Map with count on how many times a specific value is repeated in a given List. Example:- List<'A', 'B', 'A', 'C', 'B', 'A'> should result in "{A=3, B=2, C=1}"
Approached Solution: Below is my Apex class with a method to accept List and returns a Map.
/* * Purpose: Hackathon Programming Scenarios * * Developer: HackathonDev */ public class HackathoScenarios { // Method to return a map with string occurence count that was passed in a list public static Map<String, Integer> scenario1(List<String> lstStrng) { Map<String, Integer> mapVals = new Map<String, Integer>(); for (String s: lstStrng) { if (mapVals.containsKey(s)) { mapVals.put(s, mapVals.get(s)+1); } else { mapVals.put(s, 1); } } return mapVals; } }
When I use below sample code block to execute from my execute anonymous Dev Console window, I see below results.
Sample Code:
List<String> lstS = new List<String>{'A', 'B', 'A', 'C', 'B', 'A', 'Test', 'Testing', 'A', 'Test'}; Map<String, Integer> mapRslts = HackathoScenarios.scenario1(lstS); System.debug('************' + mapRslts);
Debug Log Result:
Scenario 2: Create a Method which accepts List of records to update and return List of all record id's that failed to update.
Approached Solution: Below is my Apex class with a method to accept List of sObjects and returns a List of String with failed record Id's. Here I tried to use "SaveResults" class to get update results.
/* * Purpose: Hackathon Programming Scenarios * * Developer: HackathonDev */ public class HackathoScenarios { // Method to return list of Id's that had exception while updating records public static List<String> scenario2 (List<sObject> lstRecords) { List<String> lstRcdIds = new List<String>(); Map<String, String> mapRcdIds = new Map<String, String>(); try { Database.SaveResult[] srList = Database.update(lstRecords, false); for (Database.SaveResult sr : srList) { if (sr.isSuccess()) { // Add record id to the map mapRcdIds.put(sr.getId(), sr.getId()); System.debug('************Success Record Id: ' + sr.getId()); } else { // Debug failed record id and this will result in 'null' System.debug('************Failed Record Id: ' + sr.getId()); } } } catch (exception e) { lstRcdIds.add('There is an exception while updating records: ' + e.getMessage()); } // Loop through the initial list of records to separate failed record id and add them to a list for (sObject s: lstRecords) { if (!mapRcdIds.containsKey(s.Id)) { lstRcdIds.add(s.Id); } } return lstRcdIds; } }
I created a Validation rule on Account to require Phone number on updates and When I use below sample code block to execute from my execute anonymous Dev Console window, I see below results with failed record Id's List.
Sample Code:
List<Account> lstAccnts = [Select Id, Name From Account Limit 22]; for (Account acc: lstAccnts) { acc.Name = acc.Name + 'Testing'; } List<String> lstFailedIds = HackathoScenarios.scenario2(lstAccnts); System.debug('************' + lstFailedIds);
Debug Log Result:
Scenario 3: Create a Method to Encrypt/Decrypt using Crypto class.
Approached Solution: Below is my Apex class with a method to accept String value and Encrypt/Decrypt using Crypto class available method.
/* * Purpose: Hackathon Programming Scenarios * * Developer: HackathonDev */ public class HackathoScenarios { // Method to Encrypt/Decrypt using Crypto class methods public static void scenario3(String urlParam) { // Generate crypto key Blob cryptoKey = Crypto.generateAesKey(256); // Generate initialization vector - Make sure this is always 16 bytes! Blob initialVec = Blob.valueOf('abcdefghijklmnop'); // Convert string to blob Blob data = Blob.valueOf(urlParam); // Encrypt using Crypto Encrypt method Blob encryptedData = Crypto.encrypt('AES256', cryptoKey, initialVec, data); System.debug('************Encrypted Code: ' + encryptedData); // Decrypt using Crypto Decrypt method Blob decryptedData = Crypto.decrypt('AES256', cryptoKey, initialVec, encryptedData); System.debug('************Decrypted Code: ' + decryptedData); // Obtain Decrypt value as String String decryptedDataString = decryptedData.toString(); System.debug('************Decrypted Code: ' + decryptedDataString); } }
Use below sample code block to execute from my execute anonymous Dev Console window
Sample Code:
String s = 'https://reddydev2018-dev-ed.my.salesforce.com/_ui/common/apex/debug/ApexExecAnon'; HackathoScenarios.scenario3(s);
Debug Log Result:
Comments
Post a Comment