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
tomasddtomasdd 

Trigger question - date field clear

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 ?

Best Answer chosen by tomasdd
Andrew GAndrew G
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

All Answers

Andrew GAndrew G
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
This was selected as the best answer
Ajay K DubediAjay K Dubedi
Hi Tomas,
Let us take an example with opportunity Date_For_Test__c custom field and for your requirement, you can use both functions before update, before insert as if you will insert a record with today's date then it will be automatically cleared and if you will update a record with today's date then it will be also automatically cleared.
try below code:
trigger:
trigger ClearOpportunityClosedDate on Opportunity (before insert, before Update) {
    if((trigger.IsInsert && trigger.IsBefore) || (trigger.IsUpdate && trigger.IsBefore)) {
        ClearOpportunityClosedDate_handler.clearDate(trigger.new);
    }
}
hendler:
public class ClearOpportunityClosedDate_handler {
    public static void clearDate(List<Opportunity> oppList) {
        system.debug('oppList------->' + oppList);
        if(oppList.size() > 0) {
            try {
                system.debug('oppList.size()------->' + oppList.size());
                for(Opportunity op : oppList) {
                    if (op.Date_For_Test__c == date.today()) {
                        system.debug('op.Date_For_Test__c------->' + op.Date_For_Test__c);
                        op.Date_For_Test__c = null;
                    }
                }
            }
            catch(Exception ex) {
                system.debug('Exception---ofLine--->'+ex.getLineNumber());
                system.debug('Exception---Message--->'+ex.getMessage());
            }
        }
    }
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi