How to Write Custom Code in Salesforce: A Simple Guide to Apex?

They now want faster, scalable, and flexible solutions. That’s why custom code in Salesforce is getting more popular. Many students in Salesforce Training In Hyderabad are learning Apex to meet this demand.

Apr 30, 2025 - 12:31
 0
How to Write Custom Code in Salesforce: A Simple Guide to Apex?

Introduction:

Hyderabad has become one of India’s strongest Salesforce development zones. Many companies here are moving beyond just point-and-click solutions. They now want faster, scalable, and flexible solutions. That’s why custom code in Salesforce is getting more popular. Many students in Salesforce Training In Hyderabad are learning Apex to meet this demand.

What is Apex?

Apex is Salesforce’s own coding language. It works inside the Salesforce platform. You use Apex when you need to do more than what Flows and automation tools allow. Apex is similar to Java. But it is designed only for Salesforce. It runs on the Salesforce servers. So, your code must be clean and efficient.

Where Do We Use Apex?

You use Apex when:

  • The flow is too slow.
  • You need custom logic across objects.
  • You want better control over record updates.
  • You need to run code when records are saved.
  • You want to handle large volumes of data.

In Hyderabad, many mid-level firms use Apex to clean data, manage approvals, or apply logic that Flows can’t handle. Startups are also building full backends with Apex.

How Apex Code Works Behind the Scenes?

When someone saves a record in Salesforce, here’s what happens if Apex is involved:

Step

Action

1

User edits or creates a record

2

A trigger runs

3

The trigger calls Apex code

4

Apex runs your logic

5

Record gets saved

6

Other steps like emails or updates happen later

This entire process happens in a few seconds. Your Apex code must be ready to run quickly and safely.

A Simple Apex Code Example

We also want to send an alert if it’s too big.

Here is the code:

public class DiscountHandler {

    public static void applyDiscount(List<Opportunity> opps) {

        for(Opportunity opp : opps) {

            if(opp.Amount > 100000) {

                opp.Discount__c = 0.10;

                if(opp.Discount__c > 0.08) {

                    sendAlert(opp);

                }

            }

        }

    }

 

    @future

    private static void sendAlert(Opportunity opp) {

        // Send email in background

    }

}

 

This code runs for many records at once. It sends alerts in the background. This is what we call bulk-safe and limit-friendly code.

In Salesforce Certification exams, questions often check if you can write logic that works like this.

What Makes Apex Special?

Apex runs with rules. These rules are called governor limits. They stop bad code from using too many system resources.

Here’s how to stay safe:

  • Don’t run SOQL queries inside loops.
  • Always work with lists and maps.
  • Break big jobs into smaller ones.
  • Use @future or queueable for tasks like emails.
  • Use custom settings for reusable values.

This is important in companies where thousands of records are saved each hour. In Hyderabad’s large CRMs, bad code can crash a process. That’s why training programs here now focus more on code quality than just writing code.

Another Simple Code Sample

Suppose you want to check if a lead’s email already exists in contacts. Here's how:

public class LeadChecker {

    public static void checkLeads(List<Lead> leads) {

        Set<String> emails = new Set<String>();

        for(Lead l : leads) {

            if(l.Email != null) emails.add(l.Email.toLowerCase());

        }

 

        Map<String, Contact> contactMap = new Map<String, Contact>(

            [SELECT Id, Email FROM Contact WHERE Email IN :emails]

        );

 

        for(Lead l : leads) {

            if(contactMap.containsKey(l.Email.toLowerCase())) {

                l.Status = 'Duplicate Contact';

            }

        }

    }

}

 

This code is simple but smart. It avoids limits. It works for many records in one go. You’ll often see problems like this in real Salesforce projects.

Sum up,

Apex is the custom coding tool inside Salesforce. You use it when point-and-click tools are not enough. Apex runs on rules. Follow governor limits. Always write bulk-safe code. You’ll see Apex questions in Salesforce Training. They will test how well you can manage logic and system limits.

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Angry Angry 0
Sad Sad 0
Wow Wow 0