+ Start a Discussion
HTANIRSHTANIRS 

Before Insert Trigger not updating Account

Hi,

My trigger is not checking Email exists in Account when inserting custom object with same email. But when I insert new custom object it is inserting account.

My Requirement:
1) Want to create a Custom Object by checking Email exists in Account or Not.
2) If Email Exists update the fields with that of custom object values.
3) If not Email Exists create New Account.
4) Delete the Custom Object Record once Account is created.

Kindly review my below code and please let me know what changes need to be done to achieve my requirement.
 
trigger updateCustomer on Customer__c (before insert, before update, after insert, after delete) {
    system.debug('--- Inside Customer Trigger ---');
                
    Set<String> cusSet = new Set<String>();
    List<Customer__c> cusList = [SELECT Id, Name FROM Customer__c WHERE Id IN: cusSet];        
    for(Customer__c cus : trigger.New) {
        System.debug('--- Inside Customer Obj: ---' + cus);
        cusSet.add(cus.Email__c);
    }
            
    List<Account> accList = new List<Account>();
    Map<String, List<Account>> accMap = new  Map<String, List<Account>>();
    accList = [SELECT Id, Name, PersonEmail, City__pc FROM Account WHERE PersonEmail IN: cusSet];
    
    System.debug('--- Account List ---' + accList);        
    
    if(!cusSet.isEmpty()){
        System.debug('--- cusSet ---' + cusSet);
        for(Account acc : accList) {
            System.debug('--- Account Email Map: ---' + acc);
            if(!accMap.containsKey(acc.PersonEmail)) {
                System.debug('--- Account Map: ---' + accMap);
                accMap.put(acc.PersonEmail, new List<Account> {acc});
            }
            else {
                System.debug('--- Inside Else Map ---');
                List<Account> acList = accMap.get(acc.PersonEmail);
                accList.add(acc);
                accMap.put(acc.PersonEmail, accList);
                System.debug('--- Inside Account List ---' + acList);
            }
        } 
    }
    
    if(trigger.isUpdate && trigger.isBefore) {
        for(Customer__c cus : Trigger.New) {
            System.debug('--- Inside Customer Forloop ---' + cus);
            if(accMap.containsKey(cus.Email__c)) {
                System.debug('--- Customer Email ---' + accMap);
                for(Account a : accMap.get(cus.Email__c)) {
                    System.debug('--- Matching Email ---' + a);
                    Account acct = new Account (id = a.Id);
                    acct.LastName = cus.Name;
                    acct.City__pc = cus.City__c;
                    accList.add(acct);
                }
                try {
                    System.debug('--- Inside try ---');
                    if(accList.size()>0) {
                        update accList;
                        System.debug('--- Inserted Account : ---');
                        System.debug('--- Account List: ---' + accList.size()); 
                    }
                }
                catch(Exception e) {
                    System.debug('The Following Exception has occurred: '+ e.getLinenumber() + ' : ' + e.getMessage());
                }
            }
        }
    }            
        
    if(trigger.isInsert && trigger.isAfter) {
        System.debug('--- Inside Insert ---');
        for(Customer__c cus : Trigger.New) {
            System.debug('--- Inside Customer Forloop ---' + cus);            
            if(!accMap.containsKey(cus.Email__c)) {
                System.debug('--- Customer Email ---' + accMap);
                Account acct = new Account();
                acct.PersonEmail = cus.Email__c;
                acct.LastName = cus.Name;
                acct.City__pc = cus.City__c;
                accList.add(acct);
            }
            try {
                System.debug('--- Inside try ---');
                if(accList.size()>0) {
                    insert accList;
                    System.debug('--- Inserted Account : ---');
                }
                System.debug('--- Account List: ---' + accList.size());
            }
            catch(Exception e) {
                System.debug('The Following Exception has occurred: '+ e.getLinenumber() + ' : ' + e.getMessage());
            }
        }
    }        
    
    if(trigger.isAfter && trigger.isDelete){
        for(Customer__c cus : Trigger.old){
            if(cus.Id != null) {
                cusSet.add(cus.Id);
            }
            try {
                System.debug('--- Inside try ---');
                if(cusList.size()>0) {
                    delete cusList;
                    System.debug('--- Deleted Customer : ---');
                }
                System.debug('--- Deleted List: ---' + accList.size());
            }
            catch(Exception e) {
                System.debug('The Following Exception has occurred: '+ e.getLinenumber() + ' : ' + e.getMessage());
            }
        }
    }            
}

Thanks in Advance
Best Answer chosen by HTANIRS
Maharajan CMaharajan C
Hi,

Your Trigger should be below for your requirement:

trigger updateCustomer on Customer__c (after insert) {                
    Set<String> cusSet = new Set<String>();
    Set<String> cusDelSet = new Set<String>();
    List<Account> accList = new List<Account>();
    List<Account> accListtoInsert = new List<Account>();
    List<Account> accListtoUpdate= new List<Account>();
    List<Customer__c> custListtoDelete = new List<Customer__c>();
    
    for(Customer__c cus : trigger.New) {
        System.debug('--- Inside Customer Obj: ---' + cus);
        cusSet.add(cus.Email__c);
    }
    
    Map<String, List<Account>> accMap = new  Map<String, List<Account>>();
    accList = [SELECT Id, Name, PersonEmail, City__pc FROM Account WHERE PersonEmail IN: cusSet];
    
    System.debug('--- Account List ---' + accList);        
    
    if(!cusSet.isEmpty()){
        System.debug('--- cusSet ---' + cusSet);
        for(Account acc : accList) {
            System.debug('--- Account Email Map: ---' + acc);
            if(!accMap.containsKey(acc.PersonEmail)) {
                accMap.put(acc.PersonEmail, new List<Account> {acc});
                System.debug('--- Account Map: ---' + accMap);
            }
            else {
                System.debug('--- Inside Else Map ---');
                List<Account> acList = accMap.get(acc.PersonEmail);
                accList.add(acc);
                accMap.put(acc.PersonEmail, accList);
                System.debug('--- Inside Account List ---' + acList);
                System.debug('--- Account Map: ---' + accMap);
            }
        } 
    }          
    
    if(trigger.isInsert && trigger.isAfter) {
        System.debug('--- Inside Insert ---');
        for(Customer__c cus : Trigger.New) {
            System.debug('--- Inside Customer Forloop ---' + cus);            
            if(!accMap.containsKey(cus.PersonEmail)) {
                System.debug('--- Customer Email ---' + accMap);
                Account acct = new Account();
                acct.PersonEmail = cus.Email__c;
                acct.LastName = cus.Name;
                acct.City__pc = cus.City__c;
                accListtoInsert.add(acct);
            }
            
            else {
                System.debug('--- Customer Email ---' + accMap);
                for(Account a : accMap.get(cus.PersonEmail)) {
                    System.debug('--- Matching Email ---' + a);
                    Account acct = new Account (id = a.Id);
                    acct.LastName = cus.Name;
                    acct.City__pc = cus.City__c;
                    accListtoUpdate.add(acct);
                    cusDelSet.add(cus.Id);
                }
            }            
        }
    } 
    
    if(cusDelSet.size() > 0){
        custListtoDelete = [Select Id from Customer__c where Id IN: cusDelSet];
     }
    
    
    try {
        System.debug('--- Inside try ---');
        if(accListtoInsert.size()>0) {    
            insert accListtoInsert;
            System.debug('--- Inserted Account : ---');
        }
        if(accListtoUpdate.size() > 0){
            update accListtoUpdate;
        }
        
        if(custListtoDelete.size() > 0){
           delete custListtoDelete; 
        }
        System.debug('--- Insert Account List: ---' + accListtoInsert.size());
        System.debug('--- Update Account List: ---' + accListtoUpdate.size());
    }
    catch(Exception e) {
        System.debug('The Following Exception has occurred: '+ e.getLinenumber() + ' : ' + e.getMessage());
    }
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi,

Your Trigger should be below for your requirement:

trigger updateCustomer on Customer__c (after insert) {                
    Set<String> cusSet = new Set<String>();
    Set<String> cusDelSet = new Set<String>();
    List<Account> accList = new List<Account>();
    List<Account> accListtoInsert = new List<Account>();
    List<Account> accListtoUpdate= new List<Account>();
    List<Customer__c> custListtoDelete = new List<Customer__c>();
    
    for(Customer__c cus : trigger.New) {
        System.debug('--- Inside Customer Obj: ---' + cus);
        cusSet.add(cus.Email__c);
    }
    
    Map<String, List<Account>> accMap = new  Map<String, List<Account>>();
    accList = [SELECT Id, Name, PersonEmail, City__pc FROM Account WHERE PersonEmail IN: cusSet];
    
    System.debug('--- Account List ---' + accList);        
    
    if(!cusSet.isEmpty()){
        System.debug('--- cusSet ---' + cusSet);
        for(Account acc : accList) {
            System.debug('--- Account Email Map: ---' + acc);
            if(!accMap.containsKey(acc.PersonEmail)) {
                accMap.put(acc.PersonEmail, new List<Account> {acc});
                System.debug('--- Account Map: ---' + accMap);
            }
            else {
                System.debug('--- Inside Else Map ---');
                List<Account> acList = accMap.get(acc.PersonEmail);
                accList.add(acc);
                accMap.put(acc.PersonEmail, accList);
                System.debug('--- Inside Account List ---' + acList);
                System.debug('--- Account Map: ---' + accMap);
            }
        } 
    }          
    
    if(trigger.isInsert && trigger.isAfter) {
        System.debug('--- Inside Insert ---');
        for(Customer__c cus : Trigger.New) {
            System.debug('--- Inside Customer Forloop ---' + cus);            
            if(!accMap.containsKey(cus.PersonEmail)) {
                System.debug('--- Customer Email ---' + accMap);
                Account acct = new Account();
                acct.PersonEmail = cus.Email__c;
                acct.LastName = cus.Name;
                acct.City__pc = cus.City__c;
                accListtoInsert.add(acct);
            }
            
            else {
                System.debug('--- Customer Email ---' + accMap);
                for(Account a : accMap.get(cus.PersonEmail)) {
                    System.debug('--- Matching Email ---' + a);
                    Account acct = new Account (id = a.Id);
                    acct.LastName = cus.Name;
                    acct.City__pc = cus.City__c;
                    accListtoUpdate.add(acct);
                    cusDelSet.add(cus.Id);
                }
            }            
        }
    } 
    
    if(cusDelSet.size() > 0){
        custListtoDelete = [Select Id from Customer__c where Id IN: cusDelSet];
     }
    
    
    try {
        System.debug('--- Inside try ---');
        if(accListtoInsert.size()>0) {    
            insert accListtoInsert;
            System.debug('--- Inserted Account : ---');
        }
        if(accListtoUpdate.size() > 0){
            update accListtoUpdate;
        }
        
        if(custListtoDelete.size() > 0){
           delete custListtoDelete; 
        }
        System.debug('--- Insert Account List: ---' + accListtoInsert.size());
        System.debug('--- Update Account List: ---' + accListtoUpdate.size());
    }
    catch(Exception e) {
        System.debug('The Following Exception has occurred: '+ e.getLinenumber() + ' : ' + e.getMessage());
    }
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C
This was selected as the best answer
HTANIRSHTANIRS
Hi Maharajan,

Thanks for your help. This is working.