I have a Custom Object A with start date and end date. It should insert weekly records in Custom Object B and Daily records in Custom Object C and should populate the Custom OBject B in it.
- I have used after insert on Custom Object A which will insert Custom Object B and C.
- written trigger on Custom Object B which will update the ID on Custom Object C.
Here both triggers are getting fired on the same object C.
Trigger TriggerInsertTime on Custom_Object A (after insert) {
List<CustomObjectA> CustomObjectAlist = new List<CustomObjectA>()
List<CustomObjectB> CustomObjectBlist = new List<CustomObjectB>()
List<CustomObjectC> CustomObjectClist = new List<CustomObjectC>()
for(Custom Object A a : trigger.New){
CustomObjectAlist .add(a);
}
for(CustomObjectA a1:CustomObjectAlist){
CustomObjectB = new CustomObjectB();
for(Integer i=0;i<3;i++){
b.a1 = a1.id;
b.stdt = a1.startdate;
b.enddt = a1. enddate;}
CustomObjectBlist.add(b);
CustomObjectC c = new CustomObjectC();
Integer numberDaysDue = a1.startdate.daysBetween(EndDate);
for(Integer j=0;j<numberDaysDue;j++){
c.Date__c = stdt.addDays(j);
c.a1 = a1.id;
CustomObjectClist .add(c);
}
insert CustomObjectBlist;
insert CustomObjectClist;
}
After insert both objects I want to update CustomObjectB Id in CustomObject C.
trigger Updatefield on CustomObjectB(after insert)
List<CustomObject C> updatetimeperiod = new List<CustomObject C>();
for(CustomObjectB t2: tlist){
id vid = t2.a1;
for(CustomObject C tp1: [select id,date__c from CustomObject C where
t2 =:vid]){
if(tp1.date__c>=t2.start_Date__c && tp1.date__c<=t2.End_Date__c){
system.debug('date'+tp1.date__c);
tp1.Time__c = t2.id;
system.debug('tp1'+tp1.Time__c);
updatetimeperiod.add(tp1);
}
}}
if(updatetimeperiod.size()>0){
system.debug('updatetimeperiod.size()'+updatetimeperiod.size());
Update updatetimeperiod;}
when I insert and update on CustomObjectC at the same time am getting the following error:
getmessageBefore Insert or Upsert list must not have two identically equal elements
How to avoid trigger to fire at the same time. Is there anyway i can update after insert like can I use database.insert.Can anyone please help me on this issue?