You need to sign in to do that
Don't have an account?

Can not set Custom DateTime field to null in trigger.
I have created custom DateTime field on Lead "Status Change Date" it should store the date when status was changed from Open to other than Open.
Trigger code:
public static void updateStatusChangeDate() { String oldStatus = ''; String newStatus = ''; for (Lead l : Trigger.new) { oldStatus = Trigger.oldMap.get(l.Id).Status; newStatus = String.ValueOf(lead.Status); if (!newStatus.equals('Open') && oldStatus.equals('Open')) { l.ptt_Status_change_date__c = DateTime.now(); } else if (newStatus.equals('Open') && !oldStatus.equals('Open')) { l.ptt_Status_change_date__c = null; // This don't clear the value. } } }
Problem is that l.ptt_Status_change_date__c = null; won't work.
It does not set the field to empty value.
Please advise how to do that.
I have modified code to:
An got results (Opne to Contacted):
10:25:21:663 USER_DEBUG [46]|DEBUG|@@ oldStatus: Open
10:25:21:663 USER_DEBUG [47]|DEBUG|@@ newStatus: Status
10:25:21:663 USER_DEBUG [49]|DEBUG|@@ Before field update to date
10:25:21:664 USER_DEBUG [51]|DEBUG|@@ After field update to date
From Contacted to Open:
10:26:11:243 USER_DEBUG [46]|DEBUG|@@ oldStatus: Contacted
10:26:11:243 USER_DEBUG [47]|DEBUG|@@ newStatus: Status
It seems that newStatus = String.ValueOf(lead.Status); retuns always "Status". I would not expect it.
Finnaly I have found the problem it should be newStatus = l.Status; now it works fine.
Stupid mistake.
Maybe it will help somone in future.
All Answers
I have modified code to:
An got results (Opne to Contacted):
10:25:21:663 USER_DEBUG [46]|DEBUG|@@ oldStatus: Open
10:25:21:663 USER_DEBUG [47]|DEBUG|@@ newStatus: Status
10:25:21:663 USER_DEBUG [49]|DEBUG|@@ Before field update to date
10:25:21:664 USER_DEBUG [51]|DEBUG|@@ After field update to date
From Contacted to Open:
10:26:11:243 USER_DEBUG [46]|DEBUG|@@ oldStatus: Contacted
10:26:11:243 USER_DEBUG [47]|DEBUG|@@ newStatus: Status
It seems that newStatus = String.ValueOf(lead.Status); retuns always "Status". I would not expect it.
Finnaly I have found the problem it should be newStatus = l.Status; now it works fine.
Stupid mistake.
Maybe it will help somone in future.
Please mark your solution as resolved. It will help others:).