Skip to main content
All,

I need some help with some Apex coding since I am a beginner and I appreciate all help. I have tried to modfiy the coding completed by another developer but it is not working.

below are the object and fields that will be involved with the trigger

Object

Opportunities

Field 

Engagement_Total_Fees__c  (Currency(16, 2) - label name Engagement

Custom Object

Matter__c

Fields

Total_Fees__c    (Currency(16, 2) 

Deal__c  Lookup(Opportunity) 

Schema below - Lookup relationship from Matters__c (object label Engagement)

Apex Sum on Opportunities

Request.

When a  record is updated, created, or deleted in the Total_Fees__c field in Matter__c object, I want a total sum in Total_Fees__c field to be inserted in the Engagement_Total_Fees__c field within the related Opportunity.

Will it best to place this trigger in Opportunities Or Matter__c object? 

I appreciate all of your help.

Regards

 
12 answers
  1. Mar 12, 2015, 2:39 AM
    Change trigger.isInsert to trigger.isUpdate in line 11

     

    trigger UpdateToTalFeeOnOpportunity on Matter__c(after insert,after update,after delete){

    Set<Id> oppIds = new Set<Id>();

    List<Opportunity> lstOppToBeUpdated = new List<Opportunity>();

    if(Trigger.isAfter){

    if(Trigger.isInsert || Trigger.isUpdate){

    for(Matter__c matter : trigger.new){

    if(Trigger.isInsert){

    oppIds.add(matter.Deal__c);

    }

    else if(Trigger.isUpdate){

    if(matter.Total_Fees__c != trigger.OldMap.get(matter.Id).Total_Fees__c){

    oppIds.add(matter.Deal__c);

    }

    }

    }

    }

    if(Trigger.isDelete){

    for(Matter__c matter : trigger.old){

    oppIds.add(matter.Deal__c);

    }

    }

    }

    if(!oppIds.isEmpty()){

    for(Opportunity opp :[SELECT Id,Engagement_Total_Fees__c,(SELECT Id,Total_Fees__c FROM Engagements__r ) FROM Opportunity WHERE Id IN :oppIds]){

    Decimal totalFee = 0;

    for(Matter__c matter : opp.Engagements__r ){

    if(matter.Total_Fees__c != null){

    totalFee = totalFee + matter.Total_Fees__c;

    }

    }

    opp.Engagement_Total_Fees__c = totalFee;

    lstOppToBeUpdated.add(opp);

    }

    if(!lstOppToBeUpdated.isEmpty()){

    update lstOppToBeUpdated;

    }

    }

    }

     
0/9000