Lesson 4: Advanced Salesforce Development Features

Lesson 4: Advanced Salesforce Development Features


1. Salesforce Flow Integration

  • Definition: Salesforce Flow automates processes with drag-and-drop logic.

  • Use: Simplify complex automation without code.

  • Example: Flow to auto-update Opportunity Stage based on probability.

trigger OpportunityStageUpdate on Opportunity (before insert, before update) {
    for (Opportunity opp : Trigger.new) {
        if (opp.Probability >= 75) opp.StageName = 'Closed - Won';
        else if (opp.Probability < 25) opp.StageName = 'Prospecting';
    }
}

Explanation:

  1. Trigger.new provides opportunities being modified.

  2. Logic updates StageName based on Probability.

  3. Flow and trigger ensure seamless updates.


2. Platform Events

  • Definition: Platform Events facilitate real-time integration by streaming events to external systems.

  • Use: Notify systems of Salesforce changes.

  • Example: Publish an event when a new Contact is created.

trigger PublishContactEvent on Contact (after insert) {
    for (Contact con : Trigger.new) {
        Contact_Created_Event__e event = new Contact_Created_Event__e(ContactId__c = con.Id, Name__c = con.Name);
        EventBus.publish(event);
    }
}

Explanation:

  1. Contact_Created_Event__e defines the custom event.

  2. EventBus.publish broadcasts it for subscribers.

  3. External systems receive real-time updates.


3. Asynchronous Apex (Future Methods)

  • Definition: Future methods execute code asynchronously, improving user performance.

  • Use: Process tasks in the background.

  • Example: Send emails after inserting Contacts.

public class ContactNotifier {
    @future
    public static void sendNotificationEmails(List<Id> contactIds) {
        List<Contact> contacts = [SELECT Name, Email FROM Contact WHERE Id IN :contactIds];
        for (Contact con : contacts) {
            // Mock sending email logic
            System.debug('Sending email to: ' + con.Email);
        }
    }
}

Explanation:

  1. @future marks the method for asynchronous execution.

  2. SOQL retrieves relevant contact details.

  3. Mock email logic processes data efficiently.


Summary

  • Salesforce Flow Integration: Automate processes with minimal code.

  • Platform Events: Enable real-time external system notifications.

  • Asynchronous Apex: Execute background tasks for better performance.