Skip to main content
All,

I have a one to many relationship between Opportunities and a custom object name Loan__c (one Opportunity to many Loan__c). When I delete a loan record, my trigger below does not fire. 

Does anyone know how to update this code so that when I delete a loan record, the trigger will fire?

Trigger updateDealWithLoanInfo2 on Loan__c (after insert, after update) {

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

        map<id,id> dealMap = new map<id,id>();

        set<id> consumerDeals = new set<id>();

        set<id> consumerDealsYellow = new set<id>();

        set<id> participatedDeals = new set<id>();

        list<Loan__c> loans = new List<Loan__c>();  

        list<Opportunity> deals = new List<Opportunity>();  

        list<Opportunity> updatedDeals = new List<Opportunity>();  

        for(Loan__c l : Trigger.new) {

            if(l.Deal__c <> NULL) {

                dealMap.put(l.Deal__c, l.Id);

            }

        }

        if(dealMap.size()>0){

            loans = [Select Deal__c, Consumer__c, Participated__c from Loan__c where Deal__c in :dealMap.keySet()];

            for (Loan__c l2 : loans){

                if(l2.Consumer__c == 'Consumer'){

                    consumerDeals.add(l2.Deal__c);

                }

                if(l2.Consumer__c == 'Ambiguous' || l2.Consumer__c == NULL || l2.Consumer__c == 'Insufficient information'|| l2.Consumer__c == 'Not reviewed'){

                    consumerDealsYellow.add(l2.Deal__c);

                }

                if(l2.Participated__c == TRUE){

                    participatedDeals.add(l2.Deal__c);

                }

            }

            deals = [Select Id, Consumer_Loan__c,Consumer_Loan_Hidden__c, Participated_Loan__c from Opportunity where Id in :dealMap.keySet()]; 

            for (Opportunity o : deals){

                if(consumerDeals.contains(o.Id)){

                    o.Consumer_Loan__c = TRUE;

                    o.Consumer_Loan_Hidden__c = FALSE;

                }else{

                    o.Consumer_Loan__c = FALSE;

                }

                if(consumerDealsYellow.contains(o.Id)){

                    o.Consumer_Loan_Hidden__c = TRUE;

                }else{

                    o.Consumer_Loan_Hidden__c = FALSE;

                }

                if(participatedDeals.contains(o.Id)){

                    o.Participated_Loan__c = TRUE;

                }else{

                    o.Participated_Loan__c = FALSE;

                }

                updatedDeals.add(o);

            }

            if(updatedDeals.size()>0){

                update updatedDeals;

            }

        }

    }
7 answers
  1. Jun 15, 2015, 3:19 PM
    Bhavana Adiga

    Did you change your code to look for delete in line 2 ? Currently it is checking only for if its insert or update.

    if(Trigger.isAfter && ( Trigger.isInsert || Trigger.isUpdate || Trigger.isDelete )) {

0/9000