I have a custom field (custom_field__c) on lead object and in my trigger i want to check difference between NOW() and current value of custom_field__c. And if difference is more than 5 seconds then do some action.

So for this requirement will below formula work 100% ? or are there any limitations or problems in it. Thanks for help:)

( NOW() - custom_field__c )*24*60*60

Also how to check if dateTime field is empty: Will custom_field__c == Null works?


You need to convert the datetime to Long to calculate time difference. Refer DateTime.getTime() method

Like CurrentTime DateTime.now().getTime() will return 1490556040011L

As per your requirement the logic should be like this:

if(Custom_Field__c !=null)
    Long customFieldLong = Custom_Field__c.getTime() ;
    Long currentDTLong = DateTime.now().getTime();
    Long milliseconds = currentDTLong - customFieldLong;
    Long seconds = milliseconds / 1000;
    //perform the desired action

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.