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
Ashu sharma 38Ashu sharma 38 

Test class for Sobject apex

I am getting 41% code coverage in my apex controller.

public class ContactTaskOnApplication {
    public static List<Task> getContactTask(Id recordId,String contactAPIName){
        if(recordId==null || contactAPIName==null){
            return null;     
        String sObjName = recordId.getSObjectType().getDescribe().getName();
        string query='SELECT Id, ' +contactAPIName+ ' FROM '+sObjName+ ' WHERE Id=:recordId';
        List<sObject> sObjectList=database.query(query);
        system.debug('sObjectList ' +sObjectList);
        if (!sObjectList.isEmpty()){
            id contactId=(Id)sObjectList[0].get(contactAPIName);
            List<Task> tsk=[SELECT Id,Subject,Priority,ActivityDate,Due_Date__c,
                            whoId FROM task WHERE whoId=:contactId];
            system.debug('tsk '+tsk);
            return tsk;
        return null;

Test class
public class TestContactTaskOnApplication {
    public static Account partnerAccount;
    public static Contact studentContact;
    public static Application__c application;
    public static RFI__c rfi;
    public static Task task;
    static testmethod void testContactTaskOnApplicationMethod() {
            Id idRecordId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Educational_Institution').getRecordTypeId();
            //Insert Account
            partnerAccount = new Account(Name='Gwendwyr Test', RecordTypeId=idRecordId);
            insert partnerAccount;
            //Insert Contact
            studentContact = new Contact(LastName='Someguy', firstname='Someguy' , email='',hed__Citizenship__c='Canada');  
            insert studentContact;
            //Insert Application
            application=new Application__c(,;
            insert application;
               //Insert RFI
                rfi= new RFI__c (Email__c='',Last_Name__c='Test RFI',First_Name__c='Test RFI', );
                insert rfi;
            //Insert Task
            task=new Task(Subject='Call',,,Priority ='Normal',;
            insert task;
            //, application.Contact__c
        catch(exception e){
ShirishaShirisha (Salesforce Developers) 
Hi Ashu sharma,


Please find the sample code in the below document which might help you in increasing the code coverage:

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri