• #9138
    Salesforce | Audrey Audrey #447
    Forcetalks

    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?

     

    #9168
    Salesforce | Geek Geek #442
    Forcetalks

    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<Lead> 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<Lead> 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<Group> lazyQueues {
        get {
            if (lazyQueues == null)
                lazyQueues = [Select Id,Name from Lead LIMIT 10];
            return lazyQueues;
        }
        private set;
     }
     public static void rebuildingCaches() {
          lazyQueues == null;
          
     }
    }
    #33258
    Salesforce | Parul Parul #2
    Forcetalks

    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){
    System.debug(‘leadObject>>>>>>’+leadObject.FirstName);
    }
    if(Trigger.isAfter && Trigger.isupdate){
    System.debug(‘leadObject>>>>>>’+leadObject.FirstName);
    }
    }
    }

    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 ){
    System.debug(‘leadObject>>>>>>’+leadObject.FirstName);
    }
    if(Trigger.isAfter ){
    System.debug(‘leadObject>>>>>>’+leadObject.FirstName);
    }
    }
    }

    Thanks

     

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

Please to reply to this topic.

CONTACT US

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

Sending

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 2018 Forcetalks. All Right Reserved.

Log in with your credentials

or    

Forgot your details?