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
PawelWozniakPawelWozniak 

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.

Best Answer chosen by Admin (Salesforce Developers) 
PawelWozniakPawelWozniak

 

I have modified code to:

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); //This should be changed to l.Status.
     		System.Debug('@@ oldStatus: ' + oldStatus);
     		System.Debug('@@ newStatus: ' + newStatus);
			if (!newStatus.equals('Open') && oldStatus.equals('Open')) {
				System.Debug('@@ Before field update to date');
				l.ptt_Status_change_date__c = DateTime.now();
				System.Debug('@@ After field update to date');	
			} else
			if (newStatus.equals('Open') && !oldStatus.equals('Open')) {
				System.debug('@@ Before field update to null');
				l.ptt_Status_change_date__c = null;
				System.debug('@@ After field update to null');
			}	
		}     	
     }

 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

Bhawani SharmaBhawani Sharma
This should work. Make sure you do not have wf rule or any other code which is populating data in this field. Add debug statement inside else if condition.
PawelWozniakPawelWozniak

 

I have modified code to:

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); //This should be changed to l.Status.
     		System.Debug('@@ oldStatus: ' + oldStatus);
     		System.Debug('@@ newStatus: ' + newStatus);
			if (!newStatus.equals('Open') && oldStatus.equals('Open')) {
				System.Debug('@@ Before field update to date');
				l.ptt_Status_change_date__c = DateTime.now();
				System.Debug('@@ After field update to date');	
			} else
			if (newStatus.equals('Open') && !oldStatus.equals('Open')) {
				System.debug('@@ Before field update to null');
				l.ptt_Status_change_date__c = null;
				System.debug('@@ After field update to null');
			}	
		}     	
     }

 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.

 

This was selected as the best answer
Bhawani SharmaBhawani Sharma
Ohh.. minor mistakes can be time killing :).
Please mark your solution as resolved. It will help others:).