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
SriniSrini 

Need help for test class code coverage

Hi,

Can any one  please help me on Batch apex test class as mentioned below.

Batch Apex:

global class MasterAccountNewLogoDate implements Database.Batchable<AggregateResult>{

        /// get all Account Group by Ultimate_Parent_ID__c
        
        /* START Method */
        
        global Iterable<AggregateResult> start(database.batchablecontext BC){
              return (AccountsWithParentID ); 
        }
        
        List<AggregateResult> AccountsWithParentID = [Select Ultimate_Parent_ID__c, Min(SW_New_Landing_Date_Formula__c) NewLogo_StatusDate from Account where Ultimate_Parent_ID__c!=Null and SW_New_Landing_Date_Formula__c!=Null group by Ultimate_Parent_ID__c];

        
        /* EXECUTE Method */
        global void execute(Database.BatchableContext BC, List<AggregateResult> scope){        
            
                Map<id, String> AccountNewLogoStatus = new Map<id, String>();
                Map<id, date> AccountNewlogodate = new Map<id, date>();
                List<account> AccountToUpdate = new List<Account>();
                List<id> AccountId = new List<id>();
                String tempStatus='';
                
                ////loop through the Ultimate Parent Ids and create map of id and newlogo dates
                for (AggregateResult ParentIds : scope){
                        AccountId.add((ID)ParentIds.get('Ultimate_Parent_ID__c'));
                        AccountNewlogodate.put((ID)ParentIds.get('Ultimate_Parent_ID__c'),(Date)ParentIds.get('NewLogo_StatusDate')); 
                        }
               Account[] AllAccounts = [SELECT id,SW_Status_Formula__c,Ultimate_Parent_ID__c,GED_Master_Account_Status__c,GED_Master_Account_New_Logo_Date__c from Account where Ultimate_Parent_ID__c in :AccountId ];
                  
                                
                //create map of the different statuses 
                for(Account Acc: AllAccounts ){ 
                    if(AccountNewLogoStatus.get(Acc.Ultimate_Parent_ID__c)!=Null){                       
                        tempStatus = AccountNewLogoStatus.get(Acc.Ultimate_Parent_ID__c);
                        
                        if(Acc.SW_Status_Formula__c =='Prospect' ){
                            tempStatus = tempStatus + ',P';                        
                        }
                        if(Acc.SW_Status_Formula__c =='Customer (New Landing)' ){
                            tempStatus = tempStatus + ',N';
                        }
                        if(Acc.SW_Status_Formula__c =='Customer (Active)' ){
                            tempStatus = tempStatus + ',A';
                        }
                        if(Acc.SW_Status_Formula__c =='Customer (Dormant)' ){
                            tempStatus = tempStatus + ',D';
                        }
                        AccountNewLogoStatus.put(Acc.Ultimate_Parent_ID__c,tempStatus);
                        tempStatus ='';
                    }
                    else
                    {
                        if(Acc.SW_Status_Formula__c =='Prospect' ){
                            AccountNewLogoStatus.put(Acc.Ultimate_Parent_ID__c,'P');
                        }
                        if(Acc.SW_Status_Formula__c =='Customer (New Landing)'){
                            AccountNewLogoStatus.put(Acc.Ultimate_Parent_ID__c,'N');
                        }
                        if(Acc.SW_Status_Formula__c =='Customer (Active)'){
                            AccountNewLogoStatus.put(Acc.Ultimate_Parent_ID__c,'A');
                        }
                        if(Acc.SW_Status_Formula__c =='Customer (Dormant)'){
                            AccountNewLogoStatus.put(Acc.Ultimate_Parent_ID__c,'D');
                        }
                    }
                }
                
                //loop through to update the NL_status
                for (Account Acc: AllAccounts ){   
                    Acc.GED_Master_Account_New_Logo_Date__c =AccountNewlogodate.get(Acc.Ultimate_Parent_ID__c);
                     //Acc.GED_Master_Account_Status__c =AccountNewlogodate.ValueOf(Acc.Ultimate_Parent_ID__c);
                    if (AccountNewLogoStatus.get(Acc.Ultimate_Parent_ID__c)!=Null){
                        tempStatus=AccountNewLogoStatus.get(Acc.Ultimate_Parent_ID__c);
                        if(tempStatus.contains('N') && tempStatus.containsNone('A') && tempStatus.containsNone('D')){
                            Acc.GED_Master_Account_Status__c ='Customer (New Landing)';
                        }
                        if(tempStatus.contains('D') && tempStatus.containsNone('A') && tempStatus.containsNone('N')){
                            Acc.GED_Master_Account_Status__c ='Customer (Dormant)';
                        }
                        if(tempStatus.contains('P') && tempStatus.containsNone('A') && tempStatus.containsNone('D')){
                            Acc.GED_Master_Account_Status__c ='Prospect';
                        }
                        if(tempStatus.contains('A')){
                            Acc.GED_Master_Account_Status__c ='Customer (Active)';
                        }           
                    }
                    AccountToUpdate.add(Acc); 
                } 
            if(AccountToUpdate.size()>0){
                Update AccountToUpdate;
            }         
                  
            }//execute loop
            
        /* FINISH Method */
        global void finish(Database.BatchableContext info){
               
        }//global void finish loop
}//global class loop

Thanks
Best Answer chosen by Srini
N.M. SharmaN.M. Sharma
Hi Srini,

i have made some changes in your code please try this and let me know if the issue is still happning.

Thanks
global class MasterAccountNewLogoDate implements Database.Batchable<AggregateResult>{

        /// get all Account Group by Ultimate_Parent_ID__c
       /* START Method */
        
        global Iterable<AggregateResult> start(database.batchablecontext BC){
              return (AccountsWithParentID ); 
        }
        
        List<AggregateResult> AccountsWithParentID = [Select Ultimate_Parent_ID__c, Min(SW_New_Landing_Date_Formula__c) NewLogo_StatusDate from Account where Ultimate_Parent_ID__c!=Null and SW_New_Landing_Date_Formula__c!=Null group by Ultimate_Parent_ID__c];

        
        /* EXECUTE Method */
        global void execute(Database.BatchableContext BC, List<AggregateResult> scope){        
            
                Map<id, String> AccountNewLogoStatus = new Map<id, String>();
                Map<id, date> AccountNewlogodate = new Map<id, date>();
                List<account> AccountToUpdate = new List<Account>();
                String tempStatus='';
                
                //loop through the Ultimate Parent Ids and create map of id and newlogo dates
                for (AggregateResult ParentIds : scope){
                       AccountNewlogodate.put((ID)ParentIds.get('Ultimate_Parent_ID__c'),(Date)ParentIds.get('NewLogo_StatusDate')); 
				}
               Account[] AllAccounts = [SELECT id,SW_Status_Formula__c,Ultimate_Parent_ID__c,GED_Master_Account_Status__c,GED_Master_Account_New_Logo_Date__c from Account where Ultimate_Parent_ID__c in :AccountNewlogodate.getKeySet() and Ultimate_Parent_ID__c != NULL];
                  
                                
                //create map of the different statuses 
                for(Account Acc: AllAccounts ){ 
                    if(AccountNewLogoStatus.get(Acc.Ultimate_Parent_ID__c)!=Null){                       
                        tempStatus = AccountNewLogoStatus.get(Acc.Ultimate_Parent_ID__c);
                        if(Acc.SW_Status_Formula__c =='Prospect' ){
                            tempStatus = tempStatus + ',P';                        
                        }else if(Acc.SW_Status_Formula__c =='Customer (New Landing)' ){
                            tempStatus = tempStatus + ',N';
                        }else if(Acc.SW_Status_Formula__c =='Customer (Active)' ){
                            tempStatus = tempStatus + ',A';
                        }else if(Acc.SW_Status_Formula__c =='Customer (Dormant)' ){
                            tempStatus = tempStatus + ',D';
                        }
                        AccountNewLogoStatus.put(Acc.Ultimate_Parent_ID__c,tempStatus);
                        tempStatus ='';
                    }else{
                        if(Acc.SW_Status_Formula__c =='Prospect' ){
                            AccountNewLogoStatus.put(Acc.Ultimate_Parent_ID__c,'P');
                        }else if(Acc.SW_Status_Formula__c =='Customer (New Landing)'){
                            AccountNewLogoStatus.put(Acc.Ultimate_Parent_ID__c,'N');
                        }else if(Acc.SW_Status_Formula__c =='Customer (Active)'){
                            AccountNewLogoStatus.put(Acc.Ultimate_Parent_ID__c,'A');
                        }else if(Acc.SW_Status_Formula__c =='Customer (Dormant)'){
                            AccountNewLogoStatus.put(Acc.Ultimate_Parent_ID__c,'D');
                        }
                    }
                }
                
                //loop through to update the NL_status
                for (Account Acc: AllAccounts){   
                    Acc.GED_Master_Account_New_Logo_Date__c =AccountNewlogodate.get(Acc.Ultimate_Parent_ID__c);
                   if (AccountNewLogoStatus.get(Acc.Ultimate_Parent_ID__c)!=Null){
                        tempStatus=AccountNewLogoStatus.get(Acc.Ultimate_Parent_ID__c);
                        if(tempStatus.contains('N') && tempStatus.containsNone('A') && tempStatus.containsNone('D')){
                            Acc.GED_Master_Account_Status__c ='Customer (New Landing)';
                        }else if(tempStatus.contains('D') && tempStatus.containsNone('A') && tempStatus.containsNone('N')){
                            Acc.GED_Master_Account_Status__c ='Customer (Dormant)';
                        }else if(tempStatus.contains('P') && tempStatus.containsNone('A') && tempStatus.containsNone('D')){
                            Acc.GED_Master_Account_Status__c ='Prospect';
                        }else if(tempStatus.contains('A')){
                            Acc.GED_Master_Account_Status__c ='Customer (Active)';
                        }           
                    }
                    AccountToUpdate.add(Acc); 
                } 
            if(AccountToUpdate.size()>0){
                Update AccountToUpdate;
            }         
                  
            }//execute loop
            
        /* FINISH Method */
        global void finish(Database.BatchableContext info){
               
        }//global void finish loop
}//global class loop

 

All Answers

N.M. SharmaN.M. Sharma
Hi Srini,

i have made some changes in your code please try this and let me know if the issue is still happning.

Thanks
global class MasterAccountNewLogoDate implements Database.Batchable<AggregateResult>{

        /// get all Account Group by Ultimate_Parent_ID__c
       /* START Method */
        
        global Iterable<AggregateResult> start(database.batchablecontext BC){
              return (AccountsWithParentID ); 
        }
        
        List<AggregateResult> AccountsWithParentID = [Select Ultimate_Parent_ID__c, Min(SW_New_Landing_Date_Formula__c) NewLogo_StatusDate from Account where Ultimate_Parent_ID__c!=Null and SW_New_Landing_Date_Formula__c!=Null group by Ultimate_Parent_ID__c];

        
        /* EXECUTE Method */
        global void execute(Database.BatchableContext BC, List<AggregateResult> scope){        
            
                Map<id, String> AccountNewLogoStatus = new Map<id, String>();
                Map<id, date> AccountNewlogodate = new Map<id, date>();
                List<account> AccountToUpdate = new List<Account>();
                String tempStatus='';
                
                //loop through the Ultimate Parent Ids and create map of id and newlogo dates
                for (AggregateResult ParentIds : scope){
                       AccountNewlogodate.put((ID)ParentIds.get('Ultimate_Parent_ID__c'),(Date)ParentIds.get('NewLogo_StatusDate')); 
				}
               Account[] AllAccounts = [SELECT id,SW_Status_Formula__c,Ultimate_Parent_ID__c,GED_Master_Account_Status__c,GED_Master_Account_New_Logo_Date__c from Account where Ultimate_Parent_ID__c in :AccountNewlogodate.getKeySet() and Ultimate_Parent_ID__c != NULL];
                  
                                
                //create map of the different statuses 
                for(Account Acc: AllAccounts ){ 
                    if(AccountNewLogoStatus.get(Acc.Ultimate_Parent_ID__c)!=Null){                       
                        tempStatus = AccountNewLogoStatus.get(Acc.Ultimate_Parent_ID__c);
                        if(Acc.SW_Status_Formula__c =='Prospect' ){
                            tempStatus = tempStatus + ',P';                        
                        }else if(Acc.SW_Status_Formula__c =='Customer (New Landing)' ){
                            tempStatus = tempStatus + ',N';
                        }else if(Acc.SW_Status_Formula__c =='Customer (Active)' ){
                            tempStatus = tempStatus + ',A';
                        }else if(Acc.SW_Status_Formula__c =='Customer (Dormant)' ){
                            tempStatus = tempStatus + ',D';
                        }
                        AccountNewLogoStatus.put(Acc.Ultimate_Parent_ID__c,tempStatus);
                        tempStatus ='';
                    }else{
                        if(Acc.SW_Status_Formula__c =='Prospect' ){
                            AccountNewLogoStatus.put(Acc.Ultimate_Parent_ID__c,'P');
                        }else if(Acc.SW_Status_Formula__c =='Customer (New Landing)'){
                            AccountNewLogoStatus.put(Acc.Ultimate_Parent_ID__c,'N');
                        }else if(Acc.SW_Status_Formula__c =='Customer (Active)'){
                            AccountNewLogoStatus.put(Acc.Ultimate_Parent_ID__c,'A');
                        }else if(Acc.SW_Status_Formula__c =='Customer (Dormant)'){
                            AccountNewLogoStatus.put(Acc.Ultimate_Parent_ID__c,'D');
                        }
                    }
                }
                
                //loop through to update the NL_status
                for (Account Acc: AllAccounts){   
                    Acc.GED_Master_Account_New_Logo_Date__c =AccountNewlogodate.get(Acc.Ultimate_Parent_ID__c);
                   if (AccountNewLogoStatus.get(Acc.Ultimate_Parent_ID__c)!=Null){
                        tempStatus=AccountNewLogoStatus.get(Acc.Ultimate_Parent_ID__c);
                        if(tempStatus.contains('N') && tempStatus.containsNone('A') && tempStatus.containsNone('D')){
                            Acc.GED_Master_Account_Status__c ='Customer (New Landing)';
                        }else if(tempStatus.contains('D') && tempStatus.containsNone('A') && tempStatus.containsNone('N')){
                            Acc.GED_Master_Account_Status__c ='Customer (Dormant)';
                        }else if(tempStatus.contains('P') && tempStatus.containsNone('A') && tempStatus.containsNone('D')){
                            Acc.GED_Master_Account_Status__c ='Prospect';
                        }else if(tempStatus.contains('A')){
                            Acc.GED_Master_Account_Status__c ='Customer (Active)';
                        }           
                    }
                    AccountToUpdate.add(Acc); 
                } 
            if(AccountToUpdate.size()>0){
                Update AccountToUpdate;
            }         
                  
            }//execute loop
            
        /* FINISH Method */
        global void finish(Database.BatchableContext info){
               
        }//global void finish loop
}//global class loop

 
This was selected as the best answer
SriniSrini
Hi Sharma,

We are getting error like "Method does not exist or incorrect signature: [Map<Id,Date>].getKeySet() "

how we can acheive this.

thanks
N.M. SharmaN.M. Sharma
I am assuming that Ultimate_Parent_ID__c is a String field so make your map String , date.

Thanks
SriniSrini
Hi Sharma,

 We are using in the query section like : 
Account[] AllAccounts = [SELECT id,SW_Status_Formula__c,Ultimate_Parent_ID__c,GED_Master_Account_Status__c,GED_Master_Account_New_Logo_Date__c from Account where Ultimate_Parent_ID__c in : AccountNewLogoStatus.KeySet() and Ultimate_Parent_ID__c != NULL];

For the above changes batch was succefully completed.

Thanks for your great time. and also please help me the test class for the above calss,that shuld help for me.

Thank you so much !....



 
N.M. SharmaN.M. Sharma
Hi Srini,
I have put my comment for test class on this link:
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000kDMl

Please take a look on it 

Thanks