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
Oussama ElhaddadOussama Elhaddad 

Check if datetime field is empty

Hi folks,

I've been trying to look for a solution with no luck.
Simply, I'm trying to write an apex trigger to check whether a date/time field is empty while a status field is set to a specific value at the creation or the update of a record...

Any help much appreciated. Thanks
sunny522sunny522
Hi Elhaddad,
you can directly check if Datetime field is empty using  == null condition as shown in example below.

trigger SameEmailOnAllContacts on Account (after update) {
    Set<Id> setAccountId = new Set<Id>();
    for(Account acc:trigger.new) {
        if(acc.Date_Time__c == null) {
            setAccountId.add(acc.id);
        }
    }
    system.debug('setAccountId:'+setAccountId);
    
}

 
Deepali KulshresthaDeepali Kulshrestha
Hi Oussama,
 
Greetings to you!

I have practiced it in my org on custom object help.
 
trigger HelpTrigger on Help__c (before insert,before Update) {
    if(Trigger.isInsert && Trigger.isBefore){
        for(Help__c helpObj:trigger.new) {
            if(helpObj.DateTime__c == null) {
                helpObj.Status__c = 'created with update';
            }
        }
    }
    if(Trigger.isUpdate && Trigger.isBefore){
        for(Help__c helpObj:trigger.new) {
            if(helpObj.DateTime__c == null) {
                helpObj.Status__c = 'updated';
            }
        }
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
Ajay K DubediAjay K Dubedi
Hi Oussama Elhaddad,

You can update your code by below code according to your requirement in the apex class.

If You are applying null check in query.
    
List<Account> accountList = [SELECT Id,DemoField__c,Name FROM Account WHERE DemoField__c!=null];
System.debug('accountList  :::'+accountList);

If You are filtering records after getting it in a list.

List<Account> accountList = [SELECT Id,DemoField__c,Name FROM Account];
List<Account> updatedList = new List<Account>();
for(Account accObj : accountList){
    if(accObj.DemoField__c!=null){
        updatedList.add(accObj);
    }
}

System.debug('updatedList   :::'+updatedList);
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
 
Oussama ElhaddadOussama Elhaddad

Thanks, Guys for the replies. Please see below the script I'm using.
The error I'm getting in VS editor is the following: Method does not exist or incorrect signature: void isEmpty
Thanks again for your support!

trigger posIntallation on Installation__c (after insert, after update) {
List<Installation__c> apps = new List<Installation__c>();
apps = [select Installation_Date_Time__c from Installation__c where Status__c = 'Assigned'];
if (isEmpty(apps)) {
System.debug('Please choose the installation date and time for this application');
}
}
Deepali KulshresthaDeepali Kulshrestha
Hi Oussama Elhaddad
 
Greetings to you!

I have practiced it in my org on custom object help.
trigger HelpTrigger on Help__c (before insert,before Update) {
    if(Trigger.isInsert && Trigger.isBefore){
        for(Help__c helpObj:trigger.new) {
            if(helpObj.DateTime__c == null) {
                helpObj.Status__c = 'created with update';
            }
        }
    }
    if(Trigger.isUpdate && Trigger.isBefore){
        for(Help__c helpObj:trigger.new) {
            if(helpObj.DateTime__c == null) {
                helpObj.Status__c = 'updated';
            }
        }
    }
}
    
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha