I have 3 classes, and in those classes I have different methods. Those methods I kept in handler class, and this handler class is calling from trigger.

In those classes I have multiple queries on Account and Contact, based on Opportunity. What I need is to consolidate those queries, but the main problem is I am calling those multiple methods in trigger from different events (i.e, before insert, after insert, after update).

On Account, (assume that I am getting all ids from Opportunity):

select id, AB2__ABAccountType__c from Account where id in :agencyids,

select id, Name FROM Account WHERE Id IN :acctIds,

select id from Account where id in :WalmartAccounts

On Contact (assume that I am getting all ids from Opportunity).

select id, Account.Type from Contact where id in :contactids,

select id from Contact where id in :WalmartContacts


  • 1
    Welcome to Salesforce.StackExchange Srini. What have you tried so far that hasn't worked in trying to create a single query for these? Where is it in understanding the relationships that you're stuck? Do you plan to put the results into a map so all of your classes can use them without re-running the query?
    – crmprogdev
    Mar 3 '16 at 17:56
  • Srini -- you should read about the Selector pattern: developer.salesforce.com/page/…
    – cropredy
    Mar 3 '16 at 18:22
  • Will you give me some example code that how can I construct a map and how to use that map in class
    – Srini
    Mar 4 '16 at 6:37
  • Hi I am new to salesforce so I can't understand your logic, so please put me some example code that how can I construct a map for the above queries and how to use those queries in main class where I wrote those queries in class.
    – Srini
    Mar 4 '16 at 7:02

What you need is a trigger framework. A proper trigger framework can reduce the number of queries that you need by breaking your code into distinct parts. One such framework, mentioned in the Trigger Pattern for Tidy, Streamlined, Bulkified Triggers, suggests having three distinct parts to your framework: a "before" segment, where you query any records you need, individual "during" segments where you process records, and an "after" segment where you do things like saving records back to the database.

Of course, such a framework may not always be practical, and many frameworks have pros and cons, but a simple level, the principle of a three-segment framework is sound. It's not enough to simply have a "helper class" that is called from the trigger (although it's a good start). It's about having a framework that actually encourages proper Aggregate-Query-Update patterns by first aggregating together all the data/queries you need, querying the data, and then performing updates based on the results of the first two steps.

You'll want to do some research on the subject, but the basic idea is that each of your modules needs a way to cooperate. For more information, I'd suggest doing a Google search for "salesforce trigger framework" and see what comes up.

  • Hi here is my trigger and handler class. I think so it works plz check it once and let me know if any changes.
    – Srini
    Mar 4 '16 at 6:40
  • Handler Class :public class OpportunityOptimTriggerHandler { public static void onBeforeUpdate(){ OpportunityAppianUtility.BooleanValuesSet(); OpportunityAppianUtility.AppianUpdateGroupId(); OpportunityGenericUtility.UpdateAcctManagerBeforeUpdate(); } public static void onBeforeInsUpd(){ OpportunityAppianUtility.AppianPAB(); OpportunityAdBookUtility.ABOppFieldsSet(); } public static void onAfterInsUpd(){ OpportunityGenericUtility.opportunityRelatedObjectSync(); OpportunityGenericUtility.ContractTotal(); } }
    – Srini
    Mar 4 '16 at 6:45
  • Trigger:trigger OpportunityTrigger on Opportunity (before insert,before update,after insert,after update) { if(Trigger.isBefore || Trigger.isUpdate) { Ust_OpportunityOptimTriggerHandler.onBeforeUpdate(); } if(Trigger.isInsert || Trigger.isUpdate) { Ust_OpportunityOptimTriggerHandler.onBeforeInsUpd(); } } if(Trigger.isAfter){ if(Trigger.isInsert || Trigger.isUpdate) { Ust_OpportunityOptimTriggerHandler.onAfterInsUpd(); Ust_OpportunityOptimTriggerHandler.onAfterInsUpd1(); } } }
    – Srini
    Mar 4 '16 at 6:54
  • As I mentioned the above queries need to optimize and I need to put those queries in handler class and I need to access those queries in class from handler class. for this can you give me some example code .
    – Srini
    Mar 4 '16 at 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.