+ Start a Discussion

How to write trigger to avoid duplicate records when bulkfying the trigger

i have 3 objects   Child__c is junction object , Account is master obj, Contact master object.
while createing a junction object record need to maintain uniqness its working fine
but How to write trigger to avoid duplicate records when bulkfying the trigger

my trigger is:
trigger Dupcheck on Child__c (before insert,before update) {

List<Child__C> junctionObjs = new List<Child__C>();

Set<Id> ParentAObjs = new Set<Id>();
Set<Id> ParentBObjs = new Set<Id>();

for(Child__C c:trigger.new){

JunctionObjs = [
SELECT startDate__C, endDate__C,Role__c,Child__c.physition__c,Child__c.site__c
FROM Child__C
WHERE physition__c in :parentAObjs AND
    site__c in :parentBObjs
           system.debug('i am JunctionObjs '+JunctionObjs );

for(Child__c j :JunctionObjs) {
    for(Child__c c: Trigger.new) {
        if(c.physition__c == j.physition__c && c.site__c== j.site__c && c.Role__c==j.role__c){
            if((c.EndDate__C >= j.StartDate__C && c.EndDate__C <= j.EndDate__C) ||
                       (c.StartDate__C >= j.StartDate__C && c.EndDate__C <= j.EndDate__C) ||
                       (c.StartDate__C >= j.StartDate__C && c.StartDate__C <= j.EndDate__C) ||
                       (c.StartDate__C < j.EndDate__C && c.EndDate__C > j.EndDate__C  )
                   c.addError('Youre attempting to insert a record that overlaps time-wise with an existing child object');


this is working fine when inserting single records but it is not working when inserting morer than one record like using dat loader i am inserting 10 record .. please help me on this 
Amit Chaudhary 8Amit Chaudhary 8
What error you are getting while you uploading 10 record from data loader ?
Your trigger code look good to me.
not geting error ... it allowing to save duplicate records with out error
Amit Chaudhary 8Amit Chaudhary 8
I hope you are adding all 10 duplicate record from data loader only. your code will give duplicate error only when some record are already
exists in SFDC and same you will try to upload by data loader
yes .. you are right .. but y requirment is lets say we dont have nay reords in database and i am inserting  5 records with duplicate values using data loder 

record1: xxx
record 2:xxx
record 3:xxx
record 4: yyy
record 5 : yyy

as we dont have records in Databse and we are inserting duplicate data it should give error message like you are inserting duplicate records .. 
Abdulla d 13Abdulla d 13
Hi guys this is the perfect solution for this to avoid the duplicate records when you perform the bulk operations, such as data load or through some script...

        id leadrecordTypid=Schema.SObjectType.lead.getRecordTypeInfosByName().get('Test lead').getRecordTypeId();
        set<String> emailset=new Set<String>();
        Map<String,Decimal> emailsetmap=new Map<String,Decimal>();
        for(Lead l:trigger.new){
        List<Lead> lstExistingLead = [Select id,Email from Lead where email in :emailset and RecordTypeId=:leadrecordTypid];
        if(emailset.size() > 0 )
            List<Lead> leadlist = [select email ,id from Lead where email in :emailset and RecordTypeId=:leadrecordTypid];        
            Map<String ,Lead> mapEmailWiseLead = new Map<String,Lead>();
            for(Lead ld: leadlist){
            for(Lead lead : trigger.new){
                    lead.Email.addError('Email already Existed ');
                    lead.Email.addError('Duplicate Email Detected ');