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
ram sf 10ram sf 10 

Help with Code Coverage for extension controller class

Hi..

I am New to Salesforce Development and strugling with the Code coverage for Apex Class which is used as an Extension in a Visualforce page.

This is my code for the Controller Class. Can Someone PLease help me out on this so that I can use this information as a guide for my future development.
*************************************************************************************************************
*************************************************************************************************************
public class manualpaymentcntrl{
  
    public Payment__c ptxn{ get; set; }
    public Contact con { get; set; }
    public Batch_Processing__c bp {get; set;}
    public Campaign c {get; set;}
    public boolean bsbnull {get; set;}
public manualpaymentcntrl(ApexPages.StandardController std) {
    con=new Contact();
    ptxn=new Payment__c();
    ptxn.Batch_Processing__c = ApexPages.currentPage().getParameters().get('Batch');
    ptxn.Campaign__c = ApexPages.currentPage().getParameters().get('Cam');
   }
 
public void obtaindetails() {
    for (Contact con : [select Name,FirstName,LastName,Email,AccountID,Phone,MobilePhone,Title, MailingStreet,MailingCity,MailingState,MailingCountry,MailingPostalCode,AAkPay_BSB_No__c,Account_No__c,Bank__c,Drawer__c from Contact where Id = :ptxn.Contact__c]){
  
        ptxn.FirstName__c = con.FirstName;
        ptxn.LastName__c = con.LastName;
        ptxn.Email__c = con.Email;
        ptxn.Account__c = con.AccountID;
        ptxn.Phone__c = con.Phone;
        ptxn.MobilePhone__c = con.MobilePhone;
        ptxn.Salutation__c = con.Title;
        ptxn.MailingStreet__c = con.MailingStreet;
        ptxn.MailingCity__c = con.MailingCity;
        ptxn.MailingState__c = con.MailingState;
        ptxn.MailingCountry__c = con.MailingCountry;
        ptxn.MailingPostalCode__c = con.MailingPostalCode;
        ptxn.BSB_No__c = con.AAkPay_BSB_No__c;
        ptxn.Account_No__c = con.Account_No__c;
                ptxn.Bank__c = con.Bank__c;
        ptxn.Drawer__c = con.Drawer__c;
                ptxn.Payment_Setting__c = 'a0m90000009Qwb4';
        ptxn.Payment_Type__c = 'a0o90000003Lhl2';
        ptxn.Bank_Deposit_Date__c = system.today();
        ptxn.Status__c = 'Receipting Start';
        ptxn.Transaction_Date__c = system.today();
        ptxn.Bank_Deposit_Account__c = '083-004-461061543' ;
        ptxn.Transaction_Type__c = 'Purchase';
   }
  }
    public void bankinfo(){
        if (ptxn.Method_of_Payment__c != 'Cheque'){
                ptxn.BSB_No__c = null;
            ptxn.Account_No__c = null;
                                ptxn.Bank__c = null;
                ptxn.Drawer__c = null;
           
        }
        else{
            ptxn.BSB_No__c = ptxn.Contact__r.AAkPay_BSB_No__c;
            ptxn.Account_No__c = ptxn.Contact__r.Account_No__c;
                ptxn.Bank__c = ptxn.Contact__r.Bank__c;
        ptxn.Drawer__c = ptxn.Contact__r.Drawer__c;
    }
    }
    public PageReference save(){
    String baseURL = URL.getSalesforceBaseUrl().toExternalForm();
    String PageURL = ApexPages.currentPage().getUrl();
    String wholeURL = baseURL+PageURL;
       
        for (Batch_Processing__c bp : [Select No_of_Transactions__c,Transaction_Count__c,Status__c from Batch_Processing__c where ID=:ptxn.Batch_Processing__c and Status__c != 'Completed']){
            insert ptxn;   
        PageReference reRend = new PageReference('/apex/custommanualpayment'+wholeURL);
        reRend.setRedirect(true);
        return reRend;
        }
        {
    PageReference reRend = new PageReference('/'+ptxn.Batch_Processing__c);
        reRend.setRedirect(true);
        return reRend;       
        }
    }
    public PageReference cancel(){
        PageReference reRend = new PageReference('/'+ptxn.Batch_Processing__c);
        reRend.setRedirect(true);
        return reRend;
    }
}

Thanks in Advance....

Regards,
Ram
Jagan ReddyJagan Reddy
@isTest
public class manualpaymentcntrl_test
{
    static testMethod void manualtest()
    {
        
        Contact con=new Contact();----------------> here insert required fields in contact
        con.Lastname='reddy';
        con.phone='55555555';
        con.email='test@gmail.com';
        insert con;
        
        Payment__c pay=new Payment__c();---------->insert required fields
        pay.Lastname='jagan';
        pay.email='test@gmail.com';
        insert pay;
        ApexPages.StandardController std=new ApexPages.StandardController(pay);
        manualpaymentcntrl mpc=new manualpaymentcntrl(std);
        mpc.obtaindetails();
        mpc.bankinfo();
        mpc.save();
        mpc.cancel();
    }
}

try this still here you need to assign and insert values to campaign and Batch_Processing__c.
ram sf 10ram sf 10
Hi Jagan,

Thank you very much for the reply..

I have tried the code and the Code coverage is 50%....

I have checked to see the issue and the code is not covered for the fields that I am populating on the Visualforce page by calling the method "Obtaindetails" in the Action support. I have the Highlighted and underlightened the Problem lines below.

Do you have any suggestions???  please let me know.

Thanks in Advance...

Regards,
Ram
********************************************************************************************************************************************

public void obtaindetails() {
    for (Contact con : [select Name,FirstName,LastName,Email,AccountID,Phone,MobilePhone,Title, MailingStreet,MailingCity,MailingState,MailingCountry,MailingPostalCode,AAkPay_BSB_No__c,Account_No__c,Bank__c,Drawer__c from Contact where Id = :ptxn.Contact__c]){
  
        ptxn.FirstName__c = con.FirstName;
        ptxn.LastName__c = con.LastName;
        ptxn.Email__c = con.Email;
        ptxn.Account__c = con.AccountID;
        ptxn.Phone__c = con.Phone;
        ptxn.MobilePhone__c = con.MobilePhone;
        ptxn.Salutation__c = con.Title;
        ptxn.MailingStreet__c = con.MailingStreet;
        ptxn.MailingCity__c = con.MailingCity;
        ptxn.MailingState__c = con.MailingState;
        ptxn.MailingCountry__c = con.MailingCountry;
        ptxn.MailingPostalCode__c = con.MailingPostalCode;
        ptxn.BSB_No__c = con.AAkPay_BSB_No__c;
        ptxn.Account_No__c = con.Account_No__c;
                ptxn.Bank__c = con.Bank__c;
        ptxn.Drawer__c = con.Drawer__c;
                ptxn.Payment_Setting__c = 'a0m90000009Qwb4';
        ptxn.Payment_Type__c = 'a0o90000003Lhl2';
        ptxn.Bank_Deposit_Date__c = system.today();
        ptxn.Status__c = 'Receipting Start';
        ptxn.Transaction_Date__c = system.today();
        ptxn.Bank_Deposit_Account__c = '083-004-461061543' ;
        ptxn.Transaction_Type__c = 'Purchase';

   }
  }
Jagan ReddyJagan Reddy
after getting code coverage go to developer console and check there which lines are not covered, i am unable to do that with out checking the lines.

Thanks,
Jagan
jagansfdc@outlook.com
ram sf 10ram sf 10
Hi Jagan,

The Lines that I have posted above are the ones that are not covered. I am posting them below. It seems that the issue is with the Lines where i am assigning the Payment Object fields with the values of Contact object (ptxn.firstname__c = con.FirstName) are causing the issue.


ptxn.FirstName__c = con.FirstName;
        ptxn.LastName__c = con.LastName;
        ptxn.Email__c = con.Email;
        ptxn.Account__c = con.AccountID;
        ptxn.Phone__c = con.Phone;
        ptxn.MobilePhone__c = con.MobilePhone;
        ptxn.Salutation__c = con.Title;
        ptxn.MailingStreet__c = con.MailingStreet;
        ptxn.MailingCity__c = con.MailingCity;
        ptxn.MailingState__c = con.MailingState;
        ptxn.MailingCountry__c = con.MailingCountry;
        ptxn.MailingPostalCode__c = con.MailingPostalCode;
        ptxn.BSB_No__c = con.AAkPay_BSB_No__c;
        ptxn.Account_No__c = con.Account_No__c;
                ptxn.Bank__c = con.Bank__c;
        ptxn.Drawer__c = con.Drawer__c;
                ptxn.Payment_Setting__c = 'a0m90000009Qwb4';
        ptxn.Payment_Type__c = 'a0o90000003Lhl2';
        ptxn.Bank_Deposit_Date__c = system.today();
        ptxn.Status__c = 'Receipting Start';
        ptxn.Transaction_Date__c = system.today();
        ptxn.Bank_Deposit_Account__c = '083-004-461061543' ;
        ptxn.Transaction_Type__c = 'Purchase';