1
  1. I have one object Commission(UI it's a related list) under Accounts which is having Master-detail relation with Account.

  2. My requirement is whenever there is a new record in Commission it must insert, but for subsequent records for the same object(Commission) in different accounts it must update/insert basing on the field Rule_ID.

  3. If the Rule_ID already exists in any of the records(Commission) it must update or if it is having a new Rule_ID then it must insert.

  4. Here's my code and issues I'm facing with this are whenever there is a new record(if there are no records in commission) it's deleting the new record also and it's not updating the records it's just overriding the new values(if already records exists in Commission object).

Trigger Code

Trigger Commissions_Trigger_AfterInsert on Commission__c (after insert) {

    try{
        if(Trigger.isAfter && Trigger.isInsert && !CommissionHelper.isCommissionTgrAlreadyRunning){            
            CommissionHelper.isCommissionTgrAlreadyRunning = true;

            List<ID> toDeleteCommission = new List<ID>();
            Map<string,Commission___c> triggerCommissionMap = new Map<string,Commission__c> ();
            List<Commission__c> finalUpdate = new List<Commission__c>();

            for(Commission__c cm : Trigger.new){
                if(string.isNotBlank(cm.Rule_ID__c)){
                    triggerCommissionMap.put(cm.Rule_ID__c,cm);                
                }
            }

            List<Commission__c> existingCommissions = [SELECT Id, Name, Name__c, Sales__c, Status__c, Account__c, Description__c, Rule_ID__c FROM Commission__c where Rule_ID__c IN :triggerCommissionMap.keySet() limit 10000];

            for(Commission__c cm : existingCommissions){
                Commission__c cmNew = triggerCommissionMap.get(cm.Rule_ID__c);
                if(cmNew != null){
                  //cm.Account__c = cmNew.Account__c;
            cm.Rule_ID__c = cmNew.Rule_ID__c;
                    cm.Name__c = cmNew.Name__c;
                    cm.Sales__c = cmNew.Sales__c;
                    cm.Status__c = cmNew.Status__c;
                    cm.Description__c = cmNew.Description__c;

        finalUpdate.add(cm);
                todeleteCommission.add(cmNew.id);                   
                }
            }
            system.debug('The finalUpdate Info::'+finalUpdate);
            if(finalUpdate.size() > 0){
                system.Database.update(finalUpdate);
                CommissionHelper.toDeleteCommission(toDeleteCommission);    
            }

        }else{
            //Do nothing
            system.debug('This trigger logic is not executing 2nd time');
        }
    }catch(exception ex){
        system.debug('Some thing went wrong::EX:::'+ex);
    }
}

Apex class

public class CommissionHelper {

public static boolean isCommissionTgrAlreadyRunning = false;    
    @future
    public static void toDeleteCommission(List<ID> toDelete ){
        try{
             system.debug('toDelete:::::'+toDelete);
            List<Commission__c> cmToDelete = [select ID from Commission__c where ID IN :toDelete];
            System.Database.delete(cmToDelete);
        }catch(exception ex){
            system.debug('Some thing went wrong::EX:::'+ex);
        }
    }   
}
  • I re-read your question several times, but it's unclear to me what you are trying to achieve. Seeing that, so far, this question has no comments or answers, maybe it's an idea to rephrase it? – Sander de Jong Dec 15 '15 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.