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
Wajeed KhanWajeed Khan 

Can anyone give test code coverage for below code please ,thanks in advance

public with sharing  class Call_Controller {
    public Call__c call{get;set;}
    public Id accountId {get;set;}
    public List<Account_Affiliation__c> accAf{get;Set;}
    public List<wrapperClass> wrapClassList{get;set;}
    public wrapperClass wc{get;set;}
    public Account acc{get;set;}
    Set<Id> ParentID = new Set<id>();
    Set<id> CallId= new Set<Id>();
    
    public Call_Controller(ApexPages.StandardController ctrl)
    {
        if(!test.isRunningTest())
            ctrl.addFields(new List<String>{'Account__c'});
        
        
        wrapClassList = new List<wrapperClass>();
        accountId =ApexPages.currentPage().getParameters().get('accId');
        accAf = new List<Account_Affiliation__c>();
        call = (Call__c)ctrl.getRecord();
        if(accountId==null)
        {
            accountId=call.Account__c;
        }
        else
        {
            call.Account__c = accountId; 
            
        }
        acc=[select ID,Name from Account where id=:accountId];
        
        accAf=[Select id,Status__c,To__r.name,From__c,To__c From Account_Affiliation__c 
               where From__c=:accountId and Status__c=true];
        
        Set<String> callName=new Set<String>();
        if(ctrl.getId()!=null)
        {
            try{
                List<Call__c> childCall = [select id,Account__r.Name,Call_Date__c,out_Of_Office__c,Comments__c,parent_call__c from call__c 
                                           where Parent_call__c=:call.Id];
                for(Call__c cc : childCall)
                {
                    wc = new wrapperClass(cc.Account__r.Name,cc.Id);
                    callName.add(cc.Account__r.Name);
                    wc.flag=true;
                    ParentID.add(cc.Account__c);
                    callId.add(cc.Id);
                    wrapClassList.add(wc);   
                }   
            }
            catch(Exception ex)
            {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,ex.getMessage()+' at line number '+ex.getLineNumber()));
            }
        }
        for(Account_Affiliation__c af:accAf)
        {
            if(!callName.contains(af.To__r.Name))
            {
                wc = new wrapperClass(af.To__r.Name,af.Id);
                wc.accWrap=af;
                wrapClassList.add(wc); 
            }
            
        }
    }
    
    
    public class wrapperClass
    {
        public boolean flag{get;set;}
        public Account_Affiliation__c accWrap{get;set;}
        public String accName{get;set;}
        public Id Cid{get;set;}
        
        public wrapperClass(String Name,id Cid)
        {
            this.Cid=Cid;
            accName=Name; 
        }
    }
    
    //Record Creation for Related Records on Save
    public void createRelRecordOnSave()
    {
        for(wrapperClass wc:wrapClassList)
        {
            if(wc.flag==true)
            {
                if(!callId.contains(wc.Cid))
                {
                    Call__c relCall= new Call__c();
                    relCall.Account__c=wc.accWrap.To__c;
                    relCall.Out_of_office__c=call.Out_of_office__c;
                    relcall.Parent_Call__c=call.id;
                    relCall.Call_Date__c=call.Call_Date__c;
                    relCall.Comments__c=call.Comments__c;
                    relCall.Status__c='Draft';
                    insert relCall;
                }
                else
                {
                    Call__c c1=[select id,Account__c,Out_of_office__c,Call_Date__c,Comments__c from Call__c 
                                where Account__c=:wc.accWrap.To__c];
                    c1.Call_Date__c=call.Call_Date__c;
                    c1.Status__c='Draft';
                    c1.Comments__c=call.Comments__c;
                    c1.Out_of_office__c=call.Out_of_office__c;
                    update c1;
                    
                }
            }
        }
    }
    
    public void clear()
    {
        call.Comments__c=null;
    }
    
    
    
    //Record Creation for Related Records on Submit
    public void createRelRecordOnSubmit()
    {
        for(wrapperClass wc:wrapClassList)
        {
            if(wc.flag==true )
            {
                if(!callId.contains(wc.Cid))
                {
                    Call__c relCall= new Call__c();
                    relCall.Account__c=wc.accWrap.To__c;
                    relCall.Out_of_office__c=call.Out_of_office__c;
                    relcall.Parent_Call__c=call.id;
                    relCall.Call_Date__c=call.Call_Date__c;
                    relCall.Comments__c=call.Comments__c;
                    relCall.Status__c='Submitted';
                    insert relCall;
                }
                else
                {
                    Call__c c1=[select id,Account__c,Out_of_office__c,Call_Date__c,Comments__c from Call__c 
                                where Id = :wc.Cid];
                    c1.Call_Date__c=call.Call_Date__c;
                    c1.Status__c='Submitted';
                    c1.Comments__c=call.Comments__c;
                    c1.Out_of_office__c=call.Out_of_office__c;
                    update c1;
                }
            }
        }
    }
    //Save Button 
    public PageReference Save()
    {   
        if(call.Call_Date__c>System.today())
        {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'You Cannot Enter A future Date'));
            return null;
        }
        else
        {
            try{
                call.Status__c='Draft';
                upsert call;
                createRelRecordOnSave();
                
                PageReference reRend = new PageReference('/'+AccountID);
                reRend.setRedirect(true);
                return reRend;
            }
            catch(Exception ex)
            {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,ex.getMessage()+' at line number '+ex.getLineNumber()));
            }
        }
        return null;
        
    }
    
    public PageReference cancel()
    {
        PageReference reRend = new PageReference('/'+AccountID);
        reRend.setRedirect(true);
        return reRend;
    }
    
    //Submit Button
    public PageReference Submit()
    {      
        if(call.Call_Date__c>system.today())
        {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'You Cannot Enter A future Date'));
        }
        try{ 
            call.Status__c='Submitted';
            upsert call;
            createRelRecordOnSubmit();
            PageReference reRend = new PageReference('/'+AccountID);
            reRend.setRedirect(true);
            return reRend;
            
        }
        catch(Exception ex)
        {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,ex.getMessage()+' at line number '+ex.getLineNumber()));
        }
        return null;
    }  
    
}

 
Shravan Kumar 71Shravan Kumar 71
Hi Wajeed,

The below two blog should help you to write the test class :

http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html
http://amitsalesforce.blogspot.com/search/label/Test%20Class

Let us know if you still face any further issues.

Thanks,
Shravan
Wajeed KhanWajeed Khan
I had alredy written test code but its of 68% code coverage i want 85% of code coverage can you help me to get 85%



@isTest
private class Call_ControllerTest 
{   
    public static testMethod void testCallController()
    {  
        Test.setCurrentPageReference(new PageReference('Page.Accounts'));
        Account acc = new Account();
        acc.Name='voltum';
        acc.BillingStreet='Santhom High Road';
        acc.BillingCity='Banglore';
        acc.BillingState='Karnataka';
        acc.BillingCountry='India';
        insert acc;
        
        Account acc1 = new Account();
        acc1.Name='Testing';
        acc1.BillingStreet='Santhom High Road';
        acc1.BillingCity='Banglore';
        acc1.BillingState='Karnataka';
        acc1.BillingCountry='India';
        insert acc1;
        Account_Affiliation__c acf= new Account_Affiliation__c();
        acf.From__c=acc.Id;
        acf.To__c=acc1.Id;
        acf.Status__c=true;
        insert acf;
        
        Call__c cl = new Call__c();
        ApexPages.StandardController std=new Apexpages.StandardController(cl);
        
        
        cl.Account__c=acc.Id;
        cl.Call_Date__c=System.today()-10;
        cl.Out_of_office__c='Yes';
        cl.Comments__c='TestingComments';
        cl.Status__c='Draft';
        insert cl;
           
       /** Call__c cl1 = new Call__c();
        cl1.Account__c=acc.Id;
        cl1.Call_Date__c=System.today()+10;
        cl1.Out_of_office__c='Yes';
        cl1.Comments__c='TestingComments';
        cl1.Status__c='Draft';
        insert cl1;**/
        

        System.currentPageReference().getParameters().put('accId',acc.Id);
        Call_Controller call= new Call_Controller(std); 
        
        
        call.wrapClassList[0].flag=true; 
        
        call.Save();
        call.submit();
        call.cancel();
        call.clear();
     
       
    }
    
}

 
Raj VakatiRaj Vakati
Can u give me what lines are not covering