• Abdulla d 13
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
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 
  • June 08, 2016
  • Like
  • 0