You need to sign in to do that
Don't have an account?
Anshuman Pratap Singh
This is a part of my code containig aggregate result,please help me in covering this in a test class
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
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
Thanks
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
Which id should i pass in Old_Budget_User__c field?
Thanks
Anshuman
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
i am getting error with User u=Userinfo.getUserId();
I am sending you test class,please look into it
Thanks
Anshuman
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?
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
I came up with a solution and test coverage is now above 90%.