Shopping cart

Subtotal:

$0.00

PDI Process Automation and Logic

Process Automation and Logic

Detailed list of PDI knowledge points

Process Automation and Logic Detailed Explanation

Salesforce provides tools for automating business processes, either declaratively (without code) or programmatically (using code). These tools allow you to meet various business requirements, from simple field updates to advanced workflows and asynchronous operations.

2.1 Declarative Automation

Declarative tools allow you to automate processes without writing code. They are user-friendly and ideal for many business scenarios.

1. Process Builder

  • What is Process Builder?
    • A declarative tool for building simple business logic processes.
    • Found in Setup > Process Builder.
  • Key Features:
    • Field Updates: Automatically update a record’s field when a condition is met.
    • Email Notifications: Send emails based on record changes.
    • Create Records: Create related records as part of the process.
    • Call Flow or Apex: Trigger a flow or Apex code for more complex actions.
  • Use Cases:
    • Automatically change an Account's status to "Active" when the first Opportunity is closed.
    • Notify a sales rep when an Opportunity's amount exceeds $50,000.

2. Flow Builder

  • What is Flow Builder?
    • A powerful tool for building complex workflows, forms, and automations.
    • Found in Setup > Flows.
  • Key Features:
    • Dynamic Forms: Display forms to collect user input.
    • Multi-Step Processes: Guide users through multiple steps.
    • Integration: Call Apex classes, send outbound messages, or query data using SOQL.
    • Loops and Decisions: Perform repeated actions (e.g., update multiple records) and make decisions based on conditions.
  • Use Cases:
    • Automate onboarding by guiding users through a multi-step employee setup process.
    • Automatically calculate discounts and apply them to related records.

3. Approval Processes

  • What is an Approval Process?
    • A tool for automating multi-step approval workflows.
    • Found in Setup > Approval Processes.
  • Key Features:
    • Steps: Define who approves each step in the process.
    • Actions:
      • Field Updates: Change a field's value when approved or rejected.
      • Notifications: Send emails to approvers.
  • Use Cases:
    • Approve discount requests over 20%.
    • Approve job offers before sending them to candidates.

2.2 Programmatic Automation

Programmatic automation is used when declarative tools can’t meet the requirements. It provides advanced flexibility through coding.

Triggers

Triggers are pieces of code that execute automatically in response to database events, such as inserting, updating, or deleting records.

  • Trigger Events:
    1. Before Events:
      • Logic runs before a record is saved to the database.
      • Common Use: Data validation or auto-populating fields.
    2. After Events:
      • Logic runs after a record is saved.
      • Common Use: Updating related records or sending emails.
Example Trigger

The following trigger updates the Description field of a Contact record before it is saved:

trigger ContactTrigger on Contact (before insert) {
    for (Contact con : Trigger.new) {
        con.Description = 'Welcome, ' + con.FirstName;
    }
}

Explanation:

  1. Trigger.new: A list of records being processed.
  2. The trigger iterates through each new Contact and updates its Description field.

Best Practices:

  • Avoid recursion by using static variables.
  • Use one trigger per object for simplicity and maintainability.

Asynchronous Tasks

Asynchronous tasks handle time-consuming processes, such as processing large data sets or calling external APIs, without blocking the user.

1. Future Methods
  • What are Future Methods?

    • A lightweight way to run code asynchronously in the background.
    • Common Use: Callouts to external web services or operations that don’t need an immediate result.
  • Example:

    @future
    public static void sendEmail(String email) {
        // Logic to send email
        System.debug('Sending email to ' + email);
    }
    
  • Best Practices:

    • Cannot return results.
    • Should be used sparingly; Queueable Apex is often preferred for chaining operations.
2. Batch Apex
  • What is Batch Apex?

    • Designed to process large volumes of records in chunks (up to 200 records per batch).
    • Common Use: Perform data cleanup, complex calculations, or updates on thousands of records.
  • Key Components:

    • start: Retrieves the data to be processed.
    • execute: Processes each batch of data.
    • finish: Executes post-processing logic.
  • Example:

    global class BatchExample implements Database.Batchable<sObject> {
        global Database.QueryLocator start(Database.BatchableContext context) {
            return Database.getQueryLocator('SELECT Id FROM Account');
        }
        global void execute(Database.BatchableContext context, List<Account> scope) {
            for (Account acc : scope) {
                acc.Description = 'Processed in batch';
            }
            update scope;
        }
        global void finish(Database.BatchableContext context) {
            System.debug('Batch processing completed.');
        }
    }
    
3. Queueable Apex
  • What is Queueable Apex?

    • An enhancement of Future Methods, allowing chaining of multiple asynchronous operations.
  • Features:

    • Can pass complex objects as parameters.
    • Allows jobs to be linked sequentially.
  • Example:

    public class QueueableExample implements Queueable {
        public void execute(QueueableContext context) {
            System.debug('Executing asynchronous job');
        }
    }
    
4. Scheduled Jobs
  • What are Scheduled Jobs?

    • Executes recurring tasks at specific times using the Schedulable interface.
  • Use Cases:

    • Nightly data backups.
    • Weekly recalculations of metrics.
  • Example:

    global class ScheduledJob implements Schedulable {
        global void execute(SchedulableContext sc) {
            System.debug('Scheduled job executed');
        }
    }
    
    • To schedule:

      ScheduledJob job = new ScheduledJob();
      String cron = '0 0 12 * * ?'; // Noon daily
      System.schedule('Daily Job', cron, job);
      

Summary

Declarative automation tools are great for straightforward processes, while programmatic automation provides the power to handle complex or large-scale tasks. Combining these approaches ensures you can automate virtually any business scenario in Salesforce effectively.

Process Automation and Logic (Additional Content)

1. Workflow Rules

What are Workflow Rules?

  • Workflow Rules are an older automation tool in Salesforce, accessible via Setup > Workflow Rules.
  • They allow simple automation of record updates and notifications without coding.
  • Limited functionality compared to newer automation tools like Process Builder and Flow.

Capabilities of Workflow Rules

Can do:

  • Send email alerts.
  • Update a field’s value.
  • Create tasks.
  • Send outbound messages.

Cannot do:

  • Create new records (use Flow instead).
  • Call Apex code (use Flow or Triggers).
  • Perform multiple actions based on complex conditions (Process Builder or Flow is required).

Workflow Rules vs. Process Builder

Feature Workflow Rules Process Builder
Multiple Actions per Rule No Yes
Create New Records No Yes
Call Apex Code No Yes
Scheduled Actions Yes Yes
Future Support Being phased out Being phased out (Use Flow instead)

Key Takeaways

  • Workflow Rules are becoming obsolete—Salesforce recommends Flow for all new automation.
  • If a requirement only needs a field update, email, or task assignment, Workflow Rules might still work.
  • If multiple actions are needed or logic is complex, Flow is the preferred choice.

2. Best Practices: Process Builder vs. Flow

Salesforce is deprecating Process Builder in favor of Flow because Flow is more powerful and scalable.

When to Use Flow?

If the process requires multiple steps or decisionsUse Flow
If the process needs to interact with multiple objectsUse Flow
If user input is required (e.g., a guided screen flow)Use Flow
If automation needs to be migrated from Workflow or Process BuilderUse Flow

When to Use Apex?

If the process requires complex calculations or loopsUse Apex
If the process requires external API calls (HTTP Callouts)Use Apex
If the automation needs to run asynchronously (background jobs)Use Apex

Scenario Use Flow? Use Apex?
Simple field update Yes No
Create or update related records Yes No
Work across multiple objects Yes No
Perform external API calls No Yes
High-performance batch processing No Yes

Key Takeaways

  • New automation should always be built with Flow instead of Process Builder.
  • Flow is declarative and easier to maintain, while Apex is for complex scenarios.
  • If Flow can handle a use case, avoid using Apex (recommended by Salesforce).

3. Trigger Framework (Best Practices for Apex Triggers)

A Trigger Framework is a best practice for managing triggers in large applications.

Why use a Trigger Framework?

  • Prevents recursion issues (multiple triggers on the same object can cause infinite loops).
  • Separates business logic from the trigger, making it modular and reusable.
  • Ensures a single trigger per object, improving maintainability.

Example: Using a Trigger Handler Class

Trigger Code (Minimal Logic)

trigger AccountTrigger on Account (before insert, after update) {
    AccountTriggerHandler.handleTrigger(Trigger.new, Trigger.oldMap);
}

Handler Class (Encapsulated Logic)

public class AccountTriggerHandler {
    public static void handleTrigger(List<Account> newRecords, Map<Id, Account> oldRecords) {
        for (Account acc : newRecords) {
            acc.Description = 'Handled by Trigger Framework';
        }
    }
}

How to Prevent Recursive Trigger Execution?

  • Use static variables to track execution state.

  • Example:

    public class PreventRecursion {
        private static Boolean isTriggerRunning = false;
    
        public static void executeTriggerLogic() {
            if (isTriggerRunning) return;
            isTriggerRunning = true;
    
            // Trigger logic here...
    
            isTriggerRunning = false;
        }
    }
    

Key Takeaways

  • Use a single trigger per object and delegate logic to a handler class.
  • Prevent recursion using static variables.
  • Improve maintainability by keeping business logic out of triggers.

4. Flow vs. Apex – When to Use Each?

Flow is a declarative tool, while Apex provides advanced control over logic. Choosing between them depends on complexity and performance needs.

Scenario Use Flow? Use Apex?
Simple record updates Yes No
Automate approvals Yes No
Perform API callouts No Yes
Perform bulk processing (batch jobs) No Yes
Work with complex data structures No Yes

Best Practice

  • If Flow can accomplish the task, use Flow instead of Apex.
  • Use Apex only for tasks that Flow cannot handle efficiently, such as complex calculations or API integrations.

5. Asynchronous Apex – Choosing the Right Approach

Salesforce provides four main asynchronous execution methods: @future, Queueable, Batch Apex, and Scheduled Apex.

Comparison Table: Asynchronous Apex Methods

Method Use Case Supports Chaining? Supports Complex Objects?
@future Lightweight, short-lived async jobs (e.g., API callouts) No No
Queueable Similar to @future, but supports chaining and complex objects Yes Yes
Batch Apex Process large datasets (e.g., bulk updates) Yes No
Scheduled Run jobs at specific times (e.g., nightly cleanups) Yes No

Examples

Using @future for API Callouts

public class AsyncExample {
    @future(callout=true)
    public static void callExternalAPI() {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://example.com/api');
        request.setMethod('GET');
        HttpResponse response = http.send(request);
    }
}

Using Queueable for Complex Async Processing

public class QueueableExample implements Queueable {
    public void execute(QueueableContext context) {
        System.debug('Executing Queueable Job...');
    }
}
System.enqueueJob(new QueueableExample());

Using Batch Apex for Large Data Processing

global class BatchExample implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('SELECT Id FROM Account');
    }
    global void execute(Database.BatchableContext bc, List<Account> scope) {
        for (Account acc : scope) {
            acc.Name += ' - Updated';
        }
        update scope;
    }
}
Database.executeBatch(new BatchExample());

Key Takeaways

  • For simple async tasks → Use @future (but limited functionality).
  • For complex objects and chaining → Use Queueable Apex.
  • For large-scale processing → Use Batch Apex.
  • For scheduled tasks → Use Scheduled Apex.

Frequently Asked Questions

When should a developer choose Apex triggers instead of record-triggered flows?

Answer:

Apex triggers should be used when automation requires complex logic, cross-object operations, or large-scale processing beyond Flow capabilities.

Explanation:

Record-triggered flows can handle many automation scenarios such as field updates, sending emails, and simple logic. However, Apex triggers are more suitable when developers need complex algorithms, advanced error handling, integration with external services, or highly optimized bulk processing.

For example, if a requirement involves calculating values across hundreds of related records or integrating with a third-party API, Apex provides greater control and scalability. Salesforce recommends using declarative tools like Flow first, but when the logic becomes too complex or performance-critical, Apex triggers become the better solution.

Understanding when to choose Flow versus Apex is a frequent scenario-based question in the PDI exam.

Demand Score: 90

Exam Relevance Score: 94

What is the main purpose of bulkifying an Apex trigger?

Answer:

Bulkification ensures the trigger can process multiple records efficiently in a single transaction without exceeding governor limits.

Explanation:

Salesforce processes records in batches, often up to 200 records at a time when operations occur through the API or data imports. If a trigger is written to handle only one record at a time, it may fail when processing larger batches.

Bulkified triggers operate on collections instead of individual records. Developers use lists, sets, and maps to process multiple records simultaneously and reduce the number of SOQL queries and DML operations.

For example, instead of querying related records inside a loop, developers collect IDs into a set and perform one query outside the loop. This approach avoids hitting limits such as the maximum number of queries per transaction.

Bulkification is considered one of the most critical Apex development practices tested in the PDI certification.

Demand Score: 92

Exam Relevance Score: 96

Why can a trigger run multiple times during a single transaction?

Answer:

Because automation tools like workflow rules or flows can update the same record again, causing triggers to fire repeatedly.

Explanation:

In Salesforce’s order of execution, certain automation tools may modify a record after triggers have already executed. For example, a workflow field update or Flow action may change a field value, which causes Salesforce to re-evaluate triggers for that record within the same transaction.

If developers do not design triggers carefully, this can lead to recursion, where the same logic runs repeatedly and may update records indefinitely. This behavior can cause governor limit exceptions or unintended data changes.

To prevent this, developers often use recursion control patterns such as static Boolean variables or sets that track processed record IDs. Understanding trigger recursion is important for debugging automation conflicts in real projects and appears frequently in certification scenarios.

Demand Score: 84

Exam Relevance Score: 90

What is the difference between a before trigger and an after trigger in Apex?

Answer:

Before triggers are used to modify record values before they are saved, while after triggers run after the record is committed to the database.

Explanation:

Before triggers allow developers to change field values directly on the records being saved without performing additional DML operations. This makes them efficient for tasks like populating calculated fields or enforcing business logic before data is stored.

After triggers execute once the record has already been saved. They are commonly used when developers need record IDs or must create or update related records in other objects.

Choosing the correct trigger type is important because some operations are only possible in specific contexts. For example, modifying values on Trigger.new is allowed in before triggers but not in after triggers.

Understanding the appropriate trigger context helps developers design efficient automation and avoid unnecessary database operations.

Demand Score: 80

Exam Relevance Score: 88

Why should developers avoid having multiple triggers on the same object?

Answer:

Multiple triggers on the same object can create unpredictable execution order and make maintenance more difficult.

Explanation:

Salesforce does not guarantee the order in which multiple triggers on the same object execute. If several triggers perform related logic, they may interfere with each other or produce inconsistent results depending on which runs first.

To avoid this problem, developers typically implement a single trigger per object pattern. This trigger acts as an entry point that calls handler classes responsible for different pieces of logic.

Using a structured trigger framework improves readability, maintainability, and testability of the code. It also ensures that developers can control the order of execution within the handler class rather than relying on unpredictable system behavior.

This design pattern is widely recommended in Salesforce development best practices.

Demand Score: 77

Exam Relevance Score: 85

Why might a record-triggered Flow update fail when processing large data loads?

Answer:

Because flows can still encounter governor limits when handling large batches of records.

Explanation:

Although Flows are declarative tools, they still operate within Salesforce’s governor limits. If a record-triggered flow performs many queries, loops, or record updates during bulk operations, it can exceed limits such as CPU time or database operations.

For example, a flow that queries related records inside a loop may work fine for one record but fail when processing hundreds during a data import. Developers must design flows with bulk processing in mind, similar to Apex triggers.

Best practices include reducing loops, retrieving data once, and using collection operations whenever possible. Understanding how declarative automation interacts with platform limits is a common troubleshooting topic for Salesforce developers and often appears in certification questions.

Demand Score: 81

Exam Relevance Score: 87

PDI Training Course