• Joe L. Thompson
  • NEWBIE
  • 0 Points
  • Member since 2023

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 4
    Replies
Hi All ,

can somebody help me on this please !?
I need to test the below piece of code where i have written this in catch block of some pqr class method
But i am strucked how to test it in anonymous window:
catch (exception ex)
mainclass.mainwrapper wrapp = new mainclass.mainwrapper();
                wrapp.excp= ex;
                wrapp.orgId = UserInfo.getOrganizationId();
                wrapp.winName = UserInfo.getOrganizationName();
                wrapp.cName = 'xyz class';
                wrapp.mName = 'abc method';
                ExceptionLog__c newExceptionLog = new ExceptionLog__c();
                newExceptionLog = mainclass.mno-method(wrapp);
                insert newExceptionLog;


Thanks in Advance !
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');
                     }
                     }