function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JuanTalJuanTal 

Insert child fields with 2 custom field

my parent custom object is Presupuesto__c, my child custom object is Movimiento_Presupuestal__c,  i wanna create new field in child object with a trigger when i insert new parent field:

 

trigger calculosPresupuesto on Presupuesto__c (before insert, before update) {

 

List<Sobject> child = new List<Sobject>();
 if(Trigger.isInsert){         
  Double presAnual;  
  String presid;  
  Date fecha_hoy = Date.today(); 
  Presupuesto__c [] pre = Trigger.new;      
  
  for(Presupuesto__c p : pre){   
     if(p.Presupuesto_Anual_Aprobado__c != 0){
    Double valorMes = p.Presupuesto_Anual_Aprobado__c / 12;
    presAnual = p.Presupuesto_Anual_Aprobado__c;
    presid = p.Id;
  p.Presupuesto_de_Enero__c = valorMes;
  p.Presupuesto_de_Febrero__c = valorMes;
  p.Presupuesto_de_Marzo__c = valorMes;
  p.Presupuesto_de_Abril__c = valorMes;
  p.Presupuesto_de_Mayo__c = valorMes; 
  p.Presupuesto_de_Junio__c = valorMes;
 p.Presupuesto_de_Julio__c = valorMes;
p.Presupuesto_de_Agosto__c = valorMes;
p.Presupuesto_de_Septiembre__c = valorMes; 
p.Presupuesto_de_Octubre__c = valorMes;  
p.Presupuesto_de_Noviembre__c = valorMes;
p.Presupuesto_de_Diciembre__c = valorMes;
    
p.Balance_Enero__c = p.Presupuesto_de_Enero__c - p.Gastos_Enero__c - p.Reserva_Enero__c;
p.Balance_Febrero__c = p.Presupuesto_de_Febrero__c - p.Gastos_Febrero__c - p.Reserva_Febrero__c;
p.Balance_Marzo__c = p.Presupuesto_de_Marzo__c - p.Gastos_Marzo__c - p.Reserva_Marzo__c;
p.Balance_Abril__c = p.Presupuesto_de_Abril__c - p.Gastos_Abril__c - p.Reserva_Abril__c;
p.Balance_Mayo__c = p.Presupuesto_de_Mayo__c - p.Gastos_Mayo__c - p.Reserva_Mayo__c;
p.Balance_Junio__c = p.Presupuesto_de_Junio__c - p.Gastos_Junio__c - p.Reserva_Junio__c;
     p.Balance_Julio__c = p.Presupuesto_de_Julio__c - p.Gastos_Julio__c - p.Reserva_Julio__c;
p.Balance_Agosto__c = p.Presupuesto_de_Agosto__c - p.Gastos_Agosto__c - p.Reserva_Agosto__c;
p.Balance_Septiembre__c = p.Presupuesto_de_Septiembre__c - p.Gastos_Septiembre__c - p.Reserva_Septiembre__c;
    p.Balance_Octubre__c = p.Presupuesto_de_Octubre__c - p.Gastos_Octubre__c - p.Reserva_Octubre__c;
p.Balance_Noviembre__c = p.Presupuesto_de_Noviembre__c - p.Gastos_Noviembre__c - p.Reserva_Noviembre__c;
p.Balance_Diciembre__c = p.Presupuesto_de_Diciembre__c - p.Gastos_Diciembre__c - p.Reserva_Diciembre__c;
p.Balance_Anual__c = p.Presupuesto_Anual_Aprobado__c - p.Gasto_Anual__c - p.Reserva_Anual__c;   
 
Movimiento_Presupuestal__c mov = new Movimiento_Presupuestal__c
(Fecha_de_Movimiento__c = fecha_hoy, 
Abono__c = presAnual, 
cuenta__c = p.Id );  
child.add(mov); 
insert child;
}       

 

}

 

but the error is:

 

 

Description Resource Path Location Type

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, calculosPresupuesto: execution of BeforeInsert

 

caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, required fields missings: [Cuenta__c]: [Cuenta__c]

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
dnakonidnakoni

You're doing:

 

for(Presupuesto__c p : pre)

 

The error you are getting is on "execution of BeforeInsert"

 

The reason for this is that on Before Insert triggers, all the records do not have their IDs assigned yet. Therefore this piece of code:

 

cuenta__c = p.Id

 

Assigns NULL to cuenta__c because p.Id is null.

 

Try doing this on an After Insert trigger and see if it works.