You need to sign in to do that
Don't have an account?
Renee Bartel
Apex Script for Automatically Merging Accounts
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?
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) { } }
Three curly braces are missing. Please add three curly braces before the finish method.
Please mark this as the best answer if it helps.
Thanks
Shubham Jain
All Answers
Seems like the execute method is not closed.Can you please check on the closing braces .
Hope this is helpful..Please mark this as best answer!
Regards,
Ranjan
Three curly braces are missing. Please add three curly braces before the finish method.
Please mark this as the best answer if it helps.
Thanks
Shubham Jain
May you please be more specific?
Which lines of closing braces?
Thank you so very much!
Thanks so much!
You can implement the Schedulable interface in the same class and then you can schedule it.
Thanks
Shubham Jain
Implement the Schedulable interface and add the below code in the class
global class BatchFirmCRDAccountMerge implements Database.Batchable<sobject>,Schedulable {
global void execute(SchedulableContext ctx) {
BatchFirmCRDAccountMerge bch = new BatchFirmCRDAccountMerge();
DataBase.executeBatch(bch);
}
global void schedule(String cronExpression) {
BatchFirmCRDAccountMerge sch = new BatchFirmCRDAccountMerge();
System.schedule('Schedule Batch', cronExpression, sch);
}
}
To schedule, the batch runs this script in the anonymous window
BatchFirmCRDAccountMerge sch = new BatchFirmCRDAccountMerge();
// Seconds Minutes Hours Day_of_month Month Day_of_week optional_year
You can set the cron expression as how you want to schedule the batch
String cronExpression = '20 30 8 10 2 ?';
sch.schedule(cronExpression);
We call the schedule method by passing cron Expression which schedules the scheduler which in turn runs the batch.
Go through the link to know more about Scheduler and Cron Expression :
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm
Please let me know if you have any more questions.
Thanks
Shubham Jain