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
Shelly NyeinShelly Nyein 

Apex Class Code Coverage Problem in deploying production

Hi, I have apex code below and I've been trying to deploy it to production. But, it always failed with 72% code coverage error, I'm not sure how to resolve this error, I tried to create test class, but it didn't help me. Someone please help me suggest on this. Thank you so much.

public class eReceiptExt {
    
    private final Opportunity oppty;
    private final Account acc;
    public boolean pb1 {get;set;}
    
    private final User ceo;

    private final eReceipt_info__c eReceipt;
    private final boolean isPersonAccount;

    private final Contact contactperson;
    public string email;
    
    public string oponame;
    

   
    
    public eReceiptExt(ApexPages.StandardController stdController) {
         recordType recType;
         this.oppty = (Opportunity)stdController.getRecord();
         this.oppty.eReceipt_Date__c = date.today();
         this.acc = [select id, name, recordtypeId, NRIC_Passport_No__pc,Mailing_Name__pc, mailing_name__c,TYPE,PERSONMAILINGPOSTALCODE, PERSONMAILINGSTREET, PERSONMAILINGCITY, PERSONMAILINGCOUNTRY, PERSONEMAIL, Salutation, LastName, FirstName from account where id =:this.oppty.accountId ];
       
         oponame = this.oppty.name;

         Opportunity opo = [select id, name, IPC_Receipt_No__c from Opportunity where id =:this.oppty.id];

         
         if(opo.IPC_Receipt_No__c == null)
         {
             pb1 = true;
         }else
         {
             pb1 = false;
         }
                
   
         
         System.debug('My commercial  value is ' + this.oppty.amount + this.oppty.Non_Tax_Exempt_Amount__c);       
         decimal tax;
     
         if(this.oppty.Non_Tax_Exempt_Amount__c!= null)
         {
             System.debug('My tax value is here '+ tax);
          
             tax = this.oppty.amount - this.oppty.Non_Tax_Exempt_Amount__c;
         }
        else
            {
                tax = this.oppty.amount;
            }
                   
        

         recType = [select ispersontype from recordType where id=:this.acc.recordtypeId];
      
         this.isPersonAccount = recType.ispersontype;
         
         
         this.eReceipt = [select COMPANY_YEAR__C, FROM_AND_END__c, transformed_no_of_life__c from eReceipt_info__c order by CREATEDDATE DESC limit 1];
      
         String type = 'Corporate';
         
         
         if(this.acc.TYPE == type){
             if(this.oppty.Thank_You_Letter_Contact__c != null)
             {
                 this.contactperson = [Select Email,  Mailing_Name__c, MailingStreet, Salutation, LastName, FirstName from contact where id =:this.oppty.Thank_You_Letter_Contact__c];
                 System.debug('My Thank you letter person is '+this.contactperson);
             }else{
                 system.debug('There is no Thank_you_person, need to send email to Contact Person!');
                 ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'No Thank You Person (corp) is found!'));
             }
             
          }
          else
          {
              email = this.acc.PERSONEMAIL;
          }
       
    }
    
   
    
    public string getsalutation(){
        string salutation = this.acc.Salutation;
        return salutation;
    }
    
 
    
   public string getfName(){
        string sal = this.acc.Salutation;
         string LN= this.acc.LastName;
         string FN= this.acc.FirstName;
         string MN = this.acc.mailing_name__c;
         string MP = this.acc.Mailing_Name__pc;
         string getFN;
         
       
        
             if(sal != null)
             {
                 getFN = sal +' '+LN;
                
             }
             else 
             {
                 getFN = FN +' '+ LN;
                 
             }
        
                 
         return getFN;
   }
   
   public string getlastname(){
         string salutation = this.acc.Salutation;
         string lastname = this.acc.LastName;
         string firstname = this.acc.FirstName;
         string mailingPname = this.acc.Mailing_Name__pc;
         string resultlastname;
         
        
             if(salutation != null)
             {
                 resultlastname = salutation +' '+ lastname;
             }else{
                 resultlastname = mailingPname;
             }
        
        return resultlastname;                
   }
   
    
   
   
    public string getfullname(){
         string sal = this.acc.Salutation;
         string lname = this.acc.LastName;
         string fname = this.acc.FirstName;
         string mname = this.acc.mailing_name__c;
         string mPname = this.acc.Mailing_Name__pc;
         string fullname;
         
      
         
         if(mPname == null)
         {
             if(sal != null)
             {
                 fullname = sal +' '+lname;
             }
             else 
             {
                 fullname = fname +' '+ lname;
             }
         }else
         {
             fullname = mPname;
         }
                
                 
      
         
         return fullname;
    }
    
    

    
    public decimal getCompanyYear(){
        return this.eReceipt.COMPANY_YEAR__C;
    }
    
    public PageReference attachPDF() {
        
        List<Attachment> attachmentsToInsert = new List<Attachment>();
        
        PageReference pdf = Page.oppty_attachPdf;
        pdf.getParameters().put('id',this.oppty.id);
        
 
        
        Attachment attach = new Attachment();
        Blob body;
        try{
            body = pdf.getContentasPDF();
        } catch(VIsualforceException e){
            body = Blob.valueOf('unable to get content as pdf');
        }
        attach.Body = body;
      
        attach.Name = this.oppty.IPC_RECEIPT_NO__C + '.pdf';
        attach.IsPrivate = false;
        attach.parentid =  this.oppty.Id;
      
        attach.ContentType = 'application/pdf';
        
        attachmentsToInsert.add(attach);
        if(attachmentsToInsert != null){
            System.debug('I have something here!');
           
            upsert attach;
            update this.oppty;
            return new PageReference('/'+this.oppty.Id);
        }else
        {
            System.debug('I do not have anything to insert!');
        }
        return null;
}
    
    public string getCorporatefullname(){
        string cor_sal = this.contactperson.Salutation;
        string cor_lname = this.contactperson.LastName;
        string cor_fname = this.contactperson.FirstName;
        string fullcorporatname = this.contactperson.Mailing_Name__c;
        string cor_fullname;
        
        if(cor_sal != null){
            cor_fullname = cor_sal + ' ' + cor_lname;
        }else{
            cor_fullname = fullcorporatname;
        }
        return cor_fullname;
    }
    
    public PageReference doCancel()
    {
        return new PageReference('/'+this.oppty.Id);
     }
    
  
    
    public boolean getIsIndividualAccount() {    
        return this.isPersonAccount;
    }
    
    public string getMaskedNRIC() {
        System.debug('My tax option is here '+ this.oppty.Tax_Exempt_Required__c);
        System.debug('My NRIC value is here '+ this.acc.NRIC_Passport_No__pc );
        if(this.oppty.Tax_Exempt_Required__c = true && this.acc.NRIC_Passport_No__pc != null)

        {
           String maskedNRIC = this.acc.NRIC_Passport_No__pc;
            maskedNRIC = '(NRIC: *****' + maskedNRIC.right(4) + ' )';
            return maskedNRIC;
        }else {
            String valaidNRIC ='NRIC is missing!';
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, valaidNRIC ));
            return null;
        }
        
    }
    
   
    
    public PageReference generateEno(){
        system.debug('This oppty id from generateEno is here '+this.oppty.id + ' '+ this.oppty.IPC_RECEIPT_NO__C + ' ' + this.oppty.Tax_Exempt_Required__c);
       
        for (Opportunity opp: [SELECT IPC_Receipt_No__c FROM Opportunity where IPC_RECEIPT_NO__C != null and IPC_RECEIPT_NO__C like '2%' order by IPC_RECEIPT_NO__C desc])
        {
           string oppeNo = opp.IPC_RECEIPT_NO__C ;
           
            Integer result = Integer.ValueOf(oppeNo.trim());//this is integer value
            result++;
            
            String sInteger = String.valueOf(result); //this is string 

           
            opp.IPC_RECEIPT_NO__C = sInteger;
            opp.id = this.oppty.id;
            
          
           
            update(opp);

            PageReference pr = new PageReference('/'+this.oppty.Id);
            pr.setRedirect(true);
            return pr;

        }
        return null;
    }
   

    public string getCustomText() {    
        return this.eReceipt.from_and_end__c;
    }

    public decimal getTransformedNumberOfLife() {    
        return this.eReceipt.transformed_no_of_life__c;
    }
    
    public String getMyText() {
        String myText = '&nbsp;';
        return myText;
    }
    
    
    public string getIndiv(){
        string result='r1';
        if((this.acc.PERSONEMAIL==null)&&(this.acc.PERSONMAILINGSTREET == null))
        {
            return result;
        }
        
        if(this.acc.PERSONEMAIL==null)
        {
            if(this.acc.PERSONMAILINGSTREET != null){
                return null;
            }else{
                return result;
            }
        }
        
        if((this.acc.PERSONEMAIL!=null)&&(this.acc.PERSONMAILINGSTREET != null))
        {
             return result;   
        }
        
        return result;
    }
    
    
    
    
    public Contact cp;
    
    public string getThankYouPersonEmail() { 
        
        string noemail;  
        string resultemail= 'r2';
        
         
        if(this.acc.TYPE == 'Corporate')
        {
            this.cp = [Select Email,MAILINGSTREET from contact where Mailing_Name__c =:this.oppty.name];
          
            if(this.cp.Email == null)
            {
                if(this.contactperson.Email == null)
                {
                    
                    return null;
                }
            }
            
            if((this.cp.Email != null)||(this.contactperson.Email != null))
            {
              
                return resultemail;
            }
         
        }
        return resultemail;
    }
    
}
Tad Aalgaard 3Tad Aalgaard 3

Code coverage is based on overall code coverage of all classes combined when running the default local tests.
You may be able to Run All Test from Setup \ Develop \ Apex Classes to have it recalculate your coverage and get you above 75%.

Otherwise, you need to write a test class. 

Here is a basic shell for your class that you can start with.  You may 
 

Opportunity o = new Opportunity;
insert o;

ApexPages.StandardController sc = new ApexPages.StandardController(o);

eReceiptExt er = new eReceiptExt(sc);