DEFINING SUB PROGRAMS IN APEX SALESFORCE
When working with Apex in Salesforce, breaking down business logic into manageable pieces is a best practice. One of the ways to do this is by using Sub Programs, which help in organizing and structuring logic for reusability and clarity.
Let’s dive into the two core types of sub programs in Apex: Parameterized and Non-Parameterized Procedures.
📌 What is a Sub Program?
A Sub Program is a section of a program that performs a specific task. In Apex, sub programs are written inside classes and come in two major forms:
- Procedures – Execute logic but do not return a value.
- Functions – Execute logic and return a value.
This post focuses on Procedures, especially the difference between Parameterized and Non-Parameterized types.
🔧 Non-Parameterized Procedures
✅ Definition:
Non-Parameterized procedures are sub programs that do not accept any input. They perform predefined operations and are static in behavior.
🔠 Syntax:
apex
CopyEdit
[AccessSpecifier] void procedureName() {
// Business Logic
}
🧪 Example:
public class MathClass {
public void Addition() {
System.debug(‘Addition Operation.’);
}
public void Multiply() {
System.debug(‘Multiply Method.’);
}
public void InsertAccountRecord() {
// Logic to insert Account Record
}
}
🚀 Invocation:
MathClass mClass = new MathClass();
mClass.Addition();
mClass.Multiply();
mClass.InsertAccountRecord();
📍 Real-World Use Case:
public class AccountsHelper {
public void CreateNewAccount() {
Account acc = new Account(Name=’Apex Account Record’, Rating=’Hot’, Industry=’Finance’, AnnualRevenue=3500000, Type=’Customer Direct’, Phone=’9900990000′, Fax=’9988778888′, Website=’www.salesforce.com’, BillingCity=’Kochi’, Active__c=’Yes’);
insert acc;
if(acc.Id != null) {
Case cs = new Case(Subject=’My Webcam is Not Working’, Description=’My Webcam is not working. Please help.’, Priority=’High’, Status=’New’, Origin=’Phone’, AccountId=acc.Id);
insert cs;
}
}
}
// Execute
AccountsHelper helper = new AccountsHelper();
helper.CreateNewAccount();
🔧 Parameterized Procedures
✅ Definition:
Parameterized procedures accept input values, which makes them dynamic and reusable. The input parameters guide how the method behaves.
🔠 Syntax:
apex
CopyEdit
[AccessSpecifier] void procedureName(DataType param1, DataType param2, …) {
// Business Logic
}
🧪 Example:
public class MathOperations {
public void Addition(Integer value1, Integer value2) {
System.debug(‘Addition Result: ‘ + (value1 + value2));
}
public void Multiply(Integer value1, Integer value2, Integer value3) {
System.debug(‘Multiplication Result: ‘ + (value1 * value2 * value3));
}
public void Division(Integer value1, Integer value2) {
if (value2 > 0) {
System.debug(‘Division Result: ‘ + (value1 / value2));
} else {
System.debug(‘Division Operation Cannot be performed’);
}
}
}
🚀 Invocation:
MathOperations mOp = new MathOperations();
mOp.Addition(2000, 34); // Output: 2034
mOp.Multiply(10, 2, 5); // Output: 100
mOp.Division(100, 0); // Output: Error message
📍 Real-World Use Case:
public class HiringManagerHelper {
public static void CreateRecruiterRecords(Integer maxRecords) {
for (Integer i = 1; i <= maxRecords; i++) {
Hiring_Manager__c hr = new Hiring_Manager__c(Name=’Sample HR ‘ + i, Location__c=’Chennai’, Email_ID__c=’samplehr’+i+’@gmail.com’, Contact_Number__c=’9900990000′);
insert hr;
}
}
}
// Execute
HiringManagerHelper.CreateRecruiterRecords(50);
Another practical example is creating a related Case record after an Account is created:
apex
CopyEdit
public class AccountsHelper {
public void CreateAccountRecord() {
Account acc = new Account(Name=’Parent Account Record’, Rating=’Hot’, Industry=’Banking’, AnnualRevenue=4500000, Type=’Prospect’, Ownership=’Public’, Phone=’9999999999′, Fax=’9988889988′, Website=’www.gmail.com’, Active__c=’Yes’, CustomerPriority__c=’High’);
insert acc;
if(acc.Id != null) {
CreateRelatedCase(acc.Id);
}
}
public void CreateRelatedCase(Id accId) {
Case cs = new Case(Status=’New’, Priority=’High’, Origin=’Phone’, Type=’Mechanical’, Reason=’Performance’, Subject=’Printer issue’, Description=’Unable to print. Need help.’, AccountId=accId);
insert cs;
}
}
// Execute
AccountsHelper accHelper = new AccountsHelper();
accHelper.CreateAccountRecord();
🎯 Summary
Feature | Non-Parameterized | Parameterized |
Accepts Input | ❌ No | ✅ Yes |
Behavior | Static | Dynamic |
Reusability | Limited | Highly Reusable |
Syntax Example | void method() | void method(Type param1, Type param2) |
Real-World Use Case | Predefined Account Insert | Dynamic HR Record Insert |
By understanding and properly utilizing parameterized and non-parameterized procedures, developers can build more maintainable, reusable, and scalable Apex code in Salesforce.