You need to sign in to do that
Don't have an account?
Michael Degn Jensen
Apex Class unit test get list of records + update records
Hi,
Amost there with unit testing of my first Aura Component, however I am stuck with testing of 2 functions. The first finds all the tasks assigned to current user and the second updates a task to Status=Completed.
First function:
I have done the Trailhead Apex Testing (https://trailhead.salesforce.com/content/learn/modules/apex_testing?trail_id=force_com_dev_beginner) but maybe I'm not getting it or it doesn't cover what I'm trying to test here.
Here is where I'm at in the first test:
Any help is really appreciated.
Thanks,
Michael
Amost there with unit testing of my first Aura Component, however I am stuck with testing of 2 functions. The first finds all the tasks assigned to current user and the second updates a task to Status=Completed.
First function:
@AuraEnabled public static List<Task> getTasks(Id oUser){ System.debug('oUser'+oUser); List<Task> taskList = [SELECT Id,Subject,Description,OwnerId,ActivityDate,Who.Name,Priority,Status,Type,AL_Assigned_To__c FROM Task WHERE OwnerId=:oUser AND ActivityDate <= NEXT_N_DAYS:3 AND Status != 'Completed' ORDER BY ActivityDate ASC LIMIT 7]; System.debug('taskList: ' + taskList); return taskList; }Second function:
@AuraEnabled public static void updateDetails(List<String> lstRecordId) { List<Task> lstUpdate = new List<Task>(); for(Task t : [SELECT Id, Status FROM Task WHERE Id IN : lstRecordId]){ t.Status = 'Completed'; // fields to update lstUpdate.add(t); } if(lstUpdate.size() > 0){ update lstUpdate; } }
I have done the Trailhead Apex Testing (https://trailhead.salesforce.com/content/learn/modules/apex_testing?trail_id=force_com_dev_beginner) but maybe I'm not getting it or it doesn't cover what I'm trying to test here.
Here is where I'm at in the first test:
@isTest static void testGetTasks() { Task taskt = new Task(Subject='Test Task', OwnerId=UserInfo.getUserId(), Status='Not Started'); insert taskt; }
Any help is really appreciated.
Thanks,
Michael
Try below code if it works :
Do let me know if it helps you and close your query by marking it as solved.
Thanks,
Santosh Kumar
All Answers
Then guessing that it might need the parameter Id oUser, so tried to add "UserInfo.getUserId()" to line 9:
But then getting this error instead "Static method cannot be referenced from a non static context: List<Task> ALTaskController.getTasks(Id)"
I know that there is something very basic about unit testing that I am missing here, but any pointer in the right direction is appreciated!
Thanks,
Michael
Try the following test class it may be helpful for you: I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
Thanks for your reply.
This code give med 2 errors:
Line 11: "Method does not exist or incorrect signature: void getTasks() from the type ALTaskController"
Line 16: "Method does not exist or incorrect signature: void updateDetails(Id) from the type ALTaskController" Also I think the 2 Apex Class functions are mixed together here (updateDetails and getTasks)?
Thanks,
Michael
Try below code if it works :
Do let me know if it helps you and close your query by marking it as solved.
Thanks,
Santosh Kumar
Thanks,
Michael
You don't call your function correctly. That's why this error occurs. So call your function just like this way: ClassName.FunctionName.
Try the following test class it may be helpful for you:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha