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
Anshuman Pratap SinghAnshuman Pratap Singh 

how to cover aggregate sobject in test class?

public List<AggregateResult> inBudgetList {get;set;}


inBudgetList = [SELECT  
                            Budget_User__r.Name inAEName,
                            Budget_User__c inAeId,
                            Advertiser__r.Name account, 
                            Sum(Amount__c) inBudAmt,
                            Broadcast_Month_Date__c effectiveDate
                            FROM Sales_Budget__c
                            WHERE Old_Budget_User__c = :selectedUserId
                            GROUP BY Budget_User__r.Name, Budget_User__c, Advertiser__r.Name, Broadcast_Month_Date__c
                            ORDER BY Budget_User__r.Name
                           ];







for(AggregateResult inBudItem: inBudgetList){
                
                transferObj.transferUser = (String)inBudItem.get('inAEName');	    			    		
                transferObj.account = (String)inBudItem.get('account');
                transferObj.transferIn = (Decimal)inBudItem.get('inBudAmt');
                transferObj.transferEffectiveDate = ((Datetime)inBudItem.get('effectiveDate')).format('MMMMM -YYYY');
                
                transferLogs.add(transferObj);
                transferObj = new transferLogsWrapper();

This is a part of my code containig aggregate result,please help me in covering this in a test class
KumarRajaBangari14KumarRajaBangari14

Hi Anshuman,

Just insert some date into the Sales_Budget__c sObject and call the page from the test class, since the aggregate result is looking for records from that tables, it should get covered automatically.

Regards,
Kumar

Anshuman Pratap SinghAnshuman Pratap Singh
Sales_Budget__c sb = new Sales_Budget__c();
        sb.advertiser__c = objAcc.id;
        sb.Budget_user__c = u.id;
        sb.HR_Manager__c = u.id;
        sb.Old_Budget_User__c = u.id;
        sb.Amount__c=999;
        sb.Commodity__c='Auto Aftermarket';
        sb.Product_Line__c='Linear/News/Sports';
        sb.Broadcast_Month_Date__c = System.today();
        sb.External_Id__c = '007'+u.id+System.today();
        insert sb;
Hi raja ,i have inserted data as mentioned above,but still its not getting covered.Please help me with this

Thanks
Anshuman
 
KumarRajaBangari14KumarRajaBangari14
Anshuman,

Please debug the list inBudgetList in the class and see if the test data is being passed to the class. If it doesn't, adjust the test data to meet the where condition in the class.

Probably the problem must be with selectedUserId on Old_Budget_User__c field.

Regards,
Kumar
Anshuman Pratap SinghAnshuman Pratap Singh
Hi kumar,

Which id should i pass in Old_Budget_User__c field?

Thanks
Anshuman
KumarRajaBangari14KumarRajaBangari14
Whatever the selectedUserId variable contains.. Can you provide the snippet that is assigning a value to selectedUserId.
Anshuman Pratap SinghAnshuman Pratap Singh
Snippet of code:-
public String pageMsg{get;set;}
    public List<User> aeList {get;set;}
    public User currentUser{get;set;}
    public List<transferLogsWrapper> transferLogs{get;set;}
    public List<transferDetailsWrapper> transferDetails{get;set;}
    
    public List<AggregateResult> inBudgetList {get;set;}
    public List<AggregateResult> outBudgetList {get;set;}
    public List<AggregateResult> inProductLineList {get;set;}
    public List<AggregateResult> outProductLineList {get;set;}
    public List<String> ProductLineList {get;set;}
    public final Integer currentYear = Date.Today().Year();



1.String selectedUserId = ApexPages.currentPage().getParameters().get('selUserId');

2.inBudgetList = [SELECT  
                            Budget_User__r.Name inAEName,
                            Budget_User__c inAeId,
                            Advertiser__r.Name account,
                            Sum(Amount__c) inBudAmt,
                            Broadcast_Month_Date__c effectiveDate
                            FROM Sales_Budget__c
                            WHERE Old_Budget_User__c = :selectedUserId
                                  AND CALENDAR_YEAR(Broadcast_Month_Date__c) = :currentYear
                            GROUP BY Budget_User__r.Name, Budget_User__c, Advertiser__r.Name, Broadcast_Month_Date__c
                            ORDER BY Budget_User__r.Name
                            ];


3. for(AggregateResult inBudItem: inBudgetList){
                
                transferObj.transferUser = (String)inBudItem.get('inAEName');                                
                transferObj.account = (String)inBudItem.get('account');
                transferObj.transferIn = (Decimal)inBudItem.get('inBudAmt');
                transferObj.transferEffectiveDate = ((Datetime)inBudItem.get('effectiveDate')).format('MMMMM -YYYY');
                
                transferLogs.add(transferObj);
                transferObj = new transferLogsWrapper();
            }


Snippet of test class given below:

Sales_Budget__c sb = new Sales_Budget__c();
sb.advertiser__c = objAcc.id;
sb.Budget_user__c = u.id; sb.HR_Manager__c = u.id;
sb.Old_Budget_User__c = u.id; sb.Amount__c=999;
sb.Commodity__c='Auto Aftermarket';
sb.Product_Line__c='Linear/News/Sports';
sb.Broadcast_Month_Date__c = System.today();
sb.External_Id__c = '007'+u.id+System.today();
insert sb;


Hi kumar ,these are snippet of code.Thanks for showing your concern to this problem
 
KumarRajaBangari14KumarRajaBangari14
Since the Line#1 is trying to get the selUserId from the URL parameters, pass it onto the page via the test class. See below. You will need to add this code before even instantiating your page.
 
//get the logged in user id and pass it as a parameter in the URL onto the variable selUserId in your test class
User u = UserInfo.getUserId();
ApexPages.currentPage().getParameters().put('selUserId',u.Id);
Anshuman Pratap SinghAnshuman Pratap Singh
Hi Kumar,
i am getting error with User u=Userinfo.getUserId();

I am sending you test class,please look into it

Thanks
Anshuman
@istest
public class TrackCompensationPlanDetailsContTest{
    public static testmethod void test1(){
        Profile pro = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        
        
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
                          EmailEncodingKey='UTF-8', LastName='Testing 001', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US', ProfileId = pro.Id, 
                          TimeZoneSidKey='America/Los_Angeles', UserName='standarduser124@testorg.com');
        
        insert u;
        
        User u1 = UserInfo.getUserId();
        ApexPages.currentPage().getParameters().put('00541000002Xx6z', u1.id);
        PageReference pgRef = Page.TrackCompensationPlanDetails2;
        Test.setCurrentPage(pgRef); 
        
        Account objAcc = new Account ();
        objAcc.Name = 'Test Account Owner Change';
        objAcc.BillingStreet = 'Valley';
        objAcc.BillingCity = 'dallas';
        objAcc.BillingState = 'TX';
        objAcc.BillingCountry = 'us';
        objAcc.BillingPostalCode = '75432';
        objAcc.Skip_Address_Validation__c = true;
        objAcc.Last_Client_Schedule_Date__c = Date.today();
        objAcc.Primary_Commodity__c = 'Alcoholic Beverages';
        
       
        
       
        
        Sales_Budget__c sb = new Sales_Budget__c();
        sb.advertiser__c = objAcc.id;
        sb.Budget_user__c = u.id;
        sb.HR_Manager__c = u.id;
        sb.Old_Budget_User__c = u1.id;
        sb.Amount__c=999;
        sb.Commodity__c='Auto Aftermarket';
        sb.Product_Line__c='Linear/News/Sports';
        sb.Broadcast_Month_Date__c = System.today();
        sb.External_Id__c = '007'+u.id+System.today();
        insert sb;
       
        
        User o=new user();
        ApexPages.StandardController stcon=new ApexPages.StandardController(o);
        TrackCompensationPlanDetails2Controller obj=new TrackCompensationPlanDetails2Controller();
        
        
        String v='00530000000ibYc';
        TrackCompensationPlanDetails2Controller.ConfirmAEList(v);
        
    }
    
    public static testmethod void test2(){
        
        User o=new user();
        ApexPages.StandardController stcon=new ApexPages.StandardController(o);
        TrackCompensationPlanDetails2Controller obj=new TrackCompensationPlanDetails2Controller();
        
        
        Profile pro = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        
        
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
                          EmailEncodingKey='UTF-8', LastName='Testing 001', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US', ProfileId = pro.Id, 
                          TimeZoneSidKey='America/Los_Angeles', UserName='standarduser124@testorg.com');
        
        insert u;
        
        
        User u1 = UserInfo.getUserId(); 
        ApexPages.currentPage().getParameters().put('00541000002Xx6z', u1.id);
        PageReference pgRef = Page.TrackCompensationPlanDetails2; 
        Test.setCurrentPage(pgRef);
        
       
        
        
        Account objAcc = new Account ();
        objAcc.Name = 'Test Account Owner Change';
        objAcc.BillingStreet = 'Valley';
        objAcc.BillingCity = 'dallas';
        objAcc.BillingState = 'TX';
        objAcc.BillingCountry = 'us';
        objAcc.BillingPostalCode = '75432';
        objAcc.Skip_Address_Validation__c = true;
        objAcc.Last_Client_Schedule_Date__c = Date.today();
        objAcc.Primary_Commodity__c = 'Alcoholic Beverages';
        
          
        
        Sales_Budget__c sb = new Sales_Budget__c();
        sb.advertiser__c = objacc.id;
        sb.Budget_user__c = u.id;
        sb.HR_Manager__c = u.id;
        sb.Old_Budget_User__c = u1.id;
        sb.Amount__c=0;
        sb.Commodity__c='Auto Aftermarket';
        sb.Product_Line__c='Linear/News/Sports';
        sb.Broadcast_Month_Date__c =System.today(); 
        sb.External_Id__c = '007'+u.id+System.today();
        insert sb;
        //update sb;
        
        
        
        
        
        
        
        String v='00530000000ibYc';
        TrackCompensationPlanDetails2Controller.ConfirmAEList(v);
        
        
    }
    
   
    
}

 
Anshuman Pratap SinghAnshuman Pratap Singh
Error is illegal assignment from string to user
KumarRajaBangari14KumarRajaBangari14
You got it wrong. Try this.
 
@istest
public class TrackCompensationPlanDetailsContTest{
    public static testmethod void test1(){
        Profile pro = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        
        
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
                          EmailEncodingKey='UTF-8', LastName='Testing 001', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US', ProfileId = pro.Id, 
                          TimeZoneSidKey='America/Los_Angeles', UserName='standarduser124@testorg.com');
        
        insert u;
        
        ApexPages.currentPage().getParameters().put('selUserId', u.id);
        PageReference pgRef = Page.TrackCompensationPlanDetails2;
        Test.setCurrentPage(pgRef); 
        
        Account objAcc = new Account ();
        objAcc.Name = 'Test Account Owner Change';
        objAcc.BillingStreet = 'Valley';
        objAcc.BillingCity = 'dallas';
        objAcc.BillingState = 'TX';
        objAcc.BillingCountry = 'us';
        objAcc.BillingPostalCode = '75432';
        objAcc.Skip_Address_Validation__c = true;
        objAcc.Last_Client_Schedule_Date__c = Date.today();
        objAcc.Primary_Commodity__c = 'Alcoholic Beverages';
        
        Sales_Budget__c sb = new Sales_Budget__c();
        sb.advertiser__c = objAcc.id;
        sb.Budget_user__c = u.id;
        sb.HR_Manager__c = u.id;
        sb.Old_Budget_User__c = u.id;
        sb.Amount__c=999;
        sb.Commodity__c='Auto Aftermarket';
        sb.Product_Line__c='Linear/News/Sports';
        sb.Broadcast_Month_Date__c = System.today();
        sb.External_Id__c = '007'+u.id+System.today();
        insert sb;
        
        
        User o=new user();
        ApexPages.StandardController stcon=new ApexPages.StandardController(o);
        TrackCompensationPlanDetails2Controller obj=new TrackCompensationPlanDetails2Controller();
        
        
        String v='00530000000ibYc';
        TrackCompensationPlanDetails2Controller.ConfirmAEList(v);
        
    }
    
    public static testmethod void test2(){
        
        User o=new user();
        ApexPages.StandardController stcon=new ApexPages.StandardController(o);
        TrackCompensationPlanDetails2Controller obj=new TrackCompensationPlanDetails2Controller();
        
        
        Profile pro = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        
        
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
                          EmailEncodingKey='UTF-8', LastName='Testing 001', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US', ProfileId = pro.Id, 
                          TimeZoneSidKey='America/Los_Angeles', UserName='standarduser124@testorg.com');
        
        insert u;
        ApexPages.currentPage().getParameters().put('selUserId', u.id);
        PageReference pgRef = Page.TrackCompensationPlanDetails2; 
        Test.setCurrentPage(pgRef);
        
        Account objAcc = new Account ();
        objAcc.Name = 'Test Account Owner Change';
        objAcc.BillingStreet = 'Valley';
        objAcc.BillingCity = 'dallas';
        objAcc.BillingState = 'TX';
        objAcc.BillingCountry = 'us';
        objAcc.BillingPostalCode = '75432';
        objAcc.Skip_Address_Validation__c = true;
        objAcc.Last_Client_Schedule_Date__c = Date.today();
        objAcc.Primary_Commodity__c = 'Alcoholic Beverages';
        
        Sales_Budget__c sb = new Sales_Budget__c();
        sb.advertiser__c = objacc.id;
        sb.Budget_user__c = u.id;
        sb.HR_Manager__c = u.id;
        sb.Old_Budget_User__c = u.id;
        sb.Amount__c=0;
        sb.Commodity__c='Auto Aftermarket';
        sb.Product_Line__c='Linear/News/Sports';
        sb.Broadcast_Month_Date__c =System.today(); 
        sb.External_Id__c = '007'+u.id+System.today();
        insert sb;
        //update sb;
        String v='00530000000ibYc';
        TrackCompensationPlanDetails2Controller.ConfirmAEList(v);
    }
}

 
Anshuman Pratap SinghAnshuman Pratap Singh
Hi kumar ,still the code coverage is same ,
2.inBudgetList = [SELECT  
                            Budget_User__r.Name inAEName,
                            Budget_User__c inAeId,
                            Advertiser__r.Name account,
                            Sum(Amount__c) inBudAmt,
                            Broadcast_Month_Date__c effectiveDate
                            FROM Sales_Budget__c
                            WHERE Old_Budget_User__c = :selectedUserId
                                  AND CALENDAR_YEAR(Broadcast_Month_Date__c) = :currentYear
                            GROUP BY Budget_User__r.Name, Budget_User__c, Advertiser__r.Name, Broadcast_Month_Date__c
                            ORDER BY Budget_User__r.Name
                            ];

In above query ,what about the calendar year,this should also be kept in mind?



Sales_Budget__c sb = new Sales_Budget__c();
sb.advertiser__c = objAcc.id;
sb.Budget_user__c = u.id; sb.HR_Manager__c = u.id;
sb.Old_Budget_User__c = u.id; sb.Amount__c=999;
sb.Commodity__c='Auto Aftermarket';
sb.Product_Line__c='Linear/News/Sports';
sb.Broadcast_Month_Date__c = System.today();
sb.External_Id__c = '007'+u.id+System.today();
insert sb;

I have to modify data in above code snippet of test class?
 
KumarRajaBangari14KumarRajaBangari14
Hi Anshuman,

You need to make changes to the data in test class. Both the fields Old_Budget_User__c and Broadcast_Month_Date__c in the where clause has to be kept in mind.

Please make sure to pass the data from test class just the way the actual class was expecting in the where clause, it should get covered. You may add debug statements to check if the data is being queried/passed or not and change the test data accordingly.

Regards,
Kumar
Anshuman Pratap SinghAnshuman Pratap Singh
Thanks Kumar,
I came up with a solution and test coverage is now above 90%.