+ Start a Discussion
Rohan ChadhaRohan Chadha 

Write trigger to prevent Duplicate records entered by user and it should prevent the bulkyfication

Hi All,

I am new to salesforce and have  requirment to prevent user to create duplicate record and it should work for bulkyfication as well.

Please find the screenshot bellow 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 withing 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.
  • Key Values to check duplicacy is Username, Customer and Email Id.
  • It should support bulkyfication.

Kindly help with the code.


User-added image

HARSHIL PARIKH AKA:GovindHARSHIL PARIKH AKA:Govind
Hi Rohan,

I would suggest the following approach.

1) Create a field named Formula__c on this Tickit__c object which will sumup all the values.
Formula__c = TEXT(Concert__c) + " "  + TEXT(Customer__c) + " " + Email__c + " " + User_Name__c + " " + TEXT(Number_of_Tickits__c) + " " + TEXT(End_date__c) + " " + TEXT(Start_date__c)
You may need to search a little about concerting date into text but however, try to grab all the required fields text values into Foirmul__c field.

Your final output of text field can look like this:L Formula = NYC Concert John Smith John@gmail.com John@gmail.com 25 3/20/2018 3/21/2018
You can of course eliminate some of the fields if you like based on your requirements as well.

2) Make a use of below trigger:
 
Trigger DupRecordNotAllowed on Tickit__C (Before Insert, Before Update, After UnDelete) {
	
    List<String> allIdenticalFormulas = New List<String>();
    List<Tickit__c> comingTickitRecords = New List<Tickit__c>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUndelete){
        For(Tickit__c tickit : Trigger.New){
            If(tickit.Formula__c != NULL){
                allIdenticalFormulas.add(tickit.Formula__c);
                comingTickitRecords.add(tickit);
            }
        }
    }
    
    List<Tickit__c> similarTickitRecords = [Select Id, Formula__c FROM Tickit__c WHERE Formula__c =: allIdenticalFormulas 
                                            										AND Id !=: comingTickitRecords];
    
    If(similarTickitRecords.size() > 0){
        For(Tickit__c EveryTickit : comingTickitRecords)
        {
            For(Tickit__c EverySimilarTickit : similarTickitRecords){
                If(EverySimilarTickit.Formula__c == EveryTickit.Formula__c){
                    EveryTickit.addError('Similar Record Exists!');
                }
            }
        }
    }
}
Hope this helps and if this solves the query then please mark is as best answer!

 
Rohan ChadhaRohan Chadha
Hi Harshil,
Thanks for your quick response.

I tried to progress on this and wrote the below the code. but i am failing to put condition on End Date and Start Date.
-->I want to apply 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

Kindly help
 
Below code is failing with Error: Compile Error: Variable does not exist: Start_Date__c 

trigger duplicat on Ticket__c (before insert,before update) {
        for(Ticket__c a:Trigger.new)
        {
            List<Ticket__c> tickt1 =[select User_Name__c,Customer__c,Start_Date__c, End_Date__c from Ticket__c where User_Name__c=:a.User_Name__c and Customer__c=:a.Customer__c];
            if(tickt1.size()>0)
            {
         for(Ticket__c b: tickt1 )
                 {
               if(b.Start_Date__c  tickt1.Start_Date__c || b.Start_Date__c <=tickt1 .End_Date__c ||
                 b.End_Date__c >=tickt1 .Start_Date__c || b.End_Date__c <=tickt1 .End_Date__c )
             
                a.adderror('You cannot create a dulplicate account');
               }
            }
        }
    }

This Code is working fine but not doing date Validation
trigger duplicat on Ticket__c (before insert,before update) {
        for(Ticket__c a:Trigger.new)
        {
            List<Ticket__c> tickt1 =[select User_Name__c,Customer__c,Start_Date__c, End_Date__c from Ticket__c where User_Name__c=:a.User_Name__c and Customer__c=:a.Customer__c];
            if(tickt1.size()>0)
            {
           
                a.adderror('You cannot create a dulplicate account');
                  }
            }
        }
    }