Skip to main content

Hi all, 

I have to write a trigger to clear date field when date is today.

So let's say we have date field called  'abc' and date for 'abc' is 24.06.2019 when today's date will be 24.06.2019 field 'abc' should be automatically clear.

Which function should I use to achieve this ? Before update, after insert ?

2 respostas
  1. 21 de mai. de 2019, 06:55
    Hi Tomas

    An interesting requirement.  

    To answer the first part of your question - it depends on where is the trigger to be fired from?

    If we are update the record via the UI, then I would do a trigger on the Object as a ( before Insert , before Update). This is due to if we are editing the record and do an after update, we risk the trigger being triggered into a loop. Remember, each DML (write) would trigger the After Update trigger - which writes to the record and then invokes the After update Trigger - which.... i think you get the idea.

    If we are clearing the date field in a related object, then the After Trigger comes into play.

    Note - a trigger fired from the object will not clear the field if the record is not edited on the day of the date field. 

    If the requirement is to clear the date field when the date passes, then you need to look at batch Apex jobs.  Or Time based Work Flows.

    Regards

    Andrew
0/9000