Activity Forums Salesforce® Discussions How to use one SOQL query for both before and after trigger on the same object in Salesforce?

  • Piyush

    Member
    April 30, 2016 at 1:58 pm
  • PRANAV

    Member
    January 31, 2018 at 8:46 am

    Hi Ajay,

    Yes you can use static variables for this as they are meant for same purpose .

    Create a static list in your helper class and use same in your before trigger and after trigger

    public class cachequery {
        public static List<Account> lstaccount= [Select Id,Name from Account LIMIT 10];
    }

    The sample trigger to provide an idea

    trigger T1 on Account (before insert, after insert) {
        list<Account> lstcached = cachequery.lstaccount;
        if(Trigger.isBefore && Trigger.isInsert){
            system.debug(lstcached );
        }
        if(Trigger.isAfter && Trigger.isInsert){
            system.debug(lstcached );
        }
    }

    Hope this helps you.

  • Avnish Yadav

    Member
    September 29, 2018 at 8:55 pm

    Hello,

    use static variables for this.

    Thanks.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos