• #9138
    Salesforce | Audrey Audrey #482

    How can i use same SOQL script for both before and after triggers that are run on same object?

    I have use case in which I have to query Lead data and have to fire both before and after trigger on the same object. Any way to do that using a same SOQL Statement? Do I have to hardcode two different queries?


    Salesforce | Geek Geek #478

    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.

    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.

    Salesforce | Parul Parul #2

    Hi Audrey,

    There is different DML operations i.e. Insert,Update,Delete on which any Trigger is fire,Out of these only for UPDATE we can write same SOQL for before and after Trigger.

    trigger checksameQuery on Lead (before update, after update) {

    List<Lead> listofLead = [Select id,FirstName,LastName from Lead where id =: Trigger.new];

    for(Lead leadObject : listofLead){
    if(Trigger.isBefore && Trigger.isupdate){
    if(Trigger.isAfter && Trigger.isupdate){

    In case of other INSERT DML we can use Trigger.New for both before and After Trigger


    rigger checksameQuery on Lead (before insert, after insert) {

    for(Lead leadObject : Trigger.New){
    if(Trigger.isBefore ){
    if(Trigger.isAfter ){



Viewing 3 posts - 1 through 3 (of 3 total)

Please to reply to this topic.


We're not around right now. But you can send us an email and we'll get back to you, asap.


About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2019 Forcetalks. All Right Reserved.

Log in with your credentials


Forgot your details?