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