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
himanshu huske 7himanshu huske 7 

count value should get incremented across each batch

Account_Asset_Role__c field is unique, we have integer count, while bulk updation if duplicate value is found on Account_Asset_Role__c field, feild will be updated as (duplicate + count) duplicate 1, duplicate 2..... when the new batch starts count again comes to 1
integer count should always maintain its value eg: if count is 2 in first batch than in second batch it should start with 3
please give a solution on this
global class BatchAccountAssetRoleConsolidation implements Database.Batchable<sObject>, Database.Stateful {
    private Map<String, Integer> accountAssetRoleCountMap = new Map<String, Integer>();
    private Integer successCount = 0;
    private Integer errorCount = 0;
    Integer count = 0;
    
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'Select id, Account__c, Contact__r.AccountId, Asset__c, Role__c, Account_Asset_Role__c from Account_Asset_Relationship__c';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Account_Asset_Relationship__c> aarLst) {
        Set<String> uniqueValues = new Set<String>();
        
        for(Account_Asset_Relationship__c aar : aarLst) {
            String key;
       
            if (aar.Account__c != null && aar.Role__c != null) {
                key = ((String) aar.Account__c + (String) aar.Asset__c + aar.Role__c);
            } 
            else if (aar.Account__c == null && aar.Role__c != null && aar.Contact__c != null) {
                key = ((String) aar.Contact__r.AccountId + (String) aar.Asset__c + aar.Role__c);
                aar.Account__c = aar.Contact__r.AccountId;
            }
            else if (aar.Account__c == null && aar.Role__c == null && aar.Contact__c != null) {
                key = ((String) aar.Contact__r.AccountId + (String) aar.Asset__c);
                aar.Account__c = aar.Contact__r.AccountId;
            }
            else if (aar.Account__c != null && aar.Role__c != null && aar.Contact__c != null) {
                key = ((String) aar.Account__c + (String) aar.Asset__c + aar.Role__c);
            }
            else if (aar.Account__c != null && aar.Contact__c != null && aar.Role__c == null) {
                key = ((String) aar.Contact__r.AccountId + (String) aar.Asset__c);             
            }
            else {
                continue;
            }
            
            if (accountAssetRoleCountMap.containsKey(key)) {
                count = accountAssetRoleCountMap.get(key);
                while(uniqueValues.contains('duplicate ' + count)){
                    count++;
                }
                aar.Account_Asset_Role__c = 'duplicate ' + count;
                accountAssetRoleCountMap.put(key, count + 1);
                uniqueValues.add('duplicate ' + count);
            }
            else {
                accountAssetRoleCountMap.put(key, 1);
                aar.Account_Asset_Role__c = key;
            }
        }
        
        try { 
            List<Database.SaveResult> saveResults = Database.update(aarLst, false);
            for(Database.SaveResult sr : saveResults) {
                if (sr.isSuccess()) {
                    successCount++;
                } else {
                    errorCount++;
                }
            }
        } catch(Exception e) {
            System.debug(e);
            errorCount += aarLst.size();
        }
    }
 
    global void finish(Database.BatchableContext BC) {
        // execute any post-processing operations like sending email
        System.debug('Batch finished with ' + successCount + ' successful records and ' + errorCount + ' error');
                     }
                     }

 
Joe L. ThompsonJoe L. Thompson
In the current implementation, the BatchAccountAssetRoleConsolidation class updates the Account_Asset_Role__c field for each Account_Asset_Relationship__c record based on a combination of the Account__c, Asset__c, and Role__c fields. If a duplicate value is found in the Account_Asset_Role__c field, the current implementation adds a numeric suffix to the value (e.g., "duplicate 1", "duplicate 2", etc.) to make it unique.
To ensure that the integer count value is incremented properly across batches, you can update the implementation to retrieve the maximum integer count value for each unique key in the accountAssetRoleCountMap map, and use that value to start the count for the next batch.
Here's an updated implementation that demonstrates this approach:
 
global class BatchAccountAssetRoleConsolidation implements Database.Batchable<sObject>, Database.Stateful {
    private Map<String, Integer> accountAssetRoleCountMap = new Map<String, Integer>();
    private Integer successCount = 0;
    private Integer errorCount = 0;
    private Integer maxCount = 0;
    
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'Select id, Account__c, Contact__r.AccountId, Asset__c, Role__c, Account_Asset_Role__c from Account_Asset_Relationship__c';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Account_Asset_Relationship__c> aarLst) {
        Set<String> uniqueValues = new Set<String>();
        
        for(Account_Asset_Relationship__c aar : aarLst) {
            String key;
       
            if (aar.Account__c != null && aar.Role__c != null) {
                key = ((String) aar.Account__c + (String) aar.Asset__c + aar.Role__c);
            } 
            else if (aar.Account__c == null && aar.Role__c != null && aar.Contact__c != null) {
                key = ((String) aar.Contact__r.AccountId + (String) aar.Asset__c + aar.Role__c);
                aar.Account__c = aar.Contact__r.AccountId;
            }
            else if (aar.Account__c == null && aar.Role__c == null && aar.Contact__c != null) {
                key = ((String) aar.Contact__r.AccountId + (String) aar.Asset__c);
                aar.Account__c = aar.Contact__r.AccountId;
            }
            else if (aar.Account__c != null && aar.Role__c != null && aar.Contact__c != null) {
                key = ((String) aar.Account__c + (String) aar.Asset__c + aar.Role__c);
            }
            else if (aar.Account__c != null && aar.Contact__c != null && aar.Role__c == null) {
                key = ((String) aar.Contact__r.AccountId + (String) aar.Asset__c);             
            }
            else {
                continue;
            }
            
            if (accountAssetRoleCountMap.containsKey(key)) {
                maxCount = accountAssetRoleCountMap.get(key);
                while(uniqueValues.contains('duplicate ' + maxCount)){
                    maxCount++;
                }
                aar.Account_Asset_Role__c = 'duplicate ' + maxCount;
                uniqueValues.add('duplicate ' + maxCount);
                accountAssetRoleCountMap.put(key, maxCount +

 
Bryan Leaman 6Bryan Leaman 6
Please, someone, correct me if I'm wrong, but I've always found that variables I need to update with each batch need to be either public or global.