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 trigger how to compare new incoming salesforce Accountids with existing salesforce Accountids in the database?.

Hi Guys,
Everything is fine with my trigger which updates child object(Commissions__c) records under Accounts based on Rule_ID in Commission object. If the new record is having new Rule_ID__C it just inserts new record if the Rule_ID doesn't match with existing record or if it matches with existing Commission records then it will update the existing child object(Commission) record. As the data is coming from third party 
through integration, some times they are sending invalid account ids which leads to create duplicate records in the child object, if I'm 
able to compare the existing salesforce Account id's with new incoming salesforce Account id's my issue will resolve.

Thanks in Advance and here is my code. I'm not providing related apex class code(commissionHelper).

Trigger Commissions_Trigger on Commission__c (after insert, after update) {

    try{
        if(Trigger.isAfter && Trigger.isInsert && !CommissionHelper.isCommissionTgrAlreadyRunning){            
            CommissionHelper.isCommissionTgrAlreadyRunning = true;
            
            set<id> accountids = new set<ID>();
            List<string> ruleIDs = new List<string>();
            
            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_Meter__c cm : Trigger.new){
                if(string.isNotBlank(cm.Rule_ID__c)){
                    triggerCommissionMetersMap.put(cm.Rule_ID__c+cm.account__c,cm); 
                    ruleIDs.add(cm.Rule_ID__c);
                    accountids.add(cm.account__c);               
                }
            }

            List<Commission__c> existingCommissions = [SELECT Id, Name__c, Sales__c, Status__c, Account__c, Description__c, Rule_ID__c FROM Commission_Meter__c where Rule_ID__c IN :ruleIDs and account__c IN :accountids and ID NOT IN :trigger.new limit 10000];

            for(Commission__c cm : existingCommissions){
            
            string keypoint = cm.Rule_ID__c+cm.account__c;
                Commission_Meter__c cmNew = triggerCommissionMetersMap.get(keypoint);
                if(cmNew != null && string.isNotBlank(cmNew.Rule_ID__c) && string.isNotBlank(cmNew.Account__c) && cmNew.Rule_ID__c.equals(cm.Rule_ID__c) && cmNew.Account__c.equals(cm.Account__c) ){
                    
                    if(string.isNotBlank(cmNew.Rule_ID__c))cm.Rule_ID__c = cmNew.Rule_ID__c;
                    if(string.isNotBlank(cmNew.Name__c))cm.Name__c = cmNew.Name__c;
                    if((cmNew.Sales__c != null))cm.Sales__c = cmNew.Sales__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;
            
        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);
    }
}
R Z KhanR Z Khan
Hi Laxmaya,

1. whats Commission_Meter__c? Is the trigger on Commission__c or Commission_Meter__c?
2. for htis line  if(string.isNotBlank(cmNew.Rule_ID__c))cm.Rule_ID__c = cmNew.Rule_ID__c; is useless as previosuly you check that RuleId field is the samae on your cm and cmNew records. 
3. If you are trying to prevent duplciate data, I would recommend running before insert/update trigger and then do the mapping there and prevent the insert, instead of inserting duplicate records, then updating the exisitng records and deleting records that you jsut inserted. 

Not sure what do you mean by creating duplicate Account Ids. If the Id is invalid and your Account__c field is a lookup field the validation will be thrown.