You need to sign in to do that
Don't have an account?

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
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
Your trigger code look good to me.
exists in SFDC and same you will try to upload by data loader
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 ..
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 ');
}
}
}
}