Apex Caching Performance Impact

Apex Caching is great for saving some basic Id’s of a user like his Account, Contact Id’s after he logs in so you don’t have to query for them. Reduces the amount of queries and improves speed. Caching is good for saving large blobs that don’t change frequently.

Generic Cache Service to put, retrieve and remove cached item

public with sharing class App_Service_Cache {

		private Boolean cacheEnabled;

		public App_Service_Cache() {
			  cacheEnabled = true;
		}

    public Boolean toggleEnabled() { // Use for testing misses
        cacheEnabled = !cacheEnabled;
        return cacheEnabled;
    }

    public Object get(String key) {
        if (!cacheEnabled) return null;
        Object value = Cache.Session.get(key);
        if (value != null) System.debug(LoggingLevel.DEBUG, 'Hit for key ' + key);
        return value;
    }

    public void put(String key, Object value, Integer ttl) {
        if (!cacheEnabled) return;
        Cache.Session.put(key, value, ttl);
        // for redundancy, save to DB
        System.debug(LoggingLevel.DEBUG, 'put() for key ' + key);
    }

    public Boolean remove(String key) {
        if (!cacheEnabled) return false;
        Boolean removed = Cache.Session.remove(key);
        if (removed) {
            System.debug(LoggingLevel.DEBUG, 'Removed key ' + key);
            return true;
        } else return false;
    }
}

Test cache get is faster than query

@isTest
private class App_Service_Cache_Test {

	@isTest
	static void testCachePerformance() {
		App_Service_Cache appCache = new App_Service_Cache();
		Map<String, String> communityUser = App_Global_Test.setupCommunityUserReturnIds();
		Id userId = communityUser.get('UserId');
		long startTime = System.currentTimeMillis();
		List<Contact> currentContact = [select Id, AccountId from Contact where Id IN (Select ContactId from User where Id=:userId)];
		long elapsedTime = System.currentTimeMillis() - startTime;
		appCache.put('userId', currentContact, 2000);
		System.debug(elapsedTime);

		long startCacheTime = System.currentTimeMillis();
		appCache.get('userId');
		long elapsedCacheTime = System.currentTimeMillis() - startCacheTime;
		System.assert(elapsedCacheTime < elapsedTime);
	}
}

Create a Salesforce EventBus

Create a new Planform Event Object and give it a name App Metadata (App_Metadata__e) which will also be the name of the topic. Create some fields you that you would like to listen to. In Apex add the following event

Build event payload

  List<App_Metadata.BPUpdateAccounts> updatedAccountList = new List<App_Metadata.BPUpdateAccounts>();
  App_Metadata.BPUpdateAccounts updatedAccount = new App_Metadata.BPUpdateAccounts();
  updatedAccount.setId(financialAccount.Id);
  updatedAccount.add(updatedAccount);
  new App_Rest_Events(new App_Rest_Events.Event(UserInfo.getUserId(), App_Rest_Events.EventType.UPDATE_ACCOUNT, 
  updatedAccountList)).publishToEventBus();

Publish Event to eventBus

  public void publishToEventBus(){
    Object eventPayload = buildEventPayload();
    EventBus.publish(new App_Metadata__e(Metadata_Json__c=JSON.serialize(eventPayload)));
  }

Now let’s create our event listen to subscript to the topic and consume the messages from the EventBus.

Add the emp-connector jar to project

<repositories>
        <repository>
            <id>emp-connector</id>
            <url>${project.basedir}/lib/emp-connector-0.0.1-SNAPSHOT-phat.jar</url>
        </repository>
    </repositories>

....

<dependency>
     <groupId>com.salesforce.conduit</groupId>
     <artifactId>emp-connector</artifactId>
     <version>0.0.1-SNAPSHOT</version>
</dependency>

Create TopicSubscription to your event table by name

package com.app.salesforce.eventbus;

import com.salesforce.emp.connector.BayeuxParameters;
import com.salesforce.emp.connector.EmpConnector;
import com.salesforce.emp.connector.TopicSubscription;

import java.net.URL;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

import static com.salesforce.emp.connector.LoginHelper.login;

/**
 * Created by thysmichels on 5/15/17.
 */
public class SalesforceEventBus {

    private static final String SALESFORCE_USERNAME="salesforceUsername";
    private static final String SALESFORCE_PASSWORD="salesforcePassword";

    public static void main(String[] argv) throws Exception {

        long replayFrom = EmpConnector.REPLAY_FROM_EARLIEST;

        BayeuxParameters params;
        BayeuxParameters custom = getBayeuxParamWithSpecifiedAPIVersion("39.0");
        try {
            params = login(new URL("https://test.salesforce.com"),SALESFORCE_USERNAME, SALESFORCE_PASSWORD, custom);

        } catch (Exception e) {
            e.printStackTrace(System.err);
            System.exit(1);
            throw e;
        }

        Consumer<Map<String, Object>> consumer = event -> System.out.println(String.format("Received:\n%s", event));
        EmpConnector connector = new EmpConnector(params);

        connector.start().get(5, TimeUnit.SECONDS);

        TopicSubscription subscription = connector.subscribe("/event/App_Metadata__e", replayFrom, consumer).get(5, TimeUnit.SECONDS);

        System.out.println(String.format("Subscribed: %s", subscription));
    }


    private static BayeuxParameters getBayeuxParamWithSpecifiedAPIVersion(String apiVersion) {
        BayeuxParameters params = new BayeuxParameters() {

            @Override
            public String version() {
                return apiVersion;
            }

            @Override
            public String bearerToken() {
                return null;
            }

        };
        return  params;
    }
}

Topic receives response from subscribed channel

Subscribed: Subscription [/event/App_Metadata__e:-2]
Received:
{schema=D6-eSgLDrahnNjuNI52XAg, payload={CreatedById=00521000000UcF0, CreatedDate=2017-05-15T18:35:10Z, 
Metadata_Json__c="{\"accounts\":[{\"id\":\"28706236\",\"providerAccountId\":14736,\"sfId\":\"a0721000001A6fOAAS\"},
{\"id\":\"YL1111\",\"providerAccountId\":1466,\"sfId\":\"a0721000001A6fPAAS\"}],\"event\":\"UPDATE_GOAL\",\"linkAccounts\":null,
\"financialAccountMap\":null,\"goals\":[{\"id\":\"a0821000000OVPUAA4\",\"linkedAccounts\":[\"a0721000001ADqkAAG\"],
\"status\":\"In Progress\"},{\"id\":\"a0821000000OVPZAA4\",\"linkedAccounts\":null,\"status\":\"In Progress\"},{\"id\":\"a0821000000OVPeAAO\",\"linkedAccounts\":[\"a0721000001ADqaAAG\"],\"status\":\"In Progress\"}],
\"linkUserContext\":{\"accountList\":null,\"additionalProperties\":null,\"deleteAccountList\":null,\"email\":\"blah@email.com\",
\"password\":\"p@assw0rd\",
\"providerAccountIds\":null,\"sfClientId\":\"0033600000NO1QjAAL\",\"sfHouseholdId\":\"0013600000VNXflAAH\",\"sfUserId\":\"00521000000UcF0AAK\",
\"type\":\"YL\",\"username\":\"YL00F0A\"},\"priority\":5,\"providerAccounts\":null,
\"updatedAccounts\":null,\"updatedGoals\":[{\"id\":\"a0821000000OVPZAA4\",\"linkedAccounts\":null}],\"mapKeys\":{\"event\":\"event\",\"priority\":\"priority\",\"linkUserContext\":\"linkUserContext\",\"goals\":\"goals\",
\"accounts\":\"accounts\",\"providerAccounts\":\"providerAccounts\",\"updatedAccounts\":\"updatedAccounts\",\"updatedGoals\":
\"updatedGoals\",\"goalFinancialAccountMap\":\"goalFinancialAccountMap\",\"fastlinkAccounts\":\"fastlinkAccounts\"},
\"mapKeysFlag\":true,\"serializeNulls\":false}"}, event={replayId=1}}

Apex One Endpoint to Rule them all

Most systems need to do CRUD on some objects and also query some information. Why not have one endpoint to do it all?I have created a endpoint that can do all that using mapping between systems field and Salesforce fields. System field does not contain any underscores or Salesforce specific syntax like (__c). With that a system can send:

{“name”:”John Doe”, “taxRatePerClient”:20}

And it will be translated to:

{“Name”:”John Doe”, “Tax_Rate_Per_Client__c”:20}

Do CRUD and execute any query

global with sharing class App_Rest_Data {

	global with sharing class Post implements App_Rest_Dispatcher.Dispatchable {
		private App_Rest_Global_Request_Json requestBody;

		global String getURIMapping(){
						return App_Rest_Constants.URI_DATA +'/{object}/{operation}';
		}

		global void setRequestBody(App_Rest_Global_Request_Json requestBody){
				this.requestBody = requestBody;
		}

		global App_Rest_Global_Json execute(Map<String, String> parameters){
			App_Rest_Global_Json globalJson = new App_Rest_Global_Json();
			String objectName = parameters.get('object');
			String operation = parameters.get('operation');

			if ('q'.equals(operation)){
				globalJson.setResult(Database.query(String.valueOf(((Map<String, Object>)JSON.deserializeUntyped(requestBody.getRequest())).get('query'))));
			} else {
	            Map<String,String> uiFieldNamesForObject = App_Rest_Settings.getModelByObject(objectName);
	            App_JSON.DynamicModel dynamicModel = new App_JSON.DynamicModel(uiFieldNamesForObject);
	            dynamicModel.setMapKeysFlag(true);
	            Type classType = Type.forName(objectName);
	            SObject sobjectModel = (SObject) classType.newInstance();
	            sobjectModel =  (SObject)dynamicModel.deserialize(requestBody.getRequest(), classType);

	            fflib_ISObjectUnitOfWork uow = App_Rest_App.UnitOfWork.newInstance();

	            if ('c'.equals(operation)){
	              uow.registerNew(sobjectModel);
	            } else if ('u'.equals(operation)){
	              uow.registerDirty(sobjectModel);
	            } else if('d'.equals(operation)){
	              uow.registerDeleted(sobjectModel);
	            }
	            uow.commitWork();

	            Schema.SObjectType sobjectModelType = Schema.getGlobalDescribe().get(objectName);
	            fflib_QueryFactory queryFactory = new fflib_QueryFactory(sobjectModelType);
	            queryFactory.selectFields(uiFieldNamesForObject.keySet());

	            globalJson.setResult((List<Object>) Database.query(queryFactory.toSOQL()));
  			}

			return globalJson;
		}

		global override String toString(){
			App_Rest_Endpoint.Path path = new App_Rest_Endpoint.Path();
			path.setPath(getURIMapping());
			App_Rest_Endpoint endpoint = new App_Rest_Endpoint(path);
			return JSON.serialize(endpoint);
		}
	}
}

Testing out CRUD and query operations

@isTest
private class App_Rest_Data_Test {

	@isTest(SeeAllData=true) static void testPost() {
		Test.setMock(HttpCalloutMock.class, new App_LogEntries_Api_Mock());
		RestRequest req = new RestRequest();
		RestResponse res = new RestResponse();
		User communityUser = App_Global_Test.setupCommunityUserAndLogin();
		Test.startTest();
			req.requestBody =  Blob.valueOf('{"firstName":"Test FirstName","lastName":"Test LastName", "email":"test@mail.com"}');
			req.requestURI = '/v1/data/contact/c';
			req.httpMethod = 'POST';
			RestContext.request = req;
			RestContext.response = res;
			App_Rest_Dispatcher.doPost();
		Test.stopTest();

		System.assert(res.responseBody!=null);
		System.assertEquals(res.statusCode, 200);
	}

	@isTest(SeeAllData=true) static void testPatch() {
		Test.setMock(HttpCalloutMock.class, new App_LogEntries_Api_Mock());
		RestRequest req = new RestRequest();
		RestResponse res = new RestResponse();
		User communityUser = App_Global_Test.setupCommunityUserAndLogin();
		Test.startTest();
			Account setupAccount = App_Global_Test.setupAccount();
			req.requestBody =  Blob.valueOf('{"id":"'+setupAccount.Id+'","name":"Test Update"}');
			req.requestURI = '/v1/data/account/u';
			req.httpMethod = 'POST';
			RestContext.request = req;
			RestContext.response = res;
			App_Rest_Dispatcher.doPost();
		Test.stopTest();

		System.assert(res.responseBody!=null);
		System.assertEquals(res.statusCode, 200);
	}

	@isTest(SeeAllData=true) static void testDelete() {
		Test.setMock(HttpCalloutMock.class, new App_LogEntries_Api_Mock());
		RestRequest req = new RestRequest();
		RestResponse res = new RestResponse();
		User communityUser = App_Global_Test.setupCommunityUserAndLogin();
		Test.startTest();
			Account setupAccount = App_Global_Test.setupAccount();
			req.requestBody =  Blob.valueOf('{"id":"'+setupAccount.Id+'","name":"Test Update"}');
			req.requestURI = '/v1/data/account/d';
			req.httpMethod = 'POST';
			RestContext.request = req;
			RestContext.response = res;
			App_Rest_Dispatcher.doPost();
		Test.stopTest();

		System.assert(res.responseBody!=null);
		System.assertEquals(res.statusCode, 200);
	}

	@isTest(SeeAllData=true) static void testQuery() {
		Test.setMock(HttpCalloutMock.class, new App_LogEntries_Api_Mock());
		RestRequest req = new RestRequest();
		RestResponse res = new RestResponse();
		User communityUser = App_Global_Test.setupCommunityUserAndLogin();
		Test.startTest();
			Account setupAccount = App_Global_Test.setupAccount();
			req.requestBody =  Blob.valueOf('{"query":"Select id from User"}');
			req.requestURI = '/v1/data/account/q';
			req.httpMethod = 'POST';
			RestContext.request = req;
			RestContext.response = res;
			App_Rest_Dispatcher.doPost();
		Test.stopTest();

		System.assert(res.responseBody!=null);
		System.assertEquals(res.statusCode, 200);
	}
}

Apex Convert String To Camel Case

Convert string to camelCase for one underscore

   public static String toCamelCase(String input) {
      Pattern p = Pattern.compile('_([a-zA-Z])');
      Matcher m = p.matcher( input );
      fflib_StringBuilder sb = new fflib_StringBuilder();
      while (m.find()) {
          sb.add(m.replaceAll(m.group(1).toUpperCase()));
      }
      return sb.toString().uncapitalize();
     }

Convert string to camelCase for multiple underscore

 public static String toCamelCase(String input){
      fflib_StringBuilder sb = new fflib_StringBuilder();
      boolean capitalizeNext = false;
      for (String c:input.split('')) {
        if (c == '_') {
          capitalizeNext = true;
        } else {
          if (capitalizeNext) {
            sb.add(c.toUpperCase());
            capitalizeNext = false;
          } else {
            sb.add(c.toLowerCase());
          }
        }
      }
      return sb.toString();
    }

To Camel Case Test Class

@isTest
	static void testToCamelCase(){
		Test.startTest();
			System.assertEquals(BrightPlan_Rest_Util.toCamelCase('loan_id'), 'loanId');
			System.assertEquals(BrightPlan_Rest_Util.toCamelCase('Camel_case_This_word'), 'camelCaseThisWord');
			System.assertEquals(BrightPlan_Rest_Util.toCamelCase('this_IS_A_TEst_for_CAMEL_Case'), 'thisIsATestForCamelCase');
		Test.stopTest();
	}

Query Multi-Select Picklists in SOQL

Create list of values to check for multi picklist

       Set<String> typeSet = new Set<String>();
       String allStr = 'All';
       typeSet.add('\''+ allStr + '\'');
       for (Financial_Goal__c goal  : Contact.FinancialGoals__r){
         typeSet.add('\''+ goal.Type__c + '\'');
       }

Query using INCLUDES and using String.join to create string of List.

List<Content__c> typeMappings = (List<Content__c>) Database.query(
contentMappingQueryFactory.setCondition('Status__c=\'Publish\'
AND Id!=:featureMappingId AND (Goal_Type_Attribute__c INCLUDES(' + String.join(goalTypeList, ',')+ ') 
AND Age_Attribute__c INCLUDES(' + String.join(ageList, ',')+ ') 
AND Employment_Attribute__c IN :employementList AND Gender_Attribute__c IN :genderList 
AND Net_Worth_Attribute__c IN :netWorthList)').setLimit(2).toSOQL());

Use , for OR and ; for AND to match values in picklist

OR:
will match for any value in picklist
String.join(goalTypeList, ',')

AND:
will match for all the value in picklist
String.join(goalTypeList, ';')

Apex download images from RSS feed as attachments

Update Blog Post Job calling from RSS

  @future(callout=true)
    static global void upsertContentMapping() {
        List<Dom.XMLNode> recentPosts = getRSSFeed('https://www.yoursite.com/resources/rss.xml');
        List<BP_Content_Mapping__c> blogEntries = new List<BP_Content_Mapping__c>();
        List<String> titles = new List<String>();
        List<BP_Content_Mapping__c> entriesToAdd = new List<BP_Content_Mapping__c>();

        String listOfBlogs = '';
        System.Debug('# OF POSTS FOUND:' +recentPosts.size());

        for(Dom.XMLNode post : recentPosts) {
           blogEntries.add(convertFeedToBlogPost(post));
        }

        System.Debug('# OF ENTRIES FOUND:' +blogEntries.size());

        for(BP_Content_Mapping__c bp : blogEntries) {
            titles.add(bp.Content_URL__c);
        }

        List<Attachment> blogAttachments = new List<Attachment>();
        List<BP_Content_Mapping__c> blogs = [SELECT Id, GUID__c from BP_Content_Mapping__c WHERE Content_URL__c IN :titles];
        Integer counter = 0 ;
        for(BP_Content_Mapping__c blogEntry : blogEntries) {
            if (counter < 10){
              Boolean added = false;
              for(BP_Content_Mapping__c blog : blogs) {
                  if(blog.GUID__c == blogEntry.GUID__c) { added = true; }
              }
              if(!added) {
                entriesToAdd.add(blogEntry);
                counter++;
              }
            }
        }

        Map<String, Attachment> attachments =  createAttachments(entriesToAdd);

        System.Debug('# OF ENTRIES TO IMPORT:' + entriesToAdd.size() + ' ' + entriesToAdd);
        insert entriesToAdd;

        for (BP_Content_Mapping__c entry : entriesToAdd){
          Attachment attachment = attachments.get(entry.GUID__c);
          attachment.ParentId = entry.Id;
          attachments.put(entry.GUID__c, attachment);
        }

        System.Debug('# OF ATTACHMENTS TO IMPORT:' + attachments.size() + ' ' + attachments);
        insert attachments.values();
   }

Get RSS feed and return XMLNode

static global List<Dom.XMLNode> getRSSFeed(string URL) {
      Http h = new Http();
      HttpRequest req = new HttpRequest();
      // url that returns the XML in the response body

      req.setEndpoint(url);
      req.setMethod('GET');
      HttpResponse res = h.send(req);
      Dom.Document doc = res.getBodyDocument();

      Dom.XMLNode rss = doc.getRootElement();
      System.debug('@@' + rss.getName());

      List<Dom.XMLNode> rssList = new List<Dom.XMLNode>();
      for(Dom.XMLNode child : rss.getChildren()) {
         System.debug('@@@' + child.getName());
         for(Dom.XMLNode channel : child.getChildren()) {
             System.debug('@@@@' + channel.getName());

             if(channel.getName() == 'item') {
                  rssList.add(channel);
             }
         }
      }
      return rssList;
  }

Convert XMLNode to Blog Posts

static global BP_Content_Mapping__c convertFeedToBlogPost(Dom.XMLNode post) {
      BP_Content_Mapping__c bp = new BP_Content_Mapping__c();
      Integer tagIndex = 0;

      for(Dom.XMLNode child : post.getChildren()) {
          if(child.getName() == 'title') { bp.Title__c = child.getText(); }
                    if(child.getName() == 'guid') { bp.GUID__c = child.getText(); }
          if(child.getName() == 'pubDate') { bp.Effective_Date__c = convertRSSDateStringToDate(child.getText()); }
          if(child.getName() == 'link') { bp.Content_URL__c = child.getText(); }
          if(child.getName() == 'description') {
            String[] descriptionPipeSlit = child.getText().split('\\|',-1);
            if (descriptionPipeSlit.size() > 0){
              bp.Summary__c = descriptionPipeSlit[0];
              bp.Author__c = descriptionPipeSlit[1].remove('Author:');
              bp.Blog_Category__c = descriptionPipeSlit[2].remove('Category:');
              bp.Category_Color__c = descriptionPipeSlit[3].remove('Color:');
            }
          }
          if ('content'.equals(child.getName())){
              bp.Image_URL__c = child.getAttributeValue('url', null);
          }
      }
      return bp;
  }

Download blog images to attachment

static Map<String, Attachment> createAttachments(List<BP_Content_Mapping__c> contentMappings){
    Map<String, Attachment> attachments = new Map<String, Attachment>();
    for (BP_Content_Mapping__c contentMapping : contentMappings){
      Attachment attachment = new Attachment();
      attachment.Name = contentMapping.Title__c + '.jpg';
      attachment.ContentType= 'image/jpg';
      attachment.Body = downloadImagesToBlob(contentMapping.Image_URL__c);
      attachments.put(contentMapping.GUID__c, attachment);
    }
    return attachments;
  }

  static Blob downloadImagesToBlob(String imageUrl){
      Http h = new Http();
      HttpRequest req = new HttpRequest();
      req.setEndpoint(imageUrl);
      req.setMethod('GET');
      req.setHeader('Content-Type', 'image/jpg');
      req.setCompressed(true);
      req.setTimeout(60000);
      HttpResponse res = null;
      res = h.send(req);
      Blob image = res.getBodyAsBlob();
      return image;
  }

Catch Apex Callout Loop not Allowed

Getting the following error: Apex Callout Loop not Allowed when you are doing a callout to the same Salesforce org and then calling out to another external service.

In this case I was doing a callout to the same salesforce org and then doing a callout to the my logging service.

RestRequest request = RestContext.request;
Map requestHeaders = request.headers;
if (requestHeaders.containsKey('Sfdc-Stack-Depth') && '1'.equals(requestHeaders.get('Sfdc-Stack-Depth'))){
      System.debug('Do not calllout as it will cause a callout loop error');
} 

Further you can write these values to a custom object and have a scheduler or workflow run to do the callout.

Apex workaround for doing callout after DML

If you are trying to do a callout after a DML operation you would have seen this error message:

You have uncommitted work pending. Please commit or rollback before calling out

A way around doing callouts after a DML is to do the DML as its own internal callout. This is possible by creating a internal endpoint which will be called first to insert the records. After the insert is complete serialize the response and use that for the external callout.

Steps to following:
1. Create internal endpoint to insert records
2. Create internal Httprequest to the endpoint above
3. Combine internal callout and external callout

Create internal endpoint to insert records

	global with sharing class PostInternal implements Rest_Dispatcher.Dispatchable {
			private Rest_Global_Request_Json requestBody;

			global String getURIMapping(){
							return Rest_Constants.URI_ACCOUNT + '/internal';
			}

			global void setRequestBody(Rest_Global_Request_Json requestBody){
					this.requestBody = requestBody;
			}

			global Rest_Global_Json execute(Map<String, String> parameters){
					Rest_Global_Json globalJson = new Rest_Global_Json();
					List<Financial_Account__c> financialAccountList = (List<Financial_Account__c>)JSON.deserialize(requestBody.getRequest(), List<Financial_Account__c>.class);
					List<Financial_Account__c> bpFinancialAccounts = App_Service.instance.insertFinancialAccount(financialAccountList);
					globalJson.setResult(Selector_Financial_Account.newInstance().selectByFinAccountAccounts(bpFinancialAccounts).values());
					return globalJson;
			}
}

Create internal Httprequest to the internal endpoint

public static HttpResponse doInternalCalloutToInsertAccount(List<Financial_Account__c> financialAccountList){
		Http h = new Http();
		Httprequest req = new Httprequest();
		req.setMethod('POST');

		req.setHeader('Authorization','Bearer ' + UserInfo.getSessionId());
		req.setHeader('Content-Type','application/json');
		Object finaAccountListObject = (Object)financialAccountList;
		req.setBody(JSON.serialize(finaAccountListObject));
		req.setEndpoint(System.URL.getSalesforceBaseURL().toExternalForm() + '/services/apexrest/v1/account/internal');
		HttpResponse response = h.send(req);
		return response;
}

Combine internal callout and external callout

HttpResponse response = doInternalCalloutToInsertAccount(financialAccountList);

	if (response!=null && response.getBody()!=null){
		Map<String,Object> serializeResponse = (Map<String,Object>)JSON.deserializeUntyped(response.getBody());
		bpAccountInserted = (List<Financial_Account__c>)JSON.deserialize(JSON.serialize(serializeResponse.get('result')), List<Financial_Account__c>.class);
		List<Metadata.BPUpdatedAccounts> bpAccountUpdateList = new List<Metadata.BPUpdatedAccounts>();

		for (Financial_Account__c bpAccount : bpAccountInserted){
			...
		}

		if (!bpAccountUpdateList.isEmpty())
			new Rest_Events(new Rest_Events.Event(UserInfo.getUserId(), Rest_Events.EventType.UPDATE_ACCOUNTS, bpAccountUpdateList, new List<String>{'Active', 'InActive'})).handleEvent();
}

Apex Sorting Objects with Comparable Interface

Sorting objects generic class

public abstract class Comparator {
    public abstract Integer compare(Object o1, Object o2);
    public static void sort(Object[] values, Comparator comp) {
        //  Obtain the list type of values
        Object[] temp = values.clone();
        temp.clear();
        //  Helper class for sorting using Comparable
        Helper[] tempValues = new Helper[0];
        for(Object value: values) {
            tempValues.add(new Helper(comp, value));
        }
        //  Perform sort
        tempValues.sort();
        //  Extract values back into temp list
        for(Helper helper: tempValues) {
            temp.add(helper.value);
        }
        //  And set the list to the new, sorted order
        values.clear();
        values.addAll(temp);
    }
    //  Simply calls Comparator when asked.
    class Helper implements Comparable {
        Comparator method;
        Object value;
        Helper(Comparator comp, Object val) {
            method = comp;
            value = val;
        }
        public Integer compareTo(Object o) {
            return method.compare(value, ((Helper)o).value);
        }
    }
}

Compare Strings

public class AccountNameComparator extends Comparator {
    public override Integer compare(Object a, Object b) {
        return ((Account)a).name.compareTo(((Account)b).name);
    }
}

Compare Dates

public class PriceHistoryPriceDateCompare extends Comparator {
    public override Integer compare(Object a, Object b) {
      return (DateTime.newInstance(((History__c)a).Price_Date__c.year(), ((History__c)a).Price_Date__c.month(),1).getTime().format()).compareTo((DateTime.newInstance(((History__c)b).Price_Date__c.year(), ((History__c)b).Price_Date__c.month(), 1).getTime()).format());
    }
	}

Test Class

@isTest(SeeAllData=true) static void testComparator(){
		List<Account> accountToSort = new List<Account>();
		for (Integer k = 70;k > 64;k--){
			Account acc = (Account)SmartFactory.createSObject('Account');
			acc.Name = String.fromCharArray(new List<integer> {k});
			insert acc;
			accountToSort.add(acc);
		}

		Test.startTest();
    	Comparator.sort(accountToSort, new AccountNameComparator());
			System.assertEquals(accountToSort.get(0).Name, 'A');
			System.assertEquals(accountToSort.get(1).Name, 'B');
			System.assertEquals(accountToSort.get(2).Name, 'C');
			System.assertEquals(accountToSort.get(3).Name, 'D');
			System.assertEquals(accountToSort.get(4).Name, 'E');
			System.assertEquals(accountToSort.get(5).Name, 'F');
		Test.stopTest();
	}

Apex Cache RecordTypes for fast retrieval

Apex class to lookup record types and cache them

  private static Map<Schema.SObjectType,Map<String,Id>> rtypesCache;

static {
   rtypesCache = new Map<Schema.SObjectType,Map<String,Id>>();//convenient map, formatted from r    esults.
}

  public static Map<String, Id> getRecordTypeMapForObjectGeneric(Schema.SObjectType token) {
      Map<String, Id> mapRecordTypes = rtypesCache.get(token);
      if (mapRecordTypes == null) {
          mapRecordTypes = new Map<String, Id>();
          rtypesCache.put(token,mapRecordTypes);
      } else {
           return mapRecordTypes;
      }

      Schema.DescribeSObjectResult obj = token.getDescribe();
      if (results == null || results.isEmpty()) {
          String soql = 'SELECT Id, Name, DeveloperName, sObjectType FROM RecordType WHERE IsActive = TRUE';
          try {
              results = Database.query(soql);
          } catch (Exception ex) {
              results = new List<SObject>();
          }
      }

      Map<Id,Schema.RecordTypeInfo> recordTypeInfos = obj.getRecordTypeInfosByID();
      for (SObject rt : results) {
          if (recordTypeInfos.get(rt.Id) != null) {
              if (recordTypeInfos.get(rt.Id).isAvailable()) {
                  mapRecordTypes.put(String.valueOf(rt.get('DeveloperName')),rt.Id);
              }
              else {
                  System.debug('The record type ' + rt.get('DeveloperName') + ' for object ' + rt.get('sObjectType') + ' is not availiable for the user.');
              }
          }
      }
      return mapRecordTypes;
    }

Test Class

  @isTest(SeeAllData=true) static void testGetRecordTypeMapForObjectGeneric(){
      Test.startTest();
        Map<String, Id> caseRecordTypeId = App_Service.getRecordTypeMapForObjectGeneric(Case.SobjectType);
        System.Assert(caseRecordTypeId.get('Error')!=null);
        System.Assert(caseRecordTypeId.get('Internal')==null);
      Test.stopTest();
    }
%d bloggers like this: