1

1) As shown below in class, there are 2 methods, postInsert and postUpdate. I am trying to avoid another SOQL query inside these methods by using oldmap and newmap but as shown in postUpdate method, I am not able to get values of owner.type and other values. However, if I use trigger.new and list in my methods, I am able to retrieve those values. Is there any workaround to get the value directly from map and avoid writing another query ?

2) If I have 2 methods both of which need same query results, how can i avoid writing this query in both methods (I have written query for list of cases below in both the methods but would like to know how to avoid that) ?

3) Since this is my first time moving trigger logic to class, please suggest me any corrections to my below code and suggestions/ improvements

Trigger:

trigger TriggerOnCase on Case (before insert, before update, after insert, after update) {

    caseTrigger handler1 = new caseTrigger();

    if(trigger.isAfter){
        if(trigger.isInsert){
            handler1.postInsert(trigger.new);
        }
        if(trigger.isUpdate){
            handler1.postUpdate(/*trigger.new,*/trigger.oldMap, trigger.NewMap);
        }
    }
}

CaseTrigger Class

public with sharing class caseTrigger {

    public void postInsert(list<case> caseFetch){
        list<Case> caseList = new list<case>([Select Id, status, owner.type, owner.profile.name 
                               From Case Where ID IN :caseFetch]);      

        for(case c: caseList){
            if(c.Status != 'closed'){
                if(c.owner.type == 'User'){
                    postCases_User(c);
                } else if (c.owner.type == 'Queue'){
                    postCases_Queue(c);
                }
            }
        }
    }

    public void postUpdate(/*list<case> cList,*/map<id, case> oldMap, map<id, case> newMap){
        //list<Case> caseList1 = new list<case>([Select Id, status, owner.type, owner.profile.name 
          //                                        From Case Where ID IN :cList]);

        for(case c : newMap.values()){
           system.debug(c.OwnerId); //works
            system.debug(c.owner.type); //doesnt work
            system.debug(c.owner.profile.name); //doesnt work
        }

    }

    public static void postCases_User ( case c){
        system.debug(c.owner.type);
    }

    public static void postCases_Queue (case c){
       system.debug('FromHello'+c.owner.type);
    }

}

Thanks

1 Answer 1

2
  1. Trigger Context Variables will always hold field values of the object on which the trigger is running. if you need any related object field values, we need to query them. so the query you are doing in both post insert and post update is alright.

  2. move the list<case> caseList into class level variable and initialize it in the constructor using your query (you can directly access trigger.new & the other trigger context variables in your handler directly). then you can use it any other method you need it.

  3. you did a good start.. there are some very good references on trigger patterns. this will set you in right direction

https://developer.salesforce.com/page/Trigger_Handlers

https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices

http://developer.force.com/cookbook/recipe/trigger-pattern-for-tidy-streamlined-bulkified-triggers

https://github.com/kevinohara80/sfdc-trigger-framework

3
  • 1
    Thanks Vamsi Krishna for your inputs and posting those reference links. Very helpful.
    – sf_user
    Aug 7, 2015 at 13:19
  • another question Vamsi Krishna, I am confused on how to move query to a class and access them in my methods as I have to use 'IN'. Can you please explain. Thanks
    – sf_user
    Aug 7, 2015 at 13:38
  • 1
    you can directly use trigger.new in your query in the IN clause. like this : list<Case> caseList = new list<case>([Select Id, status, owner.type, owner.profile.name From Case Where ID IN : trigger.new] ); Aug 7, 2015 at 13:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .