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
Laxmaya ChnLaxmaya Chn 

Need help on this trigger

1.I have one custom object Commission(child to Accounts) with fields Rule_id, name, sales etc.
2.My requirement is whenever there is a new record in Commission it must insert, but for subsequent records it must check Rule_id in existing Commission records. If the Rule_id is same for existing and new(incoming) record it must update existing record.and also it must check commission record Account wise not all the commission records existed in the Commission object.
DETAILED INFO:
Need to add some trigger code to the following trigger as per our new requirement. For example consider there are 2 Account records Account1 and Account2 till now Account1 can't contain duplicate commission records based on Rule_id like this Account2 can't contain duplicate commission records based on Rule_id. For your understanding if Account1 is having a commission record with Rule_id 3344, in Account2 it must accept a new commission record with Rule_id 3344 but as per this code it's not accepting(deleting) any new commission record with existing Rule_id.


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, Status__c, Account__c, Description__c, Rule_ID__c FROM Commission__c where Rule_ID__c IN :triggerCommissionMetersMap.keySet() and ID NOT IN :trigger.new 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; if(string.isNotBlank(cmNew.Name__c))cm.Name__c = cmNew.Name__c; if(string.isNotBlank(cmNew.Status__c))cm.Status__c = cmNew.Status__c; if(string.isNotBlank(cmNew.Description__c))cm.Description__c = cmNew.Description__c; if(string.isNotBlank(cmNew.Rule_ID__c))cm.Rule_ID__c = cmNew.Rule_ID__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); }

HELPER CLASS

public class CommissionMeterHelper { public static boolean isCommissionMeterTgrAlreadyRunning = false; @future public static void toDeleteCommissionMeter(List<ID> toDelete ){ try{ system.debug('toDelete:::::'+toDelete); List<Commission_Meter__c> cmToDelete = [select ID from Commission_Meter__c where ID IN :toDelete]; System.Database.delete(cmToDelete); }catch(exception ex){ system.debug('Some thing went wrong::EX:::'+ex); } } }
 
James LoghryJames Loghry
Laxmaya,

Could you please post your code using the code format button (< >)?  Also, make sure it formats nicely (has proper tabs or spacing).  In its current state, your code is VERY difficult to read.

Thanks!