Skip to main content

Latest Post

Agentforce: One Agent, Six Layers

 The Big Picture (Start Here) ⚡ 1-Minute Summary •   An agent understands language, decides what to do, and acts. A chatbot only answers. •   Every example in this notebook uses one company: Meridian Power. Same company, every module. •   Learn the 360 Card first (0.5). It is the shape of every good answer: rule, gain, price, limits. Module map MODULE 0 root: 'What is Agentforce, and how do the modules fit together?' ├─ 0.1 The map: one agent, six layers ├─ 0.2 Meridian Power — the example company ├─ 0.3 Key terms in plain words ├─ 0.4 The rename map (2024-2026) └─ 0.5 The 360 Card — how to answer any question 0.1   The Map: One Agent, Six Layers 💬 In plain words:   Agentforce looks like many products. It is not. It is one agent with six layers stacked under it. Each layer answers one question, and each module of this notebook lives on one layer. 📌 Example:   A customer types "my power is out." Identity decides whose dat...

How to Check for Duplicates in Salesforce using "From Date" and "To Date" with Apex Trigger

Introduction:

In Salesforce, ensuring data integrity is crucial for maintaining accurate records. One common scenario is checking for duplicates based on specific criteria. In this blog post, we'll explore how to use an Apex Trigger to identify and prevent duplicates based on the "from date" and "to date" fields.


Prerequisites:

Before proceeding, make sure you have a basic understanding of Salesforce development and Apex Triggers.


Scenario:

Let's consider a custom object called "Event" that has two date fields: "From_Date__c" and "To_Date__c". We want to prevent users from creating duplicate events where the "from date" and "to date" overlap with existing records.


Apex Trigger Code:

To achieve this, we'll write an Apex Trigger on the "Event" object that runs before the records are inserted or updated. Here's the code:


trigger PreventDuplicateEvents on Event (before insert, before update) {

    // Collect all "from date" and "to date" values from existing events

    Set<Date> existingFromDates = new Set<Date>();

    Set<Date> existingToDates = new Set<Date>();

    

    for (Event existingEvent : [SELECT From_Date__c, To_Date__c FROM Event]) {

        existingFromDates.add(existingEvent.From_Date__c);

        existingToDates.add(existingEvent.To_Date__c);

    }

    

    // Iterate through the new events to check for duplicates

    for (Event newEvent : Trigger.new) {

        if (existingFromDates.contains(newEvent.From_Date__c) || existingToDates.contains(newEvent.To_Date__c)) {

            newEvent.addError('This event overlaps with an existing record.');

        }

    }

}


Explanation:

Let's break down the code and understand how it works:


1. The trigger is defined on the "Event" object and runs before records are inserted or updated ('before insert, before update').


2. Two sets, 'existingFromDates' and 'existingToDates', are created to store the values of "from date" and "to date" from existing events.


3. A SOQL query is used to retrieve all existing events and populate the sets with their respective date values.


4. Next, we iterate through the new events in the trigger context ('Trigger.new') to check for duplicates.


5. For each new event, we use the 'contains' method to check if the "from date" or "to date" already exists in the respective sets. If a duplicate is found, we add an error message to the event using the 'addError' method.


6. If any new event has overlapping dates with existing records, the trigger prevents the insertion or update by throwing an error message.


Implementation and Testing:

To implement this trigger, follow these steps:


1. Open the Salesforce Developer Console or your preferred IDE.


2. Create a new Apex Trigger with the name "PreventDuplicateEvents" on the "Event" object.


3. Copy and paste the provided code into the trigger file.


4. Save the trigger and ensure that it's active.


5. Test the trigger by creating or updating events with overlapping "from date" and "to date" values. Verify that the trigger throws an error message and prevents the creation or update of duplicate events.


Conclusion:

By utilizing an Apex Trigger, you can enforce data integrity and prevent duplicates based on specific criteria in Salesforce. In this blog post, we explored how to check for duplicates using the "from date" and "to date" fields on the "Event" object. Remember to customize the trigger code to fit your specific requirements and object structure. With this solution, you can ensure the accuracy and consistency of your data, leading to improved data quality in your Salesforce org.

Popular Posts

Upload document using rest api from apex class salesforce

 Here is working class example to upload file from apex salesforce.

Salesforce LWC Code for Multi-Select Lookup

Introduction: In Salesforce Lightning Web Components (LWC), implementing a multi-select lookup field can enhance the user experience and provide greater flexibility for selecting multiple related records. In this blog post, we will walk through the process of creating a multi-select lookup field using LWC. We will cover the required code snippets and provide step-by-step instructions to help you implement this functionality in your Salesforce org.