You need to sign in to do that
Don't have an account?
gregj777
Apex trigger not refreshing on data changes
I have the below apex trigger in place but it doesn't see me to be refreshing the filed when the data changes. Can anyone help me figure out what I need to add to make sure that operation happens. Something needs to be missing.
trigger updateContract on Opportunity (before Update) { set<id> contIds = new set<id>(); list<contract> listcon = new list<contract>(); set<id> oppIds = new set<id>(); Double InitialSum ; For(Opportunity o :trigger.new) { contIds.add(o.contract__c); oppIds.add(o.id); } Map<id,contract> MapofCons = new Map<id,contract>([select id, name,Total_Opportunity_Amount__c from contract where id in :contIds]); Map<id, opportunity> MapOfOpps = new Map<id,opportunity>([select id,name, amount from opportunity where id in :oppIds]); For(opportunity o: trigger.new) { If( MapofCons.get(o.contract__c).Total_Opportunity_Amount__c ==null) InitialSum = 0 ; else InitialSum = MapofCons.get(o.contract__c).Total_Opportunity_Amount__c; If((MapOfOpps.get(o.id).amount==null)&& (o.amount!=null)) { MapofCons.get(o.contract__c).Total_Opportunity_Amount__c = InitialSum + o.amount; Listcon.add(MapofCons.get(o.contract__c)); } else If((MapOfOpps.get(o.id).amount!=null)&& (o.amount==null)) { MapofCons.get(o.contract__c).Total_Opportunity_Amount__c = InitialSum ; Listcon.add(MapofCons.get(o.contract__c)); } else { if(mapofOpps.get(o.id).amount != o.amount) { MapofCons.get(o.contract__c).Total_Opportunity_Amount__c = InitialSum + o.amount- mapofOpps.get(o.id).amount ; Listcon.add(MapofCons.get(o.contract__c)); } } } update Listcon; }
Hi,
(1) I would use the after trigger ... but this a matter of taste.
(2) I would use the trigger.oldMap to access the prev values instead of query the opps again ...
(3) Check if the trigger is eally active
(4) turn on debug, use the code below, run your use case that executes the trigger > check the debug log & share it here!