function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vinay_guptaVinay_gupta 

Trigger to prevent duplicate records and it should also work for Bulkification

Hi All,
Urgent suggestion needed!!


I am new to Salesforce and have a requirement to prevent user to create the duplicate record and it should work for bulkyfication as well.

Please find the screenshot below of the Ticket Object detail page. The requirement is that there should be validation if user try to create duplicate records for same(Customer, username,email-id) and within the period of Start date & End Date.
You can create the duplicate records if the start date & End date is before or after the existing record.

-->In order to explain this better, I want to apply a condition that user cannot create duplicate Ticket if the Start date or End date lying the in the existing record.

for ex-  if u have this entry in your DB
->NYC Concert John Smith John@gmail.com John@gmail.com 25 3/20/2018 3/25/2018
Now you tried to Enter below. It Should Fail
->NYC Concert John Smith John@gmail.com John@gmail.com 25 3/21/2018 3/25/2018  Fail
->NYC Concert John Smith John@gmail.com John@gmail.com 25 3/23/2018 3/28/2018  Fail
->NYC Concert John Smith John@gmail.com John@gmail.com 25 3/26/2018 3/27/2018  Pass
->NYC Concert John Smith John@gmail.com John@gmail.com 25 3/18/2018 3/19/2018  Pass






Key Values to check duplicacy is Username, Customer,  Email Id, StartDate, End Date
It should support bulkyfication.

Duplicate Prevention
 

Chandra@AtlantaChandra@Atlanta
Vinay,

create a trigger on Ticket object and use the below code. use can modify the code to reflect your column names and include username in the query. you can expand this to handle more logic if required.

trigger CheckDuplicateTicket on Ticket__c (Before Insert) {

for (Ticket__c NewTicket : Trigger.New) {

List <Ticket__c> DupTickets = [Select customer_name__c from Ticket__c where customer_name__c = :NewTicket.customer_name__c
                and email_address__c = :NewTicket.email_address__c
                and ( ( start_date__c <= :NewTicket.start_date__c  and  end_date__c >= :NewTicket.start_date__c )
                    )
               ];       

   if (DupTickets.size() > 0 )
   {
    /* NewTicket.Description__c = 'Duplicate'; */
    NewTicket.addError('Duplicate Ticket Found');
   }

}

}

By Falcon100 (Appreciate your feedback)
 
Abdul KhatriAbdul Khatri
Please use the below code that will work for bulkification equally.
 
trigger ticketDupCheckTrigger on Ticket__c (before insert) {

    Map<String, Ticket__c> lookupMap = new Map<String, Ticket__c>();
    Set<Id> customerSet = new Set<Id>();
    Set<String> userNameSet = new Set<String>();
    Set<String> emailSet = new Set<String>();        
    
    for(Ticket__c ticket : trigger.new) 
    {
        lookupMap.put(String.Valueof(ticket.Customer__c) + String.Valueof(ticket.User_Name__c) + String.Valueof(ticket.Email__c), ticket);   
        customerSet.add(ticket.Customer__c);
        userNameSet.add(String.Valueof(ticket.User_Name__c));
        emailSet.add(String.Valueof(ticket.Email__c));                
    }

    List<Ticket__c> ticketList = [Select Id, Customer__c, User_Name__c, Email__c From Ticket__c Where Customer__c IN :customerSet OR User_Name__c IN :userNameSet OR Email__c IN :emailSet];

    for(Ticket__c ticketRecord : ticketList) {
    
        if(lookupMap.containsKey(String.Valueof(ticketRecord .Customer__c) + String.Valueof(ticketRecord .User_Name__c) + String.Valueof(ticketRecord .Email__c))){
        
            lookupMap.get(String.Valueof(ticketRecord .Customer__c) + String.Valueof(ticketRecord .User_Name__c) + String.Valueof(ticketRecord .Email__c)).addError('Ticket already exists');
            
        }
    
    }   
    
}

 
Abdul KhatriAbdul Khatri
I have made the field names as I didn't have the exact name, so change them as per you need. Since I also didn't have the data type so I converted to string. I have tested it locally and it worked perfectly.