You need to sign in to do that
Don't have an account?
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;
}
}
}
}
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.
try this
Cahnge this
if (Contratos[0].Status_do_Contrato__c == 'Emitir Boletos')
to
See this where i have updated in your code
let me know if any issue in it.
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.
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:
I´m really lost here.
Could you help me to understand and solve it?
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