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
TanmoyBarikTanmoyBarik 

Test Class for this Apex Class ..

Hi All,

 

I am new to testclasses i dont know how to write test classes Properly ...but i am tried but i am geting 63% code coverage only.. but i need atleast 90% can any one help this please ??

 

Here is my class

 

public with sharing class CallController{

    public Boolean callDetailsFlag{set;get;}
    public Boolean editFlag{set;get;}
    public Account account{set;get;}
    public CallDetail__c callDetail{set;get;}
    public String recordTypeName{set;get;}
    public String action{set;get;}
    public Boolean planEditFlag{set;get;}
    public List<WrapperClass> productList{get;set;}
    private ApexPages.StandardController stdController;
    private Call__c call;
    private String retURL;
    private String accId;
    private String callId;
    private String recordTypeId;
    private String oldProductsChoosen;
    public String selectedProduct{set;get;}
        
    public CallController(ApexPages.StandardController stdController){
        
        system.debug('--- CallController :: CallController() ---');
        this.call = (Call__c)stdController.getRecord();
        accId = ApexPages.currentPage().getParameters().get('accID');//from Account Detail Page
        retURL = ApexPages.currentPage().getParameters().get('retURL');//from overriden standard buttons
        callId = ApexPages.currentPage().getParameters().get('callId');// from Capture Call Details button
        String details = ApexPages.currentPage().getParameters().get('details');// from Capture Call Details button
        recordTypeId = ApexPages.currentPage().getParameters().get('RecordType');// from standard Record Type selection page
        
        if(accId != null)
            account = [select Id, Name from Account where ID=:accId];
            
        if(this.call != null && this.call.Id != null){
            callId = this.call.Id;
            action = 'Update';
        }else
            action = 'Insert';
        
        if(callId != null){
            call = [select Id, Name, Activity_Type__c, Attendee1__c, Attendee2__c, Attendee3__c, RecordTypeId,  
                        Call_Activity__c, Call_Date__c, Start_Time__c, End_Time__c, Call_Feedback__c,
                         Competitor_Product_Feedback__c,Competitor_Products__c, Joint_Field_Work__c, 
                        Next_Call_Objective__c, Next_Call_Objective_Date__c, No_of_Set__c, Number_of_Attendees__c, 
                        Topic__c, Type_of_Procedure__c, Workshop_Type__c, Products_Choosen__c, 
                        Account__c,Remarks__c from Call__c where Id=:callId];
            recordTypeId = call.RecordTypeId;
            oldProductsChoosen = call.Products_Choosen__c;
            if(call.Products_Choosen__c != null){
                if(productList == null)
                    productList = new List<WrapperClass>();
                for(String s : call.Products_Choosen__c.split(';')){
                    WrapperClass wrapper = new WrapperClass(s,false);
                    productList.add(wrapper);
                }
            }
         }
        
        if(details != null && details=='1'){
            callDetailsFlag = true;
            callDetail = getCallDetail();
        }
        else
            callDetailsFlag = false;
            
        if(recordTypeId != null){
            RecordType callRecordType = [SELECT Id, Name, DeveloperName, SobjectType, IsActive FROM RecordType 
                    where SobjectType='Call__c' and IsActive=true and Id= :recordTypeId limit 1];
            if(callRecordType != null){
                recordTypeName = callRecordType.Name;
            }
        }
                
        DateTime callDate = null;
        if(call != null && call.Start_Time__c != null)
            callDate = call.Start_Time__c;
        if(callDate != null){
            Long difference = callDate.addDays(5).getTime()-system.now().getTime();            
            if(difference<=0){
                editFlag = false;
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, Label.Call_Edit_Error));
            }
            else
                editFlag = true;            
        }else{
            editFlag = true;
            planEditFlag = true;
        }
        
        if(editFlag && !callDetailsFlag && callDate != null){
            Long difference1 = callDate.getTime()-system.now().getTime();
            if(difference1<=0){
                planEditFlag = false;
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, Label.Call_Plan_Edit_Error));                
            }
            else
                planEditFlag = true;
        }
        
        if(productList == null)
            productList = new List<WrapperClass>();
            
        system.debug('--- CallController :: CallController() :: editFlag --- '+editFlag);
        system.debug('--- CallController :: CallController() :: callDetailsFlag --- '+callDetailsFlag);
        system.debug('--- CallController :: CallController() :: recordTypeName --- '+recordTypeName);
        system.debug('--- CallController :: CallController() :: action --- '+action);
        system.debug('--- CallController :: CallController() :: planEditFlag --- '+planEditFlag);
    }
    
    public List<SelectOption> getUserProducts(){
    
        system.debug('--- CallController :: getUserProducts() ---');        
        List<MyProduct__Share> userProdShareList = [select Id, Parent.Id, Parent.Name, Parent.Active__c from MyProduct__Share 
                                                    where UserOrGroupId=:UserInfo.getUserId() and Parent.Active__c=true];
        List<SelectOption> options = new List<SelectOption>();           
        options.add(new SelectOption('','--Select--'));         
        for(MyProduct__Share tempShare : userProdShareList){
            options.add(new SelectOption(tempShare.Parent.Name,tempShare.Parent.Name));            
        }        
        return options;    
    }
    
    public PageReference saveDetails(){
    
        system.debug('--- CallController :: saveDetails() ---');
        if(call != null){
            if(recordTypeId != null)
                call.RecordTypeId = recordTypeId;
            else{
                RecordType callRecordType = [SELECT Id, Name, DeveloperName, SobjectType, IsActive FROM RecordType 
                    where SobjectType='Call__c' and IsActive=true and Name= :call.Activity_Type__c limit 1];
                call.RecordTypeId =  callRecordType.Id;
            }
            String productsChoosen = '';//call.Products_Choosen__c;
            for(Integer i=0; i<productList.size(); i++){
                if(i != (productList.size()-1))
                    productsChoosen = productsChoosen+productList.get(i).product+';';
                else
                    productsChoosen = productsChoosen+productList.get(i).product;
            }
            call.Products_Choosen__c = productsChoosen;
            insert call;
            system.debug('--- CallController :: saveDetails() :: call.Id---'+call.Id);
            if(call.Id != null){
                if(productsChoosen != null){ 
                    //productsChoosen = productsChoosen.trim(); 
                    //productsChoosen = productsChoosen.replaceAll('/\r\n|\r|\n/g',';');
                    //productsChoosen = productsChoosen.replaceAll('\n',';');
                    system.debug('--- tempList ---'+productsChoosen);        
                    //List<String> tempList = productsChoosen.split(';;');
                    List<String> tempList = productsChoosen.split(';');
                    //system.debug('--- tempList ---'+tempList);
                    Map<String,ID> productMap = new Map<String,ID>();
                    List<CallDetail__c> callDetailList = new List<CallDetail__c>();
                    for(MyProduct__c tempProduct : [select Id, Name, Active__c from MyProduct__c where Name in :tempList]){
                        if(tempProduct.Active__c)
                            productMap.put(tempProduct.Name,tempProduct.Id);
                    }
                    system.debug('--- productMap ---'+productMap);
                    if(productMap != null && productMap.size()>0){
                        for(String s : tempList){
                            if(productMap.get(s) != null){
                                CallDetail__c callDetail = new CallDetail__c();
                                callDetail.Call__c = call.Id;
                                callDetail.Product__c = productMap.get(s);
                                callDetailList.add(callDetail);
                            }
                        }
                    }
                    if(callDetailList != null && callDetailList.size()>0)
                        insert callDetailList;
                }
                PageReference pageRef = new PageReference('/'+call.Id);
                return pageRef;
            }            
        }
        return null;
    }
    
    public PageReference updateDetails(){
    
        system.debug('--- CallController :: updateDetails() ---');
        if(call != null){
            String productsChoosen = '';//call.Products_Choosen__c;
            for(Integer i=0; i<productList.size(); i++){
                if(i != (productList.size()-1))
                    productsChoosen = productsChoosen+productList.get(i).product+';';
                else
                    productsChoosen = productsChoosen+productList.get(i).product;
            }
            call.Products_Choosen__c = productsChoosen;
            update call;
            system.debug('--- CallController :: updateDetails() :: oldProductsChoosen ---'+oldProductsChoosen);
            system.debug('--- CallController :: updateDetails() :: Products_Choosen__c ---'+call.Products_Choosen__c);
            system.debug('--- CallController :: updateDetails() ---');
            if(oldProductsChoosen == call.Products_Choosen__c){
                system.debug('--- CallController :: updateDetails :: No Modifications to Products ---');
            }else{
                system.debug('--- CallController :: updateDetails :: Products Modified ---');
                // delete all previous product-call association records
                List<CallDetail__c> toDeleteList = [select Id, Name from CallDetail__c where Call__c= :call.Id];
                if(toDeleteList != null && toDeleteList.size()>0)
                    delete toDeleteList;
                // Add modified product-call association records
                if(productsChoosen != null){                    
                    //productsChoosen = productsChoosen.trim(); 
                    //productsChoosen = productsChoosen.replaceAll('/\r\n|\r|\n/g',';');
                    //productsChoosen = productsChoosen.replaceAll('\n',';');
                    //system.debug('--- tempList ---'+productsChoosen);        
                    //List<String> tempList = productsChoosen.split(';;');
                    List<String> tempList = productsChoosen.split(';');
                    system.debug('--- tempList ---'+tempList);
                    Map<String,ID> productMap = new Map<String,ID>();
                    List<CallDetail__c> callDetailList = new List<CallDetail__c>();
                    for(MyProduct__c tempProduct : [select Id, Name, Active__c from MyProduct__c where Name in :tempList]){
                        if(tempProduct.Active__c)
                            productMap.put(tempProduct.Name,tempProduct.Id);
                    }
                    if(productMap != null && productMap.size()>0){
                        for(String s : tempList){
                            if(productMap.get(s) != null){
                                CallDetail__c callDetail = new CallDetail__c();
                                callDetail.Call__c = call.Id;
                                callDetail.Product__c = productMap.get(s);
                                callDetailList.add(callDetail);
                            }
                        }
                    }
                    if(callDetailList != null && callDetailList.size()>0)
                        insert callDetailList;
                }
            }
            PageReference pageRef = new PageReference('/'+call.Id);
            return pageRef;
        }
        return null;
    }
    
    public PageReference submitDetails(){
    
        system.debug('--- CallController :: submitDetails() ---');
        update call;    
        return cancel();
    }
    
    public PageReference cancel(){
        
        system.debug('--- CallController :: cancel() ---');
        if(accId != null){
            PageReference pageRef = new PageReference('/'+accId);
            system.debug('--- CallController :: cancel() :: pageRef ---'+pageRef);        
            return pageRef;
        }
        if(retURL != null){
            PageReference pageRef = new PageReference(retURL);
            system.debug('--- CallController :: cancel() :: pageRef ---'+pageRef);        
            return pageRef;
        }
        if(callId!= null){
            PageReference pageRef = new PageReference('/'+callId);
            system.debug('--- CallController :: cancel() :: pageRef ---'+pageRef);        
            return pageRef;
        }
        return null;
    }
    
    public void getCallType(){
        String test = ApexPages.currentPage().getParameters().get('test');
        system.debug('--- test ---'+test);
        recordTypeName = test;
    }
    
    public void setCallType(){
        recordTypeName = call.Activity_Type__c;
    }
    
    public PageReference addProduct(){
        system.debug('--- selectedProduct ---'+selectedProduct);
        //selectedProduct = 'GUT PLAIN SUTURE 18IN(45CM) 5-0 UND';
        if(productList == null)
            productList = new List<WrapperClass>();
            
            Boolean isAlreadyAdded = false;
            for(WrapperClass wrp: productList)
            {
               if(selectedProduct.equalsignorecase(wrp.product))
               {
                   isAlreadyAdded = true;
               }
            }
            
            if(selectedProduct != null && isAlreadyAdded != true )
            {
               WrapperClass wrapper = new WrapperClass(selectedProduct,false);
               productList.add(wrapper);
            }
            
        /*if(selectedProduct != null)
        {
            WrapperClass wrapper = new WrapperClass(selectedProduct,false);
            productList.add(wrapper);
        }*/
        system.debug('---- productList size ----'+productList.size());
        return null;
    }
    
    public PageReference removeProduct(){
        if(productList != null && productList.size()>0){
            system.debug('---- productList size ----'+productList.size());
            List<WrapperClass> tempList = new List<WrapperClass>();
            for(WrapperClass temp : productList){
                system.debug('---- checkbox selected product name  ----'+temp);
                if(temp.selected == false){
                    system.debug('---- checkbox selected product name  ----'+temp.product);  
                    tempList.add(temp);
                }
            }
            productList = tempList;
        }
        return null;
    }
    
    public void setCall(Call__c call){
        this.call = call;
    }
    
    public Call__c getCall(){
        if(this.call == null)
            call = new Call__c();
        if(account != null )
            call.Account__c = account.Id;
        return call;
    }
    
    public void setCallDetail(CallDetail__c callDetail){
        this.callDetail = callDetail;
    }
    
    public CallDetail__c getCallDetail(){
        if(this.callDetail == null){
            List<CallDetail__c> callDetailList = new List<CallDetail__c>();
            if(this.call != null){
                callDetailList = [select Id, Name, Call_Feedback__c from CallDetail__c where Call__c=:this.call.Id];
                if(callDetailList.size()>0)
                    callDetail = callDetailList.get(0);
            }
            if(callDetail == null)
                callDetail = new CallDetail__c();
        }
        return callDetail;
    }
    
    public class WrapperClass{        
        public String product {get; set;}
        public Boolean selected {get; set;}
        public WrapperClass(String p,Boolean s) {
            product = p;
            selected = s;
        }
    }
}

 

HusseyHussey

Hi Tanmoy,

 

Can you please send your current test class through which you are getting 63% code coverage, so that i can update that test class.

 

 

Thanks,

Hussain