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.

  1. I have used after insert on Custom Object A which will insert Custom Object B and C.
  2. 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;}
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){
                                           tp1.Time__c = t2.id;
                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?

  • Welcome to SFSE. It will be much easier to assist you if you (1) format your code so that it is easier to follow (use Prettier or even Developer Console), and make sure it is syntactically valid and (2) indicate which line raises the error and give the entire error message verbatim. You can edit to make changes.
    – David Reed
    Commented Oct 24, 2020 at 3:34

1 Answer 1


You're literally adding the same object instance to your insert list over and over. Here's the relevant lines for Custom Object C, although you do this for both B and C:

CustomObjectC c = new CustomObjectC();
for (Integer j=0; j < numberDaysDue; j++) {

insert CustomObjectClist;

CustomObjectClist consists of the same object instance repeated j times, because you create the instance outside the loop and never reinitialize it. The instance creation should be inside the loop if you wish to create multiple records.

You'll need to correct this for both B and C. There are multiple problems here, including an un-bulkified query in your B trigger, but that's the proximate issue.


Your query,

select id,date__c from CustomObject C where t2 =:vid

matches nothing because you're querying for child objects of Custom Object B while in a Custom Object B after insert trigger. It is not possible for an object to have children in its own after insert context as the object itself has not yet been committed.

I don't have a good understanding of what you're doing here but I suspect your Object B trigger really should be on Object C.

  • Hi David,Is there any alternate approach to achieve this?
    – Narsari
    Commented Oct 26, 2020 at 23:32
  • @Narsari You do not need an alternate approach; you need to correct the errors in this Apex.
    – David Reed
    Commented Oct 26, 2020 at 23:34
  • Thanks for the correcting me. Here it is not entering into the second loop in CustomObjectB. Please let me know where I am doing wrong.
    – Narsari
    Commented Oct 27, 2020 at 16:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .