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
HareHare 

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){
    parentAObjs.add(c.physition__c);
    parentBObjs.add(c.site__c);
}

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.
HareHare
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
 
HareHare
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...


if(trigger.isinsert&&trigger.isBefore){
        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){
            emailset.add(l.email);            
            if(emailsetmap.containsKey(l.Email))
            {
                emailsetmap.put(l.Email,(emailsetmap.get(l.Email)+1));                
            }else{
                emailsetmap.put(l.Email,0);
            }
        }
        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){
                mapEmailWiseLead.put(ld.email,ld);
            } 
            for(Lead lead : trigger.new){
                if(mapEmailWiseLead.containsKey(lead.Email))
                {
                    lead.Email.addError('Email already Existed ');
                }
                if(emailsetmap.get(lead.Email)>0)
                {
                    lead.Email.addError('Duplicate Email Detected ');
                }                
            }
            
        }
    }