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
ranga babu vangalapudi 2ranga babu vangalapudi 2 

Trigger to avoid duplicate records when ever user trying to insert records using Dataloader

I have a parent object and child object, in which a parent record shouldn't have duplicate child records.the following trigger is working fine but it is checking only the records in the database.

But my issue is? user is inserting two duplicate records at a time using .csv file and since my trigger is only validating the records in the database.
hence these duplicate records are also getting inserted.

How can I check duplicate in the .csv file itself ? or how to overcome this issue.

Can any one help me on this.
Trigger:
trigger BeatNameDuplicateCheck on Beat_TSE_Mapping__c (before insert, before update) {
        set<Id>TSEID= new set<ID>();
        set<string>bitname=new set<string>();
        map<string,Beat_TSE_Mapping__c> bitmap= new map<string,Beat_TSE_Mapping__c> ();
               for(Beat_TSE_Mapping__c beat:Trigger.new)
        {
            TSEID.add(beat.TSE_Code__c);
            bitname.add(beat.Beat_Name__c); 
        }
        if (!TSEID.isEmpty() ){
             List<Beat_TSE_Mapping__c> TSElist=new List<Beat_TSE_Mapping__c>();
        TSElist=[select id,Beat_Name__c,TSE_Code__c from Beat_TSE_Mapping__c where Beat_Name__c In: bitname and TSE_Code__c In: TSEID];
                       
        if (TSElist.size()>0) {
            for(Beat_TSE_Mapping__c beat:TSElist)
            {
              bitmap.put(beat.Beat_Name__c, beat);
            }     
        } 
                  }
        for (Beat_TSE_Mapping__c beat:Trigger.new)
                    {
            If (bitmap.containsKey(beat.Beat_Name__c ))
            {
                beat.Beat_Name__c.AddError('Beat Name Already Exist. Please try with another BeatName. ');
           }
        } 
}
Best Answer chosen by ranga babu vangalapudi 2
DeveloperSudDeveloperSud
Hi ,

The apex trigger will not work for raw data present in CSV fille.It will only work with data present in salesforce database.If you want to check duplicate before inserting records using dataloader use any duplicate checker apps.
I am giving a link of an app as example you can refer that.
https://appexchange.salesforce.com/appxListingDetail?listingId=a0N300000016cMzEAI

All Answers

DeveloperSudDeveloperSud
Hi ,

The apex trigger will not work for raw data present in CSV fille.It will only work with data present in salesforce database.If you want to check duplicate before inserting records using dataloader use any duplicate checker apps.
I am giving a link of an app as example you can refer that.
https://appexchange.salesforce.com/appxListingDetail?listingId=a0N300000016cMzEAI
This was selected as the best answer
Vishal ThubeVishal Thube
Hi, Below is my code for the trigger for prevention duplicate Name in the account . 
1.Best Code & Small
2.Flexible
3.Bulkify
4.scalability

Try it once.... n hit like if u like it
trigger PreventDuplicateAccName on Account (before insert,before Update ){
    Set <string> accSet = new Set <String>();
        
    for (Account objAcc : trigger.new){
         if (accSet.contains(objAcc.Name)){
            objAcc.addError('Account Found......');
        }
        accSet.add(objAcc.Name);   
    }   
    
    List <Account> accList = new List <Account>();
        for (Account objAcc : [select id, Name from Account where Name In : accSet]){
            accList.add(objAcc);
            
            if (accSet.equals(accList)){
                objAcc.addError('Record Alreday Found ');
            }   
    	}       
}