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();
    }

Create Spring Bean On-Demand using Bean Lookup

Bean Configuration – create prototype method on demand using look-up method

@Configuration
@Lazy(value=true)
public class SalesforceAuthenticationConfigImpl implements SalesforceAuthenticationConfig {

    @Bean(name="loginToProductionSalesforce")
    @Scope(value="prototype")
	public PartnerConnection loginToProductionSalesforce() throws ConnectionException, InterruptedException {
		int retryCounter=1;
		productionPartnerConnection();
		Date now = new Date();		
		while (partnerConnection==null && retryCounter <= RETRYAMOUNT || TimeUnit.MILLISECONDS.toMinutes(now.getTime() - partnerConnection.getServerTimestamp().getTimestamp().getTimeInMillis()) > MINUTESTILLTIMEOUT){
			logger.info("loginToProductionSalesforce retry attempt: " + retryCounter);
			productionPartnerConnection();
        	retryCounter++;
        	Thread.sleep(30*1000);
		}
		return partnerConnection;
	}


    @Bean
	public SalesforceAuthenticationFactory salesforceAuthenicationFactory(){
		return new SalesforceAuthenticationFactory() {
			@Override
			public PartnerConnection createPartnerConnection() throws ConnectionException, InterruptedException {
				return loginToProductionSalesforce();
			}
		};
	}

}

Configuration factory

@Bean
	public SalesforceAuthenticationFactory salesforceAuthenicationFactory(){
		return new SalesforceAuthenticationFactory() {
			@Override
			public PartnerConnection createPartnerConnection() throws ConnectionException, InterruptedException {
				return loginToProductionSalesforce();
			}
		};
	}

Test Class

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={SalesforceAuthenticationConfigImpl.class})
public class SalesforceAuthenticationServiceTest {

	@Autowired SalesforceAuthenticationFactory salesforceAuthenicationFactory;

     @Test
	public void testSalesforceAuthenitcationFactory(){
		try {
			PartnerConnection pConnection = salesforceAuthenicationFactory.createPartnerConnection();
			assertNotNull(pConnection.getConfig().getSessionId());
		} catch (ConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

Each time you call createPartnerConnection()() you will receive the reference to newly created instance of PartnerConnection class.

Visualforce Lookup Field

I have seen that allot of people are struggling with creating lookup field in Salesforce. It is very easy by just referencing the child relationship id in an apex:Inputfield. See examples below:

When using a standard controller in your page you can do the following:

<apex:page standardcontroller="Contact">
<apex:form>
<apex:inputField value="{!Contact.OwnerId}"/>
<apex:inputField value="{!Contact.AccountId}"/>
</apex:form>
</apex:page>



<apex:page standardcontroller="Account">
<apex:form>
<apex:inputField value="{!Account.OwnerId}"/>
<apex:inputField value="{!Account.RecordTypeId}"/>
<apex:inputField value="{!Account.ParentId}"/>
</apex:form>
</apex:page>

When using a custom controller in your page you can do the following:


<apex:page controller="UserReassignController">
<apex:form>
User From <apex:inputField label="UserFrom" id="UserFrom" value="{!User.OwnerId}" required="true"/>
User To <apex:inputField label="UserTo" id="UserTo" value="{!User.OwnerId}" required="true"/>
<apex:commandButton action="{!ReassignAllAccountContactOpportunities}" value="Reassign"/>
</apex:form>
</apex:page>


public with sharing class UserReassignController {

   private Account userFromOwnerId = new Account();
    private Account userToOwnerId = new Account();
  
    public Account UserFrom { get
    {
        return userFromOwnerId;
    } 
    set
    {
        userFromOwnerId = value;
    } 
    }

    public Account UserTo { get
    {
         return userToOwnerId;
    } 
    set
    {
        userToOwnerId = value;
    }
    }
}