+ Start a Discussion
HamptonHampton 

Help With Creation of Child Record

Hello:

 

I am having issues with an Apex trigger designed to create a child records (Rep_Summary__c) after update of parent record (Video_Turf__c) where certain conditions are met.

 

Here is my trigger:

 

trigger RepSummaryCreation on Video_Turf__c (after update) {

    List<Rep_Summary__c> newSummary = new List<Rep_Summary__c>();
    for(Video_Turf__c a: trigger.new){
        if(a.Assigned__c==TRUE && a.Rep_One__c!=NULL){
            newSummary.add(new Rep_Summary__c(
                  Date__c = a.Date__c,
                  Release__c = a.ID,
                  Doors__c = a.Doors_Per_Rep__c,
                  Name = a.Rep_One__c));
        }
       }
  
    insert newSummary;
    
    List<Rep_Summary__c> newSummary2 = new List<Rep_Summary__c>();
    for(Video_Turf__c b: trigger.new){
        if(b.Assigned__c==TRUE && b.Rep_Two__c!=NULL){
            newSummary2.add(new Rep_Summary__c(
                  Date__c = b.Date__c,
                  Release__c = b.ID,
                  Doors__c = b.Doors_Per_Rep__c,
                  Name = b.Rep_Two__c));
        }
       }
  
    insert newSummary2;
    
    List<Rep_Summary__c> newSummary3 = new List<Rep_Summary__c>();
    for(Video_Turf__c c: trigger.new){
        if(c.Assigned__c==TRUE && c.Rep_Three__c!=''){
            newSummary3.add(new Rep_Summary__c(
                  Date__c = c.Date__c,
                  Release__c = c.ID,
                  Doors__c = c.Doors_Per_Rep__c,
                  Name = c.Rep_Three__c));
    }
   }
  
    insert newSummary3;
    
    List<Rep_Summary__c> newSummary4 = new List<Rep_Summary__c>();
    for(Video_Turf__c d: trigger.new){
        if(d.Assigned__c==TRUE && d.Rep_Four__c!=''){
            newSummary4.add(new Rep_Summary__c(
                  Date__c = d.Date__c,
                  Release__c = d.ID,
                  Doors__c = d.Doors_Per_Rep__c,
                  Name = d.Rep_Four__c));
    }
   }
  
    insert newSummary4;
    
    List<Rep_Summary__c> newSummary5 = new List<Rep_Summary__c>();
    for(Video_Turf__c e: trigger.new){
        if(e.Assigned__c==TRUE && e.Rep_Five__c!=''){
            newSummary5.add(new Rep_Summary__c(
                  Date__c = e.Date__c,
                  Release__c = e.ID,
                  Doors__c = e.Doors_Per_Rep__c,
                  Name = e.Rep_Five__c));
    }
   }
  
    insert newSummary5;
    
    List<Rep_Summary__c> newSummary6 = new List<Rep_Summary__c>();
    for(Video_Turf__c f: trigger.new){
        if(f.Assigned__c==TRUE && f.Rep_Six__c!=''){
            newSummary6.add(new Rep_Summary__c(
                  Date__c = f.Date__c,
                  Release__c = f.ID,
                  Doors__c = f.Doors_Per_Rep__c,
                  Name = f.Rep_Six__c));
    }
   }
  
    insert newSummary6;
    
    List<Rep_Summary__c> newSummary7 = new List<Rep_Summary__c>();
    for(Video_Turf__c g: trigger.new){
        if(g.Assigned__c==TRUE && g.Rep_Seven__c!=''){
            newSummary7.add(new Rep_Summary__c(
                  Date__c = g.Date__c,
                  Release__c = g.ID,
                  Doors__c = g.Doors_Per_Rep__c,
                  Name = g.Rep_Seven__c));
    }
   }
  
    insert newSummary7;
    
    List<Rep_Summary__c> newSummary8 = new List<Rep_Summary__c>();
    for(Video_Turf__c h: trigger.new){
        if(h.Assigned__c==TRUE && h.Rep_One__c!=''){
            newSummary8.add(new Rep_Summary__c(
                  Date__c = h.Date__c,
                  Release__c = h.ID,
                  Doors__c = h.Doors_Per_Rep__c,
                  Name = h.Rep_Eight__c));
    }
   }
  
    insert newSummary8;
    
    List<Rep_Summary__c> newSummary9 = new List<Rep_Summary__c>();
    for(Video_Turf__c i: trigger.new){
        if(i.Assigned__c==TRUE && i.Rep_Nine__c!=''){
            newSummary9.add(new Rep_Summary__c(
                  Date__c = i.Date__c,
                  Release__c = i.ID,
                  Doors__c = i.Doors_Per_Rep__c,
                  Name = i.Rep_Nine__c));
    }
   }
  
    insert newSummary9;
    
    List<Rep_Summary__c> newSummary10 = new List<Rep_Summary__c>();
    for(Video_Turf__c j: trigger.new){
        if(j.Assigned__c==TRUE && j.Rep_Ten__c!=''){
            newSummary10.add(new Rep_Summary__c(
                  Date__c = j.Date__c,
                  Release__c = j.ID,
                  Doors__c = j.Doors_Per_Rep__c,
                  Name = j.Rep_Ten__c));
    }
   }
    insert newSummary10;
                                        
}

 This seems to be a fairly straight forward trigger. What am I missing here that is not firing to create the records?

 

Thanks,

 

Hampton

SrinuSrinu

Hi Hampton,

 

Can you try with 'before update' event instead of 'after update' event.

 

Few points needs to be take care:

 

is it Active?

Is it written in current version? If not, can u try with API  Version 29.0. jus trails..

 

 

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi Hampton,

 

I'm wondering on seeing this much of for loop you are using in a trigger. Your conditions are on same object right? So, don't need of creating for loop for each condition. Trigger is going to be in one object only.

 

In this case Assigned__c == true condition is common, so I have done some alteration in the code.

 

Try the following trigger,

trigger RepSummaryCreation on Video_Turf__c (after update) {
    List<Rep_Summary__c> newSummary = new List<Rep_Summary__c>();       
    for(Video_Turf__c a: trigger.new){
        if(a.Assigned__c == TRUE){
          if(a.Rep_One__c!=NULL){
            newSummary.add(new Rep_Summary__c(
                  Date__c = a.Date__c,
                  Release__c = a.ID,
                  Doors__c = a.Doors_Per_Rep__c,
                  Name = a.Rep_One__c));
          }
          if(a.Rep_Two__c!=NULL){
            newSummary2.add(new Rep_Summary__c(
                  Date__c = a.Date__c,
                  Release__c = a.ID,
                  Doors__c = a.Doors_Per_Rep__c,
                  Name = a.Rep_Two__c));
          }
          if(a.Rep_Three__c != NULL){
            newSummary2.add(new Rep_Summary__c(
                  Date__c = a.Date__c,
                  Release__c = a.ID,
                  Doors__c = a.Doors_Per_Rep__c,
                  Name = a.Rep_Three__c));
          }
          if(a.Rep_Four__c != NULL){
            newSummary2.add(new Rep_Summary__c(
                  Date__c = a.Date__c,
                  Release__c = a.ID,
                  Doors__c = a.Doors_Per_Rep__c,
                  Name = a.Rep_Four__c));
          }
           if(a.Rep_Five__c != NULL){
            newSummary2.add(new Rep_Summary__c(
                  Date__c = a.Date__c,
                  Release__c = a.ID,
                  Doors__c = a.Doors_Per_Rep__c,
                  Name = a.Rep_Five__c));
          }
          if(a.Rep_Six__c != NULL){
            newSummary2.add(new Rep_Summary__c(
                  Date__c = a.Date__c,
                  Release__c = a.ID,
                  Doors__c = a.Doors_Per_Rep__c,
                  Name = a.Rep_Six__c));
          }
          if(a.Rep_Seven__c != NULL){
            newSummary2.add(new Rep_Summary__c(
                  Date__c = a.Date__c,
                  Release__c = a.ID,
                  Doors__c = a.Doors_Per_Rep__c,
                  Name = a.Rep_Seven__c));
          }
          if(a.Rep_Nine__c != NULL){
            newSummary2.add(new Rep_Summary__c(
                  Date__c = a.Date__c,
                  Release__c = a.ID,
                  Doors__c = a.Doors_Per_Rep__c,
                  Name = a.Rep_Nine__c));
          }
          if(a.Rep_Ten__c != NULL){
            newSummary2.add(new Rep_Summary__c(
                  Date__c = a.Date__c,
                  Release__c = a.ID,
                  Doors__c = a.Doors_Per_Rep__c,
                  Name = a.Rep_Ten__c));
          }
        }
       }  
    insert newSummary;                                       
}

 

Hope so this helps you...!

 

Please mark this answer a Solution and please give kudos by clicking on the star icon, if this works out.

HamptonHampton

Tried Before Update, no dice. 

 

It is written in Version 29 and it is active.

 

The odd thing is that I had this in production and it had been working for months up until about 10 days ago and then it stopped.

HamptonHampton

So the reason why I think this wont work is that I want it to potentially insert multiple child records.

 

For example, if Rep_One__c != NULL, then create a record. If Rep_Two__c !=NULL, then create another child record, etc

 

Not sure if this matters but Rep_One__c, Rep_Two__c, etc are picklist fields.

 

Thanks,

 

Hampton

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi Hampton,

 

So, do changes like the following, create a list for each condition; add the creation of record and insert that at last.

 

trigger RepSummaryCreation on Video_Turf__c (after update) {
    List<Rep_Summary__c> newSummary1 = new List<Rep_Summary__c>();     

    List<Rep_Summary__c> newSummary2 = new List<Rep_Summary__c>();

    List<Rep_Summary__c> newSummary3 = new List<Rep_Summary__c>(); 

    List<Rep_Summary__c> newSummary4 = new List<Rep_Summary__c>();

    List<Rep_Summary__c> newSummary5 = new List<Rep_Summary__c>();

    List<Rep_Summary__c> newSummary6 = new List<Rep_Summary__c>();

    List<Rep_Summary__c> newSummary7 = new List<Rep_Summary__c>();

    List<Rep_Summary__c> newSummary9 = new List<Rep_Summary__c>();

    List<Rep_Summary__c> newSummary10 = new List<Rep_Summary__c>();


    for(Video_Turf__c a: trigger.new){
        if(a.Assigned__c == TRUE){
          if(a.Rep_One__c!=NULL){
            newSummary1.add(new Rep_Summary__c(
                  Date__c = a.Date__c,
                  Release__c = a.ID,
                  Doors__c = a.Doors_Per_Rep__c,
                  Name = a.Rep_One__c));
          }
          if(a.Rep_Two__c!=NULL){
            newSummary2.add(new Rep_Summary__c(
                  Date__c = a.Date__c,
                  Release__c = a.ID,
                  Doors__c = a.Doors_Per_Rep__c,
                  Name = a.Rep_Two__c));
          }
          if(a.Rep_Three__c != NULL){
            newSummary3.add(new Rep_Summary__c(
                  Date__c = a.Date__c,
                  Release__c = a.ID,
                  Doors__c = a.Doors_Per_Rep__c,
                  Name = a.Rep_Three__c));
          }
          if(a.Rep_Four__c != NULL){
            newSummary4.add(new Rep_Summary__c(
                  Date__c = a.Date__c,
                  Release__c = a.ID,
                  Doors__c = a.Doors_Per_Rep__c,
                  Name = a.Rep_Four__c));
          }
           if(a.Rep_Five__c != NULL){
            newSummary5.add(new Rep_Summary__c(
                  Date__c = a.Date__c,
                  Release__c = a.ID,
                  Doors__c = a.Doors_Per_Rep__c,
                  Name = a.Rep_Five__c));
          }
          if(a.Rep_Six__c != NULL){
            newSummary6.add(new Rep_Summary__c(
                  Date__c = a.Date__c,
                  Release__c = a.ID,
                  Doors__c = a.Doors_Per_Rep__c,
                  Name = a.Rep_Six__c));
          }
          if(a.Rep_Seven__c != NULL){
            newSummary7.add(new Rep_Summary__c(
                  Date__c = a.Date__c,
                  Release__c = a.ID,
                  Doors__c = a.Doors_Per_Rep__c,
                  Name = a.Rep_Seven__c));
          }
          if(a.Rep_Nine__c != NULL){
            newSummary9.add(new Rep_Summary__c(
                  Date__c = a.Date__c,
                  Release__c = a.ID,
                  Doors__c = a.Doors_Per_Rep__c,
                  Name = a.Rep_Nine__c));
          }
          if(a.Rep_Ten__c != NULL){
            newSummary10.add(new Rep_Summary__c(
                  Date__c = a.Date__c,
                  Release__c = a.ID,
                  Doors__c = a.Doors_Per_Rep__c,
                  Name = a.Rep_Ten__c));
          }
        }
       }  
    insert newSummary1;

    insert newSummary2;

    insert newSummary3;

    insert newSummary4;

    insert newSummary5;

    insert newSummary6;

    insert newSummary7;

    insert newSummary9;

    insert newSummary10;                                      
}

 

Hope so this helps you...!

 

Please mark this answer a Solution and please give kudos by clicking on the star icon, if this works out.

HamptonHampton

Tried that, still not creating the records.