+ Start a Discussion
Mohammed AyyubMohammed Ayyub 

Tips to decrease the SOQLs

Replace each query by schema describe call:
Replcae this:
//REPLACE THIS LINE
RecordType closedWonRecordType = [SELECT Id from RecordType WHERE SobjectType = 'Opportunity' AND Name = 'Closed Won
' LIMIT 1];

//FROM
Id closedWonRecordTypeId = Opportunity.getDescribe().getRecordTypeInfosByName().get('Closed Won').getRecordTypeId();
David Holland 6David Holland 6
Mohammed

We have developed a Utilities class that stores these all in a static Map, which means you don't need to call the schema multiple times for the same object:
 
public static Map<String, Map<String, Id>> recordTypeIdCache = new Map<String, Map<String, Id>>();
    
    public static Id getRecordTypeId(String objName, String rtName) {
        if(!recordTypeIdCache.containsKey(objName)){
            Map<String, Id> valuesToPut = new Map<String, Id>();
            Map<String,Schema.RecordTypeInfo> recordTypeInfo = Schema.getGlobalDescribe().get(objName).getDescribe().getRecordTypeInfosByName();
            for(String recordTypeInfoName : recordTypeInfo.keySet()){
                valuesToPut.put(recordTypeInfoName, recordTypeInfo.get(recordTypeInfoName).getRecordTypeId());
            }
            recordTypeIdCache.put(objName, valuesToPut);
        }
        return recordTypeIdCache.get(objName).get(rtName);
    }