function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
gregj777gregj777 

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;

}

 

Dirk GronertDirk Gronert

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!

 

 

trigger updateContract on Opportunity (after Update) {

  Set<id> contIds = new Set<id>();
  List<contract> listcon = new List<contract>();
  Double InitialSum ;

  for(Opportunity o :trigger.new){
    contIds.add(o.contract__c);
  }

  Map<id,contract> MapofCons = new Map<id,contract>([select id, name,Total_Opportunity_Amount__c from contract where id in :contIds]);

  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((trigger.oldMap.get(o.id).amount==null)&& (o.amount!=null)){ 
System.debug('(trigger.oldMap.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((trigger.oldMap.get(o.id).amount!=null)&& (o.amount==null)){
Sytem.debug('(trigger.oldMap.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{
System.debug('else');
 if(mapofOpps.get(o.id).amount != o.amount){
System.debug('mapofOpps.get(o.id).amount != o.amount');
 MapofCons.get(o.contract__c).Total_Opportunity_Amount__c = InitialSum + o.amount- trigger.oldMap.get(o.id).amount ; Listcon.add(MapofCons.get(o.contract__c)); } } } update Listcon; }