function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mohammed AyyubMohammed Ayyub 

Tips to decrease the SOQLs

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

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

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) {
            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);