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
tvvsn suryanarayanatvvsn suryanarayana 

i have custom fields RDebit and Rcredit On contact and custom fields credit, debit,Contact(lookup) on opportunity. i want to "sum" the credit and debit values for all related contact opportunities and it has to update on Contact Object Using trigger?

Best Answer chosen by tvvsn suryanarayana
Abhishek BansalAbhishek Bansal
Hi,

Please find your required trigger code below:
trigger updateContactDetails on Opportunity(after insert, after update, after delete, after undelete){
    Set<Id> contactIdSet = new Set<Id>();
    if(trigger.isInsert || trigger.isUpdate || trigger.isUndelete){
        for(Opportunity opp : trigger.new){
            if(trigger.isInsert || trigger.isUndelete){
                if(opp.ContactId != null){
                    contactIdSet.add(opp.ContactId);
                }
            }
            else if(trigger.isUpdate && (trigger.oldMap.get(opp.id).ContactId != opp.ContactId || trigger.oldMap.get(opp.id).Credit__c != opp.Credit__c || trigger.oldMap.get(opp.id).Debit__c != opp.Debit__c)){
                if(opp.ContactId != null){
                    contactIdSet.add(opp.ContactId);
                }
                if(trigger.oldMap.get(opp.id).ContactId != null){
                    contactIdSet.add(trigger.oldMap.get(opp.id).ContactId);
                }
            }
        }
    }
    else if(trigger.isDelete){
        for(Opportunity opp : trigger.old){
            if(opp.ContactId != null){
                contactIdSet.add(opp.ContactId);
            }
        }
    }
    if(contactIdSet.size() > 0){
        List<Contact> listOfContactsToUpdate = new List<contact>();
        
        for(Contact con : [Select RDebit__c, RCredit__c, (Select Debit__c, Credit__c from Opportunities__r) from Contact where ID IN :contactIdSet]){
            con.RDebit__c = 0;
            con.RCredit__c = 0;
            
            for(Opportunity childOpp : con.Opportunities__r){
                if(childOpp.Debit__c != null){
                    con.RDebit__c += childOpp.Debit__c;
                }
                if(childOpp.Credit__c != null){
                    con.RCredit__c += childOpp.Credit__c;
                }
            }
            listOfContactsToUpdate.add(con);
        }
        
        if(listOfContactsToUpdate.size() > 0){
            update listOfContactsToUpdate;
        }
    }
}
//Take care of the compilation issues
Please let me know if you find any issues with this.

Thanks,
Abhishek Bansal.

All Answers

Abhishek BansalAbhishek Bansal
Hi,

Please find your required trigger code below:
trigger updateContactDetails on Opportunity(after insert, after update, after delete, after undelete){
    Set<Id> contactIdSet = new Set<Id>();
    if(trigger.isInsert || trigger.isUpdate || trigger.isUndelete){
        for(Opportunity opp : trigger.new){
            if(trigger.isInsert || trigger.isUndelete){
                if(opp.ContactId != null){
                    contactIdSet.add(opp.ContactId);
                }
            }
            else if(trigger.isUpdate && (trigger.oldMap.get(opp.id).ContactId != opp.ContactId || trigger.oldMap.get(opp.id).Credit__c != opp.Credit__c || trigger.oldMap.get(opp.id).Debit__c != opp.Debit__c)){
                if(opp.ContactId != null){
                    contactIdSet.add(opp.ContactId);
                }
                if(trigger.oldMap.get(opp.id).ContactId != null){
                    contactIdSet.add(trigger.oldMap.get(opp.id).ContactId);
                }
            }
        }
    }
    else if(trigger.isDelete){
        for(Opportunity opp : trigger.old){
            if(opp.ContactId != null){
                contactIdSet.add(opp.ContactId);
            }
        }
    }
    if(contactIdSet.size() > 0){
        List<Contact> listOfContactsToUpdate = new List<contact>();
        
        for(Contact con : [Select RDebit__c, RCredit__c, (Select Debit__c, Credit__c from Opportunities__r) from Contact where ID IN :contactIdSet]){
            con.RDebit__c = 0;
            con.RCredit__c = 0;
            
            for(Opportunity childOpp : con.Opportunities__r){
                if(childOpp.Debit__c != null){
                    con.RDebit__c += childOpp.Debit__c;
                }
                if(childOpp.Credit__c != null){
                    con.RCredit__c += childOpp.Credit__c;
                }
            }
            listOfContactsToUpdate.add(con);
        }
        
        if(listOfContactsToUpdate.size() > 0){
            update listOfContactsToUpdate;
        }
    }
}
//Take care of the compilation issues
Please let me know if you find any issues with this.

Thanks,
Abhishek Bansal.
This was selected as the best answer
tvvsn suryanarayanatvvsn suryanarayana
Thank you Abhishek, there are no issues.