1
transact.Transaction_Date__c=Date.Today();

Above is my code. Why am i getting yesterdays date?

  • Most likely due to timezone in org vs where you are? – Eric Jul 6 at 20:12
3

A possible cause would be if the field Transaction_Date__c is a DateTime field, assigning a Date to it would set the hours/minutes/seconds to zero, and then the automatic timezone handling for your User would subtract (or add) hours and so move the Date part to the previous day. A quick fix for that would be:

transact.Transaction_Date__c=DateTime.Now();

but better to change the type of the field Transaction_Date__c to Date if that is what it represents.

If not the above, search for other code or configuration in your system that modifies the Transaction_Date__c field as the code you have posted looks correct.

  • Hai @keith C.. I tried using DateTime.Now( ); which gives me an error "DateTime variable doesn't exist"... so sick of being confused for this title date problem... help me with this please – Lokesh Jun 29 at 8:26
  • @Lokesh I don't understand that. If Transaction_Date__c is of type Date it won't help anyway. Post more of your code. – Keith C Jun 29 at 8:56
  • List<Transaction__c> bal = [Select Balance__c from Transaction__c ]; System.debug('Balance' + bal); transact.Transaction_Date__c=Date.Today(); if(!bal.isEmpty() && transact.Action__c == 'Deposit'){ transact.Balance__c = bal[0].Balance__c + transact.Amount__c; } else if(!bal.isEmpty() && transact.Action__c == 'Withdrawal'){ transact.Balance__c = bal[0].Balance__c - transact.Amount__c; } System.debug('Final Detail' + transact); insert transact; – Lokesh Jun 29 at 14:39
  • @Lokesh Nothing much new in there. How are you presenting the data e.g. apex:outputField? – Keith C Jun 29 at 14:44
  • ....No i am not using in any where i am just calculating the current balance using the date...and i check the date through standard related list Home page – Lokesh Jun 30 at 13:20
0

I found the problem.It was my TimeZone setting in Org. I changed my "User" settings under setup -> User -> Time Zone.

Problem Solved

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.