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
brunol11brunol11 

Trigger Duplicates Records when running for the first time

I Have 2 objects called "Contratacao__c" and "Faturamento__c".

 

When a field in Contratacao__c named "Status do Contrato" has the value "Emitir Boletos", the TRIGGER below generates 12 registers on "Faturamento__c".

 

But when the trigger runs for the first time after an user selected the value "Emitir Boletos" in "Contratacao__c", it generates 24 instead of 12. As far as I can see, the 12 extra registers are the original 12 registers duplicated.

 

If I Delete the 24 registers and select the field value again, the Trigger generates 12 "Faturamento__c" as I want to.

 

It happens only at first time the value is selected.

 

What could be happennig with this trugger? 

How can I solve this?

 

Here´s the Code:

 

trigger CreateBilling on Contratacao__c (after update, after insert) {
 for (Contratacao__c contract : Trigger.new) {       

     string dia_venc = contract.Vencimento__c;         

  integer boleto = 0;

  date  hoje = date.today();
  string mes_venc = String.valueOf(hoje.month());

  string ano_venc = String.valueOf(hoje.year());

  string format_data =  dia_venc + '/' + mes_venc + '/'+ ano_venc;

  date data_venc = date.parse(format_data);

while (boleto < 12) {   

 boleto ++;   

 data_venc = data_venc.addMonths(1);    

 Contratacao__c[] Contratos = Trigger.new;
 if (Contratos[0].Status_do_Contrato__c == 'Emitir Boletos') 

    { 

      Faturamento__c Billing = new Faturamento__c ( 

       Contrato__c = Contratos[0].id, 

       F__c = Contratos[0].Total_do_Contrato__c, 

       V__c = data_venc                                                     ); 

      insert Billing;

     }

 } 

}

}

 

My OwnMy Own

 

This trigger action(insert) will be excuted in 2 conditions, like while insertion and updation.   

 

   a. If this is first time(Insert), then you can proceed with what you are trying to do. 

   b. If it is Update action, you will be having 12 old records and now again you are trying to insert the 12 records, instead of updating the old once. because of this reasion you were getting 24 instead 12 records.  differetiate with Trigger.Isinsert() and trigger.Isupdate() methods seperately.  

 

try this code. 

trigger CreateBilling on Contratacao__c (after update, after insert) {
	
		trigger.Isinsert(){
			List<Faturamento__c> lst = new List<Faturamento__c>();
			 for (Contratacao__c contract : Trigger.new) {       
			      string dia_venc = contract.Vencimento__c;         
				  integer boleto = 0;
				  date  hoje = date.today();
				  string mes_venc = String.valueOf(hoje.month());
				  string ano_venc = String.valueOf(hoje.year());
				  string format_data =  dia_venc + '/' + mes_venc + '/'+ ano_venc;
				  date data_venc = date.parse(format_data); 
				  
				while (boleto < 12) {   
				 boleto ++;   
				 data_venc = data_venc.addMonths(1);    
				 Contratacao__c[] Contratos = Trigger.new;
				 if (Contratos[0].Status_do_Contrato__c == 'Emitir Boletos') 
				    { 
				      Faturamento__c Billing = new Faturamento__c ( 
				       												Contrato__c = Contratos[0].id, 
				       												F__c = Contratos[0].Total_do_Contrato__c, 
				       												V__c = data_venc
				       											); 
				      
				      lst.add(Billing); 
				      			      
				     }
				 }
				 
				  if(!lst.isEmpty())
				 	insert lst; 
			}
			
		}
		trigger.Isupdate(){			
			Map<Id, Faturamento__c> mapExisting = new Map<Id, Faturamento__c>([select id,name,Contrato__c,F__c,V__c from Faturamento__c where 'your condition to get the existing records']);
			List<Faturamento__c> lst = new List<Faturamento__c>();
			for (Contratacao__c contract : Trigger.old) {       
			      string dia_venc = contract.Vencimento__c;         
				  integer boleto = 0;
				  date  hoje = date.today();
				  string mes_venc = String.valueOf(hoje.month());
				  string ano_venc = String.valueOf(hoje.year());
				  string format_data =  dia_venc + '/' + mes_venc + '/'+ ano_venc;
				  date data_venc = date.parse(format_data); 
				  
				while (boleto < 12) {   
				 boleto ++;   
				 data_venc = data_venc.addMonths(1);    
				 Contratacao__c[] Contratos = Trigger.new;
				 if (Contratos[0].Status_do_Contrato__c == 'Emitir Boletos') 
				    { 
				      Faturamento__c Billing = mapExisting.get(); // Record comparision and get the record. 
				      if(Billing != null){
				      	//Do your logic
				      }
				      lst.add(Billing); 			      
				     }
				 }  
				 
				 if(!lst.isEmpty())
				 	update lst;
			}
			
		}
}

 

 

 

Shashikant SharmaShashikant Sharma

try this

Cahnge this 

if (Contratos[0].Status_do_Contrato__c == 'Emitir Boletos') 

to

if ((trigger.IsInsert && Contratos[0].Status_do_Contrato__c == 'Emitir Boletos') || (trigger.IsUpdate && Contratos[0].Status_do_Contrato__c != trigger.oldMap[Contratos[0].id].Status_do_Contrato__c && Contratos[0].Status_do_Contrato__c == 'Emitir Boletos')) 

 

 

See this where i have updated in your code

 

trigger CreateBilling on Contratacao__c (after update, after insert) {
 for (Contratacao__c contract : Trigger.new) {       
     string dia_venc = contract.Vencimento__c;         
  integer boleto = 0;
  date  hoje = date.today();
  string mes_venc = String.valueOf(hoje.month());
  string ano_venc = String.valueOf(hoje.year());
  string format_data =  dia_venc + '/' + mes_venc + '/'+ ano_venc;
  date data_venc = date.parse(format_data);

while (boleto < 12) {   
 boleto ++;   
 data_venc = data_venc.addMonths(1);    
 Contratacao__c[] Contratos = Trigger.new;
 
 if ((trigger.IsInsert && Contratos[0].Status_do_Contrato__c == 'Emitir Boletos') || (trigger.IsUpdate && Contratos[0].Status_do_Contrato__c != trigger.oldMap[Contratos[0].id].Status_do_Contrato__c && Contratos[0].Status_do_Contrato__c == 'Emitir Boletos')) 
    { 
      Faturamento__c Billing = new Faturamento__c ( 
       Contrato__c = Contratos[0].id, 
       F__c = Contratos[0].Total_do_Contrato__c, 
       V__c = data_venc                                                     ); 
      insert Billing;
     }
 } 
}
}

 let me know if any issue in it.

brunol11brunol11

Hi, Shashikant Sharma!

 

Thanks a lot for your Reply!

 

 

I Got the error:

 

Save error: Expression must be a list type: MAP<Id,Contratacao__c>

 

I´m really trying to understand but I don´t know how to proceed from here.

brunol11brunol11

Hi, My Own!

 

I really appreciate your Help!

 

I Didn´t get it:

"b. If it is Update action, you will be having 12 old records and now again you are trying to insert the 12 records, instead of updating the old once. because of this reasion you were getting 24 instead 12 records.  differetiate with Trigger.Isinsert() and trigger.Isupdate() methods seperately. "

 

What 12 old records if there is no record yet?

What part of the trigger could possibly insert 12 records twice? There is only one routine in the trigger. that´s my question:

 

  • After the record in "Contratacao__c" has been created there is no records in related list "Faturamento__c". 
  • I save the record in "Contratacao__c" and then I Change the status to "Emitir Boletos" for the first time and it gives me 24 records in  "Faturamento__c".
  • If I delete all the 24 records and change the status again to "Emitir Boletos", it creates only 12 records. 
  • why it works wrong on first time (after the record in Contratacao__c has been saved) and works fine at the second, third, etc?

 

I´m really lost here.

 

Could you help me to understand and solve it?

brunol11brunol11

Hi!

 

I´m still trying to understand your solution.

 

Could you please give more information on the logic? This line:

 

Map<Id, Faturamento__c> mapExisting = new Map<Id, Faturamento__c>([select id,name,Contrato__c,F__c,V__c from Faturamento__c where 'your condition to get the existing records']);

 

I can´t figure what condition to get the existing records could be inserted if the record in FATURAMENTO__C still not exists. I want to create 12 records in FATURAMENTO__C after update a record in CONTRATACAO__C.

 

Could you help me to understand?

 

Thanks a lot.

 

Best Regards,

Bruno