You need to sign in to do that
Don't have an account?
Apex Trigger with different behavior
Hello,
We are having a strange behavior with a trigger here in our system, can anyone tell how we can have this behavior?
The process is like this:
- A check box is selected on a visualpage and a record is updated.
- After this update, the trigger fire and a master detail object record is created. This object is like a history log, where we store the action the user have done.
- After this, the workflow rules start the avaliation and one workflow rule will calculate a new value for a field, because the check box was clicked, and that field will updated.
By here everything is ok, and working as expected. NEXT is the problem:
Sometime after the workflow rules, the trigget will fire again and will create a new History log, for the exact same action at the same time. But this only happens some times and we are not understanding why.
The Trigger look like this:
trigger InfoLog on Info_Demandante_APC__c (before update) { Info_Demandante_APC__c infoOld = Trigger.old[0]; Info_Demandante_APC__c infoNew = Trigger.new[0]; List<Info_log__c> newLogs = new List<Info_log__c>(); if((infoOld.preguntas_criticas_completadas__c != infoNew.preguntas_criticas_completadas__c) && infoNew.preguntas_criticas_completadas__c) { Info_log__c newLog = new Info_log__c(); newLog.Info_Demandante_APC__c = infoNew.id; newLog.Fecha__c = Datetime.now(); newLog.Accion__c = 'Preguntas críticas'; newLog.Descripcion__c = Datetime.now() + ' - El demandante ha contestado a las preguntas críticas'; newLogs.add(newLog); } insert newLogs; }
Can you help me why we are having this behavior?
Thanks,
André Cunha
First Try this
Take that insert new logs statement because you are performning on update
Second Try this
Workflow and Trigger are in sync.
Here Trigger is also on before update
Workflow is also on a before update because its on same object.
So every time before update action occurs the trigger criteria performs .
So wat i would suggest you is to perform the logic of workflow in the trigger so that evrything will go well and delete the workflow
I had a problem like this today. The combination of triggers and workflows can cause unexpected behaviour. The root of the problem is that we are likely to think of the two trigger firings as two separate events, but to salesforce they are part of a single transaction. One result of this that the value of trigger.old stays the same throughout, rather than being different the second time it fires. I imagine there are probably other effects like this that we would not necessarily expect.
I solved the double-logging problem by creating a static map and putting the log entries in there before inserting them. That way you can check if a log entry already exists (by checking the map) before you insert it again.
I hope this helps you find a solution