Apex Design Pattern to Avoid Recursive Trigger | Approach Two

Assume a workflow where you do a field update based on some criteria match and on the same field if we set Trigger that creates a Task. What...

Assume a workflow where you do a field update based on some criteria match and on the same field if we set Trigger that creates a Task.

What happens next if someone activate this workflow ?

You will get 'two' tasks on each opportunity and result in test failure 

zVhRk5owEP41zLQPvQECKK9nvfalc53eda59RFkxYyRMCCfer2+QBExEz1Po3YMObGCz++337UYtNFmX31iULX/QGIjl2nFpoa+W645tW3xXhq1hSBiOa5MjDQWOIddMnFLCcaYb5zRNYc4124IS3VkWJXBgeJhH5ND6hGO+lMG5QWv/DjhZqm2cIKxXcr5VPmJYRAXhX3Ymt14ubfmCK5/fSkOoHKdaAC+UrjUDgxy/6EEusIxCIjejLAammQhOV/sooamFJoxSXl+tywmQqiwK8vq1uyOrDTgMUn7OCzL554gUMnSrQhKVkYVuHxlOEhGvivUZGIfy6E5t/IJSQNfA2bYCtl5FyoukUyhvN20RHfXIcq+AgbRFEqWk8dymJS5kZt1Zoo4sDUpALFgmb0VGmG9/AYk4pum0Wdm9Uz14IQRO+DoEdgcEjbELA7nbT4pFEM1Wrq9v5RseclqwOciX9vlh+DGr1pRIOeIRS4AfONpVpEHgrCJ5p6g4YRBxqPYVn8coXx0UkNEijaHy5fTAVt9+P7b6r7NVdA3oIUvPM6rr+AdpBh1Z+tcnGfQjSXQrVtj2j3jEVjd/q5sb/1y5imG143BtGp1WcKADJqQ5mISRIWHnYg0bXHZHg2l4dErDT5StFoRu+hgmyEjJ+4/6HPdF3asHimtUFgUD0tEYXsh0cTYdjdo1Tah/Ooan6Niebj49gDiaxlUnwGv4PAA/va4+MRQ/1V4fZJI6BhT26OZwygwHhtMBxkDT1GjYHnI7Mh1onqq09vK8zzLKeJGK7rM7wjcU2GARjGa5w0Aq/v/O4h05DIDyZZRVl0soo4SmPWDlGAI5Vx/jHihxzgFLa+BkRjfakaMyiIUKASx+mV7dx/2RgcZ4uD4emKPz4j4+PhJ0/32887TYkPdeEFL81K4ZXEgGt2RvTh02znXW4/SZriC2FNH2608IzvKq2Ir7MS1mBKbSfr0CzDp01tzpqHnYhwS6JuO7SiAwxgQa8GQdGHJrpvVbJeC5R4LuXwIqxEsk0J50PpYC/NdLHr614kcUIG7bP9Vq+Ns/PdH0Hw==


In my last post here - I have already discussed this issue, but I believe this is more well driven efficient approach than the previous one.

The very first solution comes in mind is using 'Static variable' to keep track of whether  a task has already been created in this execution context, and simply exit when the variable is set. However, the solution fails in the cases when workflow itself doing field update that you want to track.

The better approach that I implemented was mixture of my experience and 'Dan Appleman' approach which deals with keeping track of 'correct old-value' the value that is set during the previous trigger invocation.

Code snippet below explains the approach



Find Dan Appleman here and his outstanding piece of work here

What Others Are Reading