You need to sign in to do that
Don't have an account?
ranga 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. ');
}
}
}
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. ');
}
}
}
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
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
1.Best Code & Small
2.Flexible
3.Bulkify
4.scalability
Try it once.... n hit like if u like it