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
mahemahe 

Apex Test case 40%

Hi, I was trying to write test case for below class and I was covered 40% only.
Can you please help me to cover for 75% .

public with sharing class Receive_Deliverables {
    string tempDels{get; set;}
    string[] deliArray;
    public string DSId{get;set;}
    public String confirmmsg{get;set;}
    public List<Deliverable__c> lstdels;
    boolean isUnreceive = false;
    public string PrevPageUrl = ApexPages.currentPage().getUrl();

public Receive_Deliverables() {      
        DSId= ApexPages.currentPage().getParameters().get('id');
        UnreceiveLines = getUnreceiveLines();
//refreshPage=false;

    }

public Receive_Deliverables(ApexPages.StandardSetController controller) {
        System.debug('Selected deliverables : '+ApexPages.currentPage().getParameters().get('DeliverablesIds'));
        if(ApexPages.currentPage().getParameters().get('DeliverablesIds') != null){
             tempDels= ApexPages.currentPage().getParameters().get('DeliverablesIds');
             deliArray = tempDels.split(',');
             DSId= ApexPages.currentPage().getParameters().get('id');
             UnreceiveLines = getUnreceiveLines();                                             
        }
      
    }


List<Deliverable__c> ConfirmedDels{get;set;}
    Map<Id, Deliverable__c> InvoiceAmount;
    List<Deliverable__c> UnreceiveLines{get;set;}
    public string getDSId(){
     return DSId= ApexPages.currentPage().getParameters().get('id');

    }

public List<Deliverable__c> getConfirmedDels() {
    if(ConfirmedDels == null)
       ConfirmedDels=[select id,IsLocked,Name,Amount_Paid__c,Balance__c,Deliverable_Number__c,Partial_Amount__c,Make_Partial_Payment__c,Notify_DS_Owner_for_PO_TroubleShoot_Del__c,Work_Approved_By__c,Approved_By_Date__c,Contractor_Invoice__c, Deliverable_Amount__c,due_date__c,Approval_Status__c,Deliverable_Description__c,Invoice_Date__c,PO_Number__c from Deliverable__c where Id IN : deliArray and (Approval_Status__c =: 'Not Completed' Or Approval_Status__c =: 'Deliverable Completed')];
         System.debug('Confirmed Deliverables: '+ConfirmedDels.size());
              
       return ConfirmedDels ;
    }

public List<Deliverable__c> getUnreceiveLines(){
            if(UnreceiveLines == null )
             UnreceiveLines=[select id,IsLocked,Name,Amount_Paid__c,Balance__c,Deliverable_Number__c,Partial_Amount__c,Make_Partial_Payment__c,Notify_DS_Owner_for_PO_TroubleShoot_Del__c,Work_Approved_By__c,Approved_By_Date__c,Contractor_Invoice__c, Deliverable_Amount__c,due_date__c,Approval_Status__c,Deliverable_Description__c,PO_Number__c from Deliverable__c where Opportunity__c =:DSId and (Approval_Status__c =: 'Deliverable Approved for Payment') ];
     
                return UnreceiveLines;
        }

public Map<Id, Deliverable__c> getInvoiceAmount(){
            if(InvoiceAmount == null){
                InvoiceAmount = new Map<Id, Deliverable__c>();
                for(Deliverable__c del : getConfirmedDels()){              
                   InvoiceAmount.put(del.Id,del);              
                }
            }
            return InvoiceAmount ;
      
       }

public void ReceiveDeliverable(){
   
    try{
        if(ConfirmedDels.size() > 0 ){
        Boolean IsInvoiceAmountGreaterthanDeliAmount = false;
        if(UnreceiveLines == null || UnreceiveLines.size() == 0){
                UnreceiveLines = new List<deliverable__c>();
    }                   
        for(Deliverable__c deli : ConfirmedDels){
                deli.Approval_Status__c = 'Deliverable Completed';
                System.debug('Partial Amount: '+deli.Partial_Amount__c);
                if(deli.Partial_Amount__c == 0){
                        deli.Partial_Amount__c = null;
                }
                if(deli.Partial_Amount__c > deli.Deliverable_Amount__c){                   
                        IsInvoiceAmountGreaterthanDeliAmount = true;
                }
                if(deli.Partial_Amount__c <> null ){
              
                        if(deli.Make_Partial_Payment__c == false){
                                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR  ,'Please check [Make Partial Payment?]field when you trying to do partail receive'));
                        }
                }
        }
        List<Database.Saveresult> Dbresult;
        if(!IsInvoiceAmountGreaterthanDeliAmount){                     
           Dbresult = Database.update(ConfirmedDels);
        }
        else
        {             
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR  ,'You cannot receive deliverable morethan deliverable amount. Invoice Amount should be less than or equal to Deliverable amount.'));
        }
      
           for(Integer i =0;i< ConfirmedDels.size();i++){
         
                if(Dbresult[i].isSuccess()){
                        try{
                        // create the new approval request to submit
                        Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
                        req.setComments('Submitted for approval. Please approve.');
                        req.setObjectId(ConfirmedDels[i].Id);
                        // submit the approval request for processing
                        Approval.ProcessResult result = Approval.process(req);
                        // display if the reqeust was successful
                        System.debug('Submitted for approval successfully: '+result.isSuccess());                                              
                          UnreceiveLines.add(ConfirmedDels[i]); 
                        }
                        catch(Exception ex){
                                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR  ,'Unable to Submit the Record , please check the entry criteria.'));
                                 PageReference OppPage = Page.View_List_Of_Deliverables;
 
  

                        }
                }       
          
            }


if(Dbresult[0].isSuccess()){
confirmmsg='You have successfully received in Salesforce. Please click “Close” button to close this window or click “Receive another Deliverable” button if you want to receive another deliverable.';

ApexPages.addmessage(new ApexPages.message(ApexPages.severity.confirm,'REMINDER: 1.When contractor sends you the invoice, label invoice with PO number '+ConfirmedDels[0].PO_Number__c+' and send to AP. Payment will not be released unless invoice is validated by AP. (Except ERS Suppliers). 2. Upload a sofy copy of the invoice to the Deliverable record (click 5 digits deliverable number auto to go into the deliverable record) for archiving purpose. Scroll down to Notes & Attachements section and click Attach File.'));                       
            }
        }
    }
    catch(Exception e){
     ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR  ,'Following error occured while Receiving '+ ApexPages.currentPage().getParameters().get('id')+ e));
    }
   
   if(UnreceiveLines == null || UnreceiveLines.size() == 0){
                UnreceiveLines = getUnreceiveLines();
    }
   
     
}

public void UnReceiveDeliverable(){
    
    try{     
            if(UnreceiveLines.size()>0){
                   
                for(Deliverable__c del : UnreceiveLines){
                        if(del.IsLocked ){
                                // create the new approval request to submit
                                Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();               
                                req.setComments('Submitted for Unreceive deliverable/PO line.');
                                req.setObjectId(del.Id);
                                // submit the approval request for processing
                                Approval.ProcessResult result = Approval.process(req);
                                // display if the reqeust was successful
                                System.debug('Submitted for approval successfully: '+result.isSuccess());
                        }
               
               
                   
                        del.Approval_Status__c ='Not Completed';
                        del.Work_Approved_By__c= null;                    
                        del.Approved_By_Date__c=null;
                        del.Contractor_Invoice__c = null;
                        if(del.Make_Partial_Payment__c){
                                del.Make_Partial_Payment__c=false;
                                del.Partial_Amount__c=Decimal.valueOf('0');
                        }
                   
                }
               update UnreceiveLines;
             
               
            if(UnreceiveLines.size() == 1)
                ApexPages.addmessage(new ApexPages.message( ApexPages.severity.confirm,UnreceiveLines.size() +' Deliverable got Unreceived successfully, Please close the window'));
               
            if(UnreceiveLines.size() >1)
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.confirm,UnreceiveLines.size() +' Deliverables got Unreceived successfully , Please close the window'));
           }
    }
    catch(Exception e){
    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR  ,'Following error occured while Receiving '+ ApexPages.currentPage().getParameters().get('id')+ e));
    }


}



public PageReference goBack() {
    string id = ApexPages.currentPage().getParameters().get('id');
    PageReference OppPage = Page.View_List_Of_Deliverables;//new PageReference('https://na3.salesforce.com/apex/View_List_Of_Deliverables?id=' + id );
    OppPage.getParameters().put('id', id);
    OppPage.setRedirect(true); 
      return OppPage;
   }



public PageReference RedirectToActualPage(){
       
    PageReference OppPage_actual = new PageReference(PrevPageUrl);
    OppPage_actual.setRedirect(true);
    return OppPage_actual;
}

public PageReference CancelAction(){
string id = ApexPages.currentPage().getParameters().get('id');
PageReference OppPage = new PageReference('/' + id );
OppPage.setRedirect(true);
return OppPage;
}

}
Ashish_SFDCAshish_SFDC
Hi Mahe, 


You have to give dummy records that instantiate every method in your classes and meet every condition in the loops. 


Regards,
Ashish
mahemahe
I am new to created test calss. will you help me to provide any example
Vamshi Reddy BandaruVamshi Reddy Bandaru
Hi Mahe,
when you write test cases use different records for testing. every loop and every condition you mentioned will be passed in that ways. YOu should have all kinds of records that would help you invoke every conditon or logic that you used in ur code.
mahemahe
Hi Vamsi,

can we simply copy the code and pass the record? any example please

Vamshi Reddy BandaruVamshi Reddy Bandaru
Hi mahe. I am willing to help you. Can we have a webinar..i live in USA... u can call me on 4692746315