You need to sign in to do that
Don't have an account?
himanshu 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
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'); } }
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: