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
sf training 36sf training 36 

Can someone help me in writing test class for the following code

public class LPT_InterestAccrued_TriggerHandler {

    public static void updateIAonLoan(List<loan__Loan_Payment_Transaction__c> lptNewData){
        List<loan__Loan_Account__c> loanList = new List<loan__Loan_Account__c>();
        List<loan__Loan_Payment_Transaction__c> lptList = [select id, loan__Cleared__c,
                                                           loan__Loan_Account__r.loan__Loan_Status__c, 
                                                           loan__Loan_Account__r.loan__Interest_Accrued_Not_Due__c
                                                           from loan__Loan_Payment_Transaction__c where id in:lptNewData];
                                                           
                System.debug('Loan payment trsansactions list:====='+lptList);                                         
        for(loan__Loan_Payment_Transaction__c lpt: lptList){
            System.debug('Loan payment trsansactions list:====='+lpt);  
               

               if(lpt.loan__Loan_Account__r.loan__Loan_Status__c=='Closed- Written Off')
               {
                    loan__Loan_Account__c l = new loan__Loan_Account__c();
                    
                    l.id = lpt.loan__Loan_Account__c;
                    
                    System.debug('Loans:====='+l);  
                    if(lpt.loan__Loan_Account__r.loan__Interest_Accrued_Not_Due__c!=0){
                        l.loan__Interest_Accrued_Not_Due__c=0;
                        loanList.add(l);
                    System.debug('Loans list:====='+loanList);  
                    }            
                }
                
                
            } 
            
        
        try{
            update loanList;
        }
        Catch(Exception e){
            System.debug('Exception Occurred:'+e);
        }
    }
}
Best Answer chosen by sf training 36
Amit Chaudhary 8Amit Chaudhary 8
try below code
@isTest 
public class LPT_InterestAccrued_TriggerHandlerTest 
{
   public static testMethod void testMethod1() 
    {
    
    loan__Loan_Account__c loanList = new loan__Loan_Account__c();
    
        loan__Loan_Account__c acc = new loan__Loan_Account__c();
            acc.loan__Interest_Accrued_Not_Due__c = 100;
            acc.loan__Loan_Status__c='Closed- Written Off';
        insert acc;
        
    
        loan__Loan_Payment_Transaction__c  paymet = new loan__Loan_Payment_Transaction__c();
            paymet.loan__Cleared__c= True;
            paymet.loan__Loan_Account__c =acc.id;
        insert paymet;
		
		Update 	paymet;
		
		List<loan__Loan_Payment_Transaction__c> lstPay = new List<loan__Loan_Payment_Transaction__c>();
		lstPay.add(paymet);
		
		LPT_InterestAccrued_TriggerHandler.updateIAonLoan(lstPay);
    
    }
}

Let us know if this will help you
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Hi Nagarjun,

I will recommend you to start using trailhead to learn about test classes
1) https://trailhead.salesforce.com/modules/apex_testing

Pleasse check below post sample test class
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

Also please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm


You write a test class for this the same way that you would any other:
- Set up some data for the Apex Class to access (loan__Loan_Payment_Transaction__c, loan__Loan_Account__c etc)
- Execute a method/methods:-
- Verify the behaviour with asserts.

Please try below code
@isTest 
public class LPT_InterestAccrued_TriggerHandlerTest 
{
    static testMethod void testMethod1() 
	{
		loan__Loan_Account__c acc = new loan__Loan_Account__c();
			// Add all required field;
			acc.loan__Loan_Status__c='Closed- Written Off';
		insert acc;
		
	
		loan__Loan_Payment_Transaction__c  paymet = new loan__Loan_Payment_Transaction__c();
			// add all required field
			paymet.loan__Loan_Account__c =acc.id;
		insert paymet;
		
 
	}
}

Let us know if this will help you
 
sf training 36sf training 36
Can you correct my code amit ?

@isTest 
public class LPT_InterestAccrued_TriggerHandlerTest 
{
   public static testMethod void testMethod1() 
    {
    
    loan__Loan_Account__c loanList = new loan__Loan_Account__c();
    
        loan__Loan_Account__c acc = new loan__Loan_Account__c();
            acc.loan__Interest_Accrued_Not_Due__c = 100;
            acc.loan__Loan_Status__c='Closed- Written Off';
        insert acc;
        
    
        loan__Loan_Payment_Transaction__c  paymet = new loan__Loan_Payment_Transaction__c();
            paymet.loan__Cleared__c= True;
            paymet.loan__Loan_Account__c =acc.id;
        insert paymet;
        
        if(acc.loan__Loan_Status__c =='Closed- Written Off')
               {
                    loan__Loan_Account__c l = new loan__Loan_Account__c();
                    
                    l.id = acc.id;
                    
                    System.debug('Loans:====='+l);  
                    if(paymet.loan__Interest_Accrued_Not_Due__c!=0){
                        l.loan__Interest_Accrued_Not_Due__c=0;
                        loanList.add(l);
                    System.debug('Loans list:====='+loanList);  
                    }            
                }
            Update loanList;
        
                    System.assert(acc.id,loanList.id);
    
    }
}
Amit Chaudhary 8Amit Chaudhary 8
try below code
@isTest 
public class LPT_InterestAccrued_TriggerHandlerTest 
{
   public static testMethod void testMethod1() 
    {
    
    loan__Loan_Account__c loanList = new loan__Loan_Account__c();
    
        loan__Loan_Account__c acc = new loan__Loan_Account__c();
            acc.loan__Interest_Accrued_Not_Due__c = 100;
            acc.loan__Loan_Status__c='Closed- Written Off';
        insert acc;
        
    
        loan__Loan_Payment_Transaction__c  paymet = new loan__Loan_Payment_Transaction__c();
            paymet.loan__Cleared__c= True;
            paymet.loan__Loan_Account__c =acc.id;
        insert paymet;
		
		Update 	paymet;
		
		List<loan__Loan_Payment_Transaction__c> lstPay = new List<loan__Loan_Payment_Transaction__c>();
		lstPay.add(paymet);
		
		LPT_InterestAccrued_TriggerHandler.updateIAonLoan(lstPay);
    
    }
}

Let us know if this will help you
 
This was selected as the best answer