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
CooldayCoolday 

Help me write the test class for this Apex class.

Apex class 

@RestResource(urlMapping='/v1/Task/*')
global class TaskCreation {
    global class Result {
        webservice Integer returnCode;
        webservice String message='';
    }
     @HttpPost
     global static Result CreationLeadTask() {
        Result res=new Result();
        Map<String, Object> requestBody = (Map<String, Object>)JSON.deserializeUntyped(RestContext.request.requestBody.toString());
        Map < String, Object > mSet = (Map < String, Object > ) requestBody.get('taskDetails');
        String ID = (String) mSet.get('cusID'); 
        String assignedTo = (String) mSet.get('assignedTo');
        String taskName = (String) mSet.get('taskName');
        String priority = (String) mSet.get('priority');
        String activityDate = (String) mSet.get('activityDate');
        Date newDate = activityDate != '' && activityDate != null ? Date.valueOf(activityDate) : null;
         
        List<Lead> LeadsToBeUpdated = new List<Lead>();
        if (String.isEmpty(ID)) {
            res.returnCode = 400;
            res.message='Required';
            return res;
        } 
            List<Lead> Leadlist =[SELECT id,Lead.OwnerId, RecordType.DeveloperName FROM Lead where Id = :ID AND RecordType.DeveloperName = 'Person'];
            if(Leadlist == null || Leadlist.size() == 0){
                res.returnCode = 401;
                res.message = 'No Lead found with this Id';
                return res;
            }
                List<Task> Tasklist = new List<Task>();
                Task Ta = new Task();
                Ta.OwnerId= assignedTo != '' && assignedTo != null ? assignedTo : Leadlist[0].OwnerId;
                Ta.Status = 'New';
                Ta.Subject= taskName;
                Ta.Priority = priority != '' && priority != null ? priority : 'Normal';
                Ta.WhoId = Leadlist[0].id;
                Ta.ActivityDate =newDate;
                Tasklist.add(Ta);
    
        if (!Tasklist.isEmpty()) {
            insert Tasklist ;
            res.returnCode = 201;
            res.recordId = Tasklist[0].id;
            res.message='Task creation was successful.';
        }
        if (Leadlist.size() != 0 && Ta.Subject == 'TASK CREATION') {
            System.debug('123');
                Lead LeadUpdate = new Lead(Id = Leadlist[0].Id); 
                LeadUpdate.update = 'Yes';
                LeadUpdate.add(LeadUpdate);
        }
        Update LeadUpdate;
        return res;
    }
}
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Are you able to save the above apex class. I am getting 4 errors while saving the Apex class itself. Can you recheck and confirm on it.

Thanks,
 
CooldayCoolday
Thanks Sai Praveen but I wrote the Test class and Yes I was able to save the Apex class without any errors.