• DMario Lewis
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 2
    Replies
Im currently writing a trigger handler to add a parent id to an account based on a variable called DUNS Number. When I add a new record I run into a DML Error 50001 limit reached.  

/******************************************************************************************************
 * @NAME         : OnBeforeInsert
 * @DESCRIPTION  : Catch any missing data before inserting a Account record
 * @PARAMETER    : List of Accounts to affect
 ******************************************************************************************************/
    public void BeforeInsert(Account[] newAccounts){
        Map<String, Id> dunsMap = getDUNSMap(newAccounts);
        try{
            FOR(Account acct : newAccounts){
                //acct.Sales_Region__c = region.getCorrectAccountRegionSingle(acct);
                if(acct.Parent_DUNS_Number__c != null){
                    acct.ParentId = dunsMap.get(acct.Parent_DUNS_Number__c);
                }
            }
        }catch(Exception ex){}
    }

 /******************************************************************************************************
 * @NAME         : getDUNSMap
 * @DESCRIPTION  : Gets the Id of the Parent Account based on the DUNS Number
 * @PARAMETER    : Account List to search for
 * @RETURN       : Map<String, Id> of the Queried Parent Account with their duns numbers
 ******************************************************************************************************/   
    private Map<String, Id> getDUNSMap(List<Account> accountList){
        Map<String, Id> dunsMap = new Map<String, Id>();
        
        List<String> parentDunsList = new List<String>();
        
        For(Account a : accountList){ 
            parentDunsList.add(a.Parent_DUNS_Number__c);
        }
        List<Account> parentList =[SELECT id, DUNS_Number__c FROM Account WHERE DUNS_Number__c In : parentDunsList];
        
        FOR(Account a1 : parentList){
            dunsMap.put(a1.DUNS_Number__c, a1.Id);
        }
        
        return dunsMap;
    }