Forum Replies Created

  • A good practice is to create code that is as reusable as possible. So I would advise to keep query number as low as possible.
    So there are two ways you can run before and after triggers simultaneously, first is using static variable. You can include it in your trigger handler class.

    `Static Variable

    public class cachequery {
    public static List Leaddata= [Select Id,Name from Lead LIMIT 10];
    }

    You can then use it in the trigger something like this

    trigger T1 on Lead (before insert, after insert) {
    list Leaddata = cachequery.Leaddata;
    if(Trigger.isBefore && Trigger.isInsert){
    system.debug(Leaddata );
    }
    if(Trigger.isAfter && Trigger.isInsert){
    system.debug(Leaddata );
    }
    }`

    The other way is to use Lazy Loading
    Lazy loading is a technique to use cache in such a way that it your code consumes the query only when it actually need the data.

    `public with sharing class MyService {
    public static List lazyQueues {
    get {
    if (lazyQueues == null)
    lazyQueues = [Select Id,Name from Lead LIMIT 10];
    return lazyQueues;
    }
    private set;
    }
    public static void rebuildingCaches() {
    lazyQueues == null;

    }
    }`

Popular Salesforce Blogs

Popular Salesforce Videos