Simple Interest Formula in Apex Explained with Code

When you’re learning Salesforce development, working with numbers and formulas is a great way to build confidence with Apex. In this blog post, we’ll walk through a simple method written in Apex that calculates Simple Interest using three inputs: principal, interest rate, and time in years.

This is a perfect exercise if you’re new to Apex or just brushing up on your basic programming logic. Don’t worry we’ll keep things easy to follow!

🧠 What Are We Trying to Do?

 

Let’s say you have a Salesforce application where users enter financial data  like the amount of a loan, the interest rate, and the number of years. You want to calculate the Simple Interest based on this input and return the result.

The formula we’re using is:

Simple Interest = (Principal × Rate × Time) / 100

We’ll create an Apex method that takes in the principal, rate, and time, does the math, and returns the result as a Decimal. It also checks for null values to make sure we don’t run into errors.

🔍 Code Explanation

 

public with sharing class ApexUseCaseTwo {
    public static Decimal calculateInterest(Decimal principal, Decimal interest, Integer timeInYears) {

We define a class called ApexUseCaseTwo.

Inside the class, we define a static method called calculateInterest. It accepts three input parameters:

  • principal: the original amount

  • interest: the annual interest rate

  • timeInYears: number of years

We make sure all of them are not null before performing the calculation.

if(principal != null && interest != null && timeInYears != null){
            Decimal simpleInterest = (principal * interest * timeInYears)/100;
            return simpleInterest;
        }

If all three inputs are valid (not null), we apply the simple interest formula and return the result.

return 0;
    }
}

If any of the inputs are missing, we safely return 0 to avoid errors or bad calculations.

✅ Why This Is Useful

 

This method is simple but practical it introduces how to:

  • Work with method parameters and return types

  • Use basic arithmetic inside Apex

  • Perform null checks to avoid runtime issues

You can reuse this method anywhere in your Salesforce logic  like Lightning components, LWC, or backend processes  whenever you need to calculate interest.

It’s also a great pattern for handling small calculations that depend on user input.

🧾 Final Code Snippet

 
public with sharing class ApexUseCaseTwo {
    
    public static Decimal calculateInterest(Decimal principal, Decimal interest, Integer timeInYears){
        if(principal != null && interest != null && timeInYears != null){
            Decimal simpleInterest = (principal * interest * timeInYears)/100;
            return simpleInterest;
        }
        return 0;
    }
}

🎥 Watch It in Action!

 

If you’re more of a visual learner, I’ve got you covered. Watch this short video where I explain the Apex method and show it in action:

Want to Apply As Content Writer?

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart

Let's get you started!

Interested in writing Salesforce Content?

Fill in this form and we will get in touch with you :)