• Renee Bartel
  • NEWBIE
  • 10 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 4
    Replies
Hello! I am working on a script to automatically merge duplicate Accounts, based on the Accounts sharing the same number in the field "FirmCRD__c."

I am getting a Compile Error: Unexpected token 'global'. at line 83 column 5 when I attempt to save the class below in my Sandbox.
May you please help me fix/improve this script?
 
global class BatchFirmCRDAccountMerge implements Database.Batchable<sobject> {

    global  Database.QueryLocator start(Database.BatchableContext ctx) {                  
        return Database.getQuerylocator([SELECT Id, Name, MasterRecordId, FirmCRD__c FROM Account]); 
    }
    
    global void execute(Database.BatchableContext BC, List<Account> accounts ) {
        
        
        // Create a map with FirmCRD code and its account
        Map<String, List<Account>> FirmCRDAccountsToMerge = new Map<String, List<Account>>();
        
        
        for (Account account : accounts) {
                
            List<Account> accountsToMerge = FirmCRDAccountsToMerge.get(account.FirmCRD_Code__c);
            
            if (accountsToMerge == null) {
                accountsToMerge = new List<Account>();
                FirmCRDAccountsToMerge.put(account.FirmCRD_Code__c, accountsToMerge);
            }
            
            if (accountsToMerge.size() < 2) {
                accountsToMerge.add(account);
            } else {
                
                // Merges takes up to two records to merge with the master
                // https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_database.htm
                System.debug('Maximum of accounts to merge reached.');
            }
            
        }

        System.debug('****FirmCRD and acc map*** ');
        System.debug(FirmCRDAccountsToMerge);

        List<account> dupacc = new list<account>();
            
        for (String FirmCRDCode : FirmCRDAccountsToMerge.keySet()) {
            
            List<Account> accountsToMerge = FirmCRDAccountsToMerge.get(FirmCRDCode);
            
            if (accountsToMerge.size() > 1) {  
            
                Account masterAccount;
                List<Id> duplicatedAccounts = new List<Id>();           
          
                for (Account account : accountsToMerge) {
                    
                        if (masterAccount == null) {
                            masterAccount = account;
                                      }

                System.debug('***Master account*** ' + masterAccount);
                System.debug('***Duplicated accounts*** ' + duplicatedAccounts);
                
                Database.MergeResult[] results = Database.merge(masterAccount, duplicatedAccounts, false);
                
                System.debug('***results merged** ' + results);
                
                for (Database.MergeResult res : results) {
                    if (res.isSuccess()) {
                        System.debug('Master record ID: ' + res.getId());
                        System.assertEquals(masterAccount.Id, res.getId());               
                        List<Id> mergedIds = res.getMergedRecordIds();
                        System.debug('IDs of merged records: ' + mergedIds);                       
                    } else {
                        for (Database.Error err : res.getErrors()) {
                            System.debug(err.getMessage());
                        }
                    }                       
                }                
            }
            
            // If the DML limit is reached, breaks the execution and continue on the next Batch execution
            if (Limits.getDMLRows() == Limits.getLimitDMLRows()) {
                System.debug('DML limit reached. Shall continue on the next execution');
                break;
            }

    
 
    global void finish(Database.BatchableContext BC) {
         
    }

}
Hello! I am working on a script to automatically merge duplicate Accounts, based on the Accounts sharing the same number in the field "FirmCRD__c."

I am getting a Compile Error: Unexpected token 'global'. at line 83 column 5 when I attempt to save the class below in my Sandbox.
May you please help me fix/improve this script?
 
global class BatchFirmCRDAccountMerge implements Database.Batchable<sobject> {

    global  Database.QueryLocator start(Database.BatchableContext ctx) {                  
        return Database.getQuerylocator([SELECT Id, Name, MasterRecordId, FirmCRD__c FROM Account]); 
    }
    
    global void execute(Database.BatchableContext BC, List<Account> accounts ) {
        
        
        // Create a map with FirmCRD code and its account
        Map<String, List<Account>> FirmCRDAccountsToMerge = new Map<String, List<Account>>();
        
        
        for (Account account : accounts) {
                
            List<Account> accountsToMerge = FirmCRDAccountsToMerge.get(account.FirmCRD_Code__c);
            
            if (accountsToMerge == null) {
                accountsToMerge = new List<Account>();
                FirmCRDAccountsToMerge.put(account.FirmCRD_Code__c, accountsToMerge);
            }
            
            if (accountsToMerge.size() < 2) {
                accountsToMerge.add(account);
            } else {
                
                // Merges takes up to two records to merge with the master
                // https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_database.htm
                System.debug('Maximum of accounts to merge reached.');
            }
            
        }

        System.debug('****FirmCRD and acc map*** ');
        System.debug(FirmCRDAccountsToMerge);

        List<account> dupacc = new list<account>();
            
        for (String FirmCRDCode : FirmCRDAccountsToMerge.keySet()) {
            
            List<Account> accountsToMerge = FirmCRDAccountsToMerge.get(FirmCRDCode);
            
            if (accountsToMerge.size() > 1) {  
            
                Account masterAccount;
                List<Id> duplicatedAccounts = new List<Id>();           
          
                for (Account account : accountsToMerge) {
                    
                        if (masterAccount == null) {
                            masterAccount = account;
                                      }

                System.debug('***Master account*** ' + masterAccount);
                System.debug('***Duplicated accounts*** ' + duplicatedAccounts);
                
                Database.MergeResult[] results = Database.merge(masterAccount, duplicatedAccounts, false);
                
                System.debug('***results merged** ' + results);
                
                for (Database.MergeResult res : results) {
                    if (res.isSuccess()) {
                        System.debug('Master record ID: ' + res.getId());
                        System.assertEquals(masterAccount.Id, res.getId());               
                        List<Id> mergedIds = res.getMergedRecordIds();
                        System.debug('IDs of merged records: ' + mergedIds);                       
                    } else {
                        for (Database.Error err : res.getErrors()) {
                            System.debug(err.getMessage());
                        }
                    }                       
                }                
            }
            
            // If the DML limit is reached, breaks the execution and continue on the next Batch execution
            if (Limits.getDMLRows() == Limits.getLimitDMLRows()) {
                System.debug('DML limit reached. Shall continue on the next execution');
                break;
            }

    
 
    global void finish(Database.BatchableContext BC) {
         
    }

}