You need to sign in to do that
Don't have an account?
Dave Berenato
Test Class for Count Function
This might be really simple, but I'm banging my head over this Test Class for an custom controller Apex Class.
A snippet of the Apex Class:
Is this close to the right markup for the TestClass? I'm getting so many "variable does not exist" errors that I can't even know if I'm headed in the right direction.
A snippet of the Apex Class:
public class GoalChart { public Integer getAryCalls(){return (Integer) [SELECT count() FROM Task WHERE skyplatform__Is_Call_Record__c = TRUE And CreatedDate = THIS_MONTH And CreatedById = '0056A000001IVgU'];} }
Is this close to the right markup for the TestClass? I'm getting so many "variable does not exist" errors that I can't even know if I'm headed in the right direction.
@isTest public class GoalChartTest{ GoalChart gc = new GoalChart(); static TestMethod TestAryCalls{ system.assert(0,gc.AryCalls) } }Any help would be greatly appreciated. Thank you.
All Answers
You have hard-coded createdById in the query. Even if you are able to successfully deploy the code, getArrayCalls() going to return 0 records(because production user ids will be different) unless this sandbox is full copy ( both prod and sand will have same ID)
As a best practice, you should never hardcode ids.
Atleast if you assume user name is the main key to avoid this issue or you are querying for the current logged in user, code can be changed like this :
IF by particular user name,
public class GoalChart { public Integer getAryCalls(){
List <User> lstUser = [ SELECT Id FROM User WHERE name = 'Nayana K'];
if(!lstUser.isEmpty())
return (Integer) [SELECT count() FROM Task WHERE skyplatform__Is_Call_Record__c = TRUE And CreatedDate = THIS_MONTH And CreatedById = lstUser[0].Id];
return 0;
} }
IF by logged in user,
public class GoalChart { public Integer getAryCalls(){return (Integer) [SELECT count() FROM Task WHERE skyplatform__Is_Call_Record__c = TRUE And CreatedDate = THIS_MONTH And CreatedById = userInfo.getUserId()];} }
1. Invalid constructor name system.assertEquals
2. Invalid constructor name gc.getAryCalls
I appreciate you staying to help me with this!